﻿
var rootNodes, getRootChid;
var boxnum, feedboxnum, videochildren, videonum, photochildren, photonum;
var getXMLFeed ="";

var filenames = [
				 {type: 'youtubeplaylist', file: "http://www.eawiipushingplay.com/Chatter/List/YouTubePlaylist/", fnum: 50, profile: getCookie("profile") },
				 {type: 'sixaparttwitter', file: "http://www.eawiipushingplay.com/Chatter/List/TwitterEntryList/", fnum: 50, profile: getCookie("profile") },
				 {type: 'sixapartarticles', file: "http://www.eawiipushingplay.com/Chatter/List/ArticleEntryList/", fnum: 50, profile: getCookie("profile") },
				 {type: 'flickr', file: "http://www.eawiipushingplay.com/Chatter/List/FlickrEntryList/", fnum: 50, profile: getCookie("profile") },
				 {type: 'reviews', file: "http://www.eawiipushingplay.com/Chatter/List/ReviewsEntryList/", fnum: 50, profile: getCookie("profile") }
				];

var twitterarr = new Array();
var articlearr = new Array();
var youtubearr = new Array();
var youtubearrId = new Array();
var flickrarrId = new Array();
var flickrarr = new Array();
var reviewsarr = new Array();

var rotateTwitter="";
var rotateArticles="";
var rotateEyeCandy="";

function setIntervalXMLAnimationTime(){
    //refreshEyeCandy = window.setInterval ( "rotateEyeCandy()", 6000 );
    //refreshTwitter = window.setInterval ( "rotateTwitter()", 5000 );
    //refreshArticles = window.setInterval ( "rotateArticles()", 5000 );
}
function clearIntervalXMLAnimationTime(){
    //clearInterval(refreshYoutube);
    //clearInterval(refreshTwitter);
    //clearInterval(refreshFlickr);
}

function getXMLFile(getXMLFeed){
	$(filenames).each(function(f,files){
	    if (getXMLFeed == files.type || getXMLFeed == "all" ){
		    // Load XML from a file on the web server
    	    $.ajax({
                type: "GET",
	            url: files.file+files.fnum+"/"+files.profile,
	            dataType: "xml",
	            success: function(xml) {
                    var root = xml.documentElement;
		            XMLContentLoader(files.type,root);
	            }
            });
        }
	});//if filenames
}//getXMLFile

function ErrorCodeMsg(errorXML){
	alert("an errror occured with "+errorXML)
}

