﻿// JScript File
var newsArray = [];
var item = 1;
var curHolderInit = "B";
var firsttime = true;
var paused = false;
var dir = "ff";
var staypaused = false;

//Set Download Item Class
function newsItem() {
			this.Date = null;
			this.Cat =  null;
			this.Link = null;
			this.Title = null;
			this.Desc = null;
}


$(document).ready(function() {

    imgstopreload = new Array(
		'../../images/newsfeed/icons_white/icon_white_article.gif',
		'../../images/newsfeed/icons_white/icon_white_audio.gif',
		'../../images/newsfeed/icons_white/icon_white_bio.gif',
		'../../images/newsfeed/icons_white/icon_white_blog.gif',
		'../../images/newsfeed/icons_white/icon_white_casestudy.gif',
		'../../images/newsfeed/icons_white/icon_white_datasheet.gif',
		'../../images/newsfeed/icons_white/icon_white_executable.gif',
		'../../images/newsfeed/icons_white/icon_white_forum.gif',
		'../../images/newsfeed/icons_white/icon_white_presentation.gif',
		'../../images/newsfeed/icons_white/icon_white_video.gif'
	);
    for (x in imgstopreload) { (new Image).src = imgstopreload[x]; }


    $('#newsItemA').bind("mousedown", function() { window.location = newsArray[item].Link; });
    $('#newsItemB').bind("mousedown", function() { window.location = newsArray[item].Link; });

    var sPath = window.location.pathname;
    var sLoc = sPath.substring(0, sPath.lastIndexOf('/') + 1);
    var newsLoc = sLoc + "xml/News.xml";
    // Check to see if menuslider has a localized version, if not use global version
    $.ajax({
        url: newsLoc,
        type: 'GET',
        success: function() {
            buildnews();
        },
        error: function() {
        $("div .ticker_rss").html("<a href='../../en/us/xml/NewsRss.xml'></a>");
            newsLoc = '../../en/us/xml/News.xml';
            buildnews();
        }
    });


    function buildnews() {
        $.get(newsLoc, {}, function(newsXML) {
        //newsXML = parseXml(newsXML);
            $('News', newsXML).each(function(i) {
                var curItem = new newsItem();
                curItem.Date = $(this).find("Posted_x0020_Date").text();
                curItem.Cat = $(this).find("Category").text();
                curItem.Title = $(this).find("Title").text();
                curItem.Link = $(this).find("Link").text();
                curItem.Desc = $(this).find("Description").text();
                newsArray.push(curItem);
            });
            //Set A Contents
            myHTMLOutput = "<span class='date'>" + newsArray[1].Date + "</span><span class='icon'><img title='" + newsArray[1].Cat + "' alt='" + newsArray[1].Cat + "' src='../../images/newsfeed/icons_white/icon_white_" + newsArray[1].Cat + ".gif'/></span><span>" + newsArray[1].Desc + "</span>";
            document.getElementById("newsItemA").innerHTML = myHTMLOutput;
            //Set B Contents
            myHTMLOutput = "<span class='date'>" + newsArray[0].Date + "</span><span class='icon'><img title='" + newsArray[0].Cat + "' alt='" + newsArray[0].Cat + "' src='../../images/newsfeed/icons_white/icon_white_" + newsArray[0].Cat + ".gif'/></span><span>" + newsArray[0].Desc + "</span>";
            document.getElementById("newsItemB").innerHTML = myHTMLOutput;
            firsttime = false;
            item = 0;
            setTimeout("slideNews(0)", 4000);
        });
    }
});

function slideNews (curItem) {	
	if(!paused){
		//Slide for Offscreen Left to Right
		if (curHolderInit=="A"){
			curHolderInit = "B";
			//Slide A in Place
			$("#newsItemA").animate({ left: "400px","opacity":"0"},1000).animate({left:"-715px"},1);
			//Slide B to Right and Fade
			$("#newsItemB").animate({"opacity":"1", left: "0px" } , 1000);
			//Load next Item into B
			cycleItems();
			loadItem();

		}else{
			curHolderInit = "A";
			//Slide B in Place
			$("#newsItemB").animate({ left: "400px" ,"opacity":"0"},1000).animate({left:"-715px"},1);
			//Slide A to Right and Fade
			$("#newsItemA").animate({"opacity":"1", left: "0px" } , 1000);
			//Load Next Item into A
			cycleItems();
			loadItem();

		}
	}
	setTimeout("slideNews(0)",4000);
}