function XMLContentLoader(feed,feedNode){
	switch (feed){
		case "youtubeplaylist":
			XMLYoutubePlaylistLoader(feedNode)
		break;
		case "sixaparttwitter":
			XMLTwitterLoader(feedNode)
		break;
		case "sixapartarticles":
			XMLArticlesLoader(feedNode)
		break;
		case "flickr":
			XMLFlickrLoader(feedNode)
		break;
		case "reviews":
			XMLReviewsLoader(feedNode)
		break;
		default : alert("add feed infomation to var filenames and create a function call");
	}	
}//XMLContentLoader
var youtubedone = false;
var flickrdone = false;
function XMLYoutubePlaylistLoader(feedNode){
	getRootChid =  $(feedNode).children().get(0).tagName;
	var y=0;
	$(feedNode).find(getRootChid).each(function(r,rootNodes){
	    var youtube_title = removeCDATA($(rootNodes).find("youtube_title").text());
	    var youtube_link = removeCDATA($.trim($(rootNodes).find("youtube_link").text()));
	    var youtube_thumbnail_url = removeCDATA($.trim($(rootNodes).find("youtube_thumbnail_url").text()));
	    var nextIdx = y+1;
        if (nextIdx >= $(rootNodes).children()) {
            nextIdx = 0;
        }
        y++;
        var maxTitleLength = youtube_title.length > 15 ? 15 : youtube_title.length;
	    var html = "<h3 title='Youtube'>Youtube</h3>";
        html += "<a href=\"javascript:goNav('" + youtube_link + "');\" onclick=\"setOmniValues(this,'e','YouTubeFlickrModule','','','',0,'','" + youtube_title.substr(0,maxTitleLength) + "','');\"><img width='120' height='90' src='" + youtube_thumbnail_url + "' alt='' /></a>";
        html += "<p class='title' title='" + youtube_title.substr(0,maxTitleLength) + "'><a href=\"javascript:goNav('" + youtube_link + "');\" onclick=\"setOmniValues(this,'e','YouTubeFlickrModule','','','',0,'','" + youtube_title.substr(0,maxTitleLength) + "','');\">" + youtube_title.substr(0,maxTitleLength) + "</a></p>";
        html += "<p class='refresh' title='Refresh'><a href='javascript:refreshEyeCandy("+nextIdx+");' onclick=\"setOmniValues(this,'o','SocialModuleRefresh','','','',0,'','Clicked','');\">Refresh</a></p>";
	    eyeCandyHTML.push(html);
	});
	youtubedone = true; 
	if (flickrdone) {
	    fisherYates(eyeCandyHTML);
	}
}//XMLYoutubeLoader
function XMLTwitterLoader(feedNode){
	getRootChid =  $(feedNode).children().get(0).tagName;
	twitterText = new Array();
	$(feedNode).find(getRootChid).each(function(r,rootNodes){
	    var twitter_text = removeCDATA($(rootNodes).find("twitter_text").text().replace(/&apos;/, '\''));
	    var twitter_link = removeCDATA($.trim($(rootNodes).find("twitter_link").text()));
	    var twitter_user = removeCDATA($.trim($(rootNodes).find("twitter_user").text()));
	    //var html = "<a href=\"javascript:goNav('" + twitter_link + "');\" onclick=\"setOmniValues(this,'e','SocialModuleTwitter','','','',0,'','Clicked','');\">" + twitter_user + "</a>";
        //output
        //html += "<a href=\"javascript:goNav('" + twitter_link + "');\" onclick=\"setOmniValues(this,'e','SocialModuleTwitter','','','',0,'','Clicked','');\">" + twitter_text + "</a>";
	    //var html = "<a href=\"javascript:goNav('" + twitter_link + "');\" onclick=\"setOmniValues(this,'e','SocialModuleTwitter','','','',0,'','Clicked','');\">" + twitter_text + "</a>";
	    var html = "<a href='" + twitter_link + "' target='_blank' onclick=\"setOmniValues(this,'e','SocialModuleTwitter','','','',0,'','Clicked','');\">" + twitter_text + "</a>";
	    twitterText.push(html);
	}); 
	//refreshTwitter = window.setInterval ( "createTwitterBoxes()", 16000 );
}//XMLTwitterLoader
function XMLArticlesLoader(feedNode){
	getRootChid =  $(feedNode).children().get(0).tagName;
	articleText = new Array();
	$(feedNode).find(getRootChid).each(function(r,rootNodes){
	    var article_title = removeCDATA($(rootNodes).find("article_title").text());
	    var article_text = removeCDATA($(rootNodes).find("article_text").text());
	    var article_url = removeCDATA($.trim($(rootNodes).find("article_url").text()));
	    var html = "<a href=\"javascript:goNav('" + article_url + "');\">";
	    //var full_article_text = article_title + " " + article_text;
        var full_article_text = article_title;
        full_article_text = full_article_text.replace(/<img[^>]*\/>/,"");
        full_article_text = full_article_text.replace(/<p[^>]*\/>/,"");
        full_article_text = full_article_text.replace(/<\/p[^>]*>/,"");
        full_article_text = full_article_text.replace("&apos;", "'");
        //trim length
        var maxLength = 110;
        if (full_article_text.Length < maxLength) {
            maxLength = full_article_text.Length;
        }
        full_article_text = full_article_text.substr(0, maxLength);
        if (maxLength == 110) {
            full_article_text+="...";
        }
        //output
        html += full_article_text + "</a>";
	    articleText.push(html);
	});
}//XMLArticlesLoader
function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj.replace(/refreshEyeCandy\(.*\)/,"refreshEyeCandy("+j+")");
     myArray[j] = tempi;
  }
  for (x=0; x<myArray.length; x++) {
    if (x<myArray.length-1) {
      myArray[x] = myArray[x].replace(/refreshEyeCandy\(.*\)/,"refreshEyeCandy("+(x+1)+")");
    }
    else {
      myArray[x] = myArray[x].replace(/refreshEyeCandy\(.*\)/,"refreshEyeCandy(0)");
    }
  }
}
function XMLFlickrLoader(feedNode){ 
	//console.log("XMLFlickrLoader"); 
	getRootChid =  $(feedNode).children().get(0).tagName;
	var y=0;
	$(feedNode).find(getRootChid).each(function(r,rootNodes){ 
	    var flickr_image_title = removeCDATA($(rootNodes).find("flickr_image_title").text());
	    var flickr_link = removeCDATA($.trim($(rootNodes).find("flickr_link").text()));
	    var flickr_image_url = removeCDATA($.trim($(rootNodes).find("flickr_image_url").text()));
	    var nextIdx = y+1;
        if (nextIdx >= $(rootNodes).children()) {
            nextIdx = 0;
        }
        y++;
        var maxTitleLength = flickr_image_title.length > 15 ? 15 : flickr_image_title.length;
	    var html = "<h3 title='Youtube'>Youtube</h3>";
        html += "<a href=\"javascript:goNav('" + flickr_link + "');\"><img width='120' height='90' src='" + flickr_image_url + "' alt='' /></a>";
        html += "<p class='title' title='" + flickr_image_title.substr(0,maxTitleLength) + "'><a href=\"javascript:goNav('" + flickr_link + "');\">" + flickr_image_title.substr(0,maxTitleLength) + "</a></p>";
        html += "<p class='refresh' title='Refresh'><a href='javascript:refreshEyeCandy("+nextIdx+");' onclick=\"setOmniValues(this,'o','SocialModuleRefresh','','','',0,'','Clicked','');\">Refresh</a></p>";
	    eyeCandyHTML.push(html);
	}); 
	flickrdone = true; 
	if (youtubedone) {
	    fisherYates(eyeCandyHTML);
	}
}//XMLFlickrLoader
function XMLReviewsLoader(feedNode){ 
	getRootChid =  $(feedNode).children().get(0).tagName;
	reviewsTitles = new Array();
    reviewsScores = new Array();
    reviewsURLs = new Array();
	$(feedNode).find(getRootChid).each(function(r,rootNodes){
	    var reviews_title = removeCDATA($.trim($(rootNodes).find("reviews_title").text()));
	    reviewsTitles.push(reviews_title);
	    var reviews_url = removeCDATA($.trim($(rootNodes).find("reviews_url").text()));
	    reviewsURLs.push(reviews_url);
        var reviews_text = removeCDATA($.trim($(rootNodes).find("reviews_text").text()));
        var score = matchMe("Score: ", reviews_text);
        reviewsScores.push(score);
        replaceReviews();
	});
}//XMLReviewsLoader
function removeCDATA(string) {
    string = string.replace("<![CDATA[","");
    string = string.replace("]]>","");
    return string;
}
function matchMe(pattern, string) {
  var re = new RegExp(pattern);
  var m = re.exec(string);
  if (m == null) {
    return "";
  } else {
    var s = m.index + pattern.length;
    var retVal = string.substr(s,3);
    return retVal;
  }
}

$(document).ready(function(){
    setTimeout('getXMLFile("all");',3000);
}); //main - xml document