function cycleItems(){ 
	if(dir=="ff"){
		if(item<newsArray.length-1){
			item+=1;
		}else{
			item=0;
		}
	}else{
		if(item>0){
			item-=1;
		}else{
			item=newsArray.length-1;
		}
	}
}

function loadItem(){
	myHTMLOutput = "";
    myHTMLOutput = "<span class='date'>"+newsArray[item].Date+"</span><span class='icon'><img title='"+newsArray[item].Cat+"' alt='"+newsArray[item].Cat+"' src='../../images/newsfeed/icons_white/icon_white_"+newsArray[item].Cat+".gif'/></span><span>"+newsArray[item].Desc+"</span>";
   	var nextHolder = "newsItem"+curHolderInit;
	document.getElementById(nextHolder).innerHTML=myHTMLOutput;
}

function news_pause(){
		paused = true;
		$('#btnPP').css({backgroundPosition: '8px -43px'});
}

function news_ff(){
	dir = "ff";
	news_pause();
	staypaused = true;
	//Step Foward
	if (curHolderInit=="A"){
			curHolderInit = "B";
			//Load next Item into B
			cycleItems();
			loadItem();
			//Slide A in Place
			$("#newsItemA").animate({"opacity":"-1", left: "400px" } , 1000).animate({left:"-715px"},1);
			//Slide B to Right and Fade
			$("#newsItemB").animate({"opacity":"1", left: "0px" } , 1000);
		}else{
			curHolderInit = "A";
			//Load Next Item into A
			cycleItems();
			loadItem();
			//Slide B in Place
			$("#newsItemB").animate({"opacity":"-1", left: "400px" } , 1000).animate({left:"-715px"},1);
			//Slide A to Right and Fade
			$("#newsItemA").animate({"opacity":"1", left: "0px" } , 1000);
			
		}	
}

function news_rw(){
	dir = "rw";
	news_pause();
	staypaused=true;
	//Step Foward
	if (curHolderInit=="A"){
			curHolderInit = "B";
			//Load next Item into B
			cycleItems();
			loadItem();
			//Slide A in Off Stage and Fade Out
			$("#newsItemA").animate({ left: "-715px" } , 1000).animate({"opacity":"0"},1);
			//Slide B to Left and Fade In
			$("#newsItemB").animate({left: "715px" } , 1).animate({"opacity":"1",left:"0px"},1000);	
		}else{
			curHolderInit = "A";
			//Load Next Item into A
			cycleItems();
			loadItem();
			//Slide B in Place
			$("#newsItemB").animate({left: "-715px" } , 1000).animate({"opacity":"0"},1);
			//Slide A to Left and Fade In
			$("#newsItemA").animate({left: "715px" } , 1).animate({"opacity":"1",left:"0px"},1000);		
		}	
}

function btnPP_Over(){
	if(!paused){
		$('#btnPP').css({backgroundPosition: '8px -21px'});
	}else{
		$('#btnPP').css({backgroundPosition: '8px -65px'});
	}
}

function btnPP_Out(){
	if(!paused){
		$('#btnPP').css({backgroundPosition: '8px 1px'});
	}else{
		$('#btnPP').css({backgroundPosition: '8px -43px'});
	}
}

function btnPP_Click(){
	if(paused){
		paused=false;
		staypaused=false;
		$('#btnPP').css({backgroundPosition: '8px -21px'});
	}else{
		paused=true;
		staypaused=true;
		$('#btnPP').css({backgroundPosition: '8px -65px'});
	}
}

function restart(){
	if(!staypaused){
		//btnPP_Click();
		if(paused){
			paused=false;
			staypaused=false;
			$('#btnPP').css({backgroundPosition: '8px 1px'});
		}else{
			paused=true;
			staypaused=true;
			$('#btnPP').css({backgroundPosition: '8px -43px'});
		}
	}
}

function news_reset(){
	dir = "ff";
}

function parseXml(xml){
	if (jQuery.browser.msie)
	{
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.loadXML(xml);
		xml = xmlDoc;
	}
	return xml;
}
