var currentIndex = 0;

var urlObj = {};
	urlObj.promoXML = "/data/creativity.xml";

var playerVersion = String("9.0.0");
var playerPath = String("/media/swf/DemoVideo.swf");
var expInstallPath = String("/ScriptLibrary/swfobject/expressInstall.swf");
var videoPath = String("../video/A51-Reel.flv");
var videoPathMobile = String("/media/video/A51-Reel-Full_Mobile.mp4");

$(document).ready(function() {
	// check for flash version
	if (swfobject.hasFlashPlayerVersion(playerVersion)) {
		// check for mobile device (Android w/Flash)
		if (checkForMobile()) {
			initMobileVideo();
		} else {
			initVideo();
		}
	} else {
		// check for mobile device (iPhone, iPad, iPod & Android wo/Flash)
		if (checkForMobile()) {
			initMobileVideo();
		} else {
			initPromos();
		}
	}
	
	$("#promo .icon-info").live("mouseover", function(evt) {
		expandInfo($(this));
	});
	
	/*$("#promo .icon-arrow").live("click", function(evt) {
		collapseInfo();
	});*/
	
});



function initVideo() {
	var flashvars = {
		videoPath: videoPath
	};
	var params = {
		menu: "false", 
		wmode: "opaque", 
		scale: "noscale", 
		bgcolor: "#000000"
	};
	var attributes = {};
	
	swfobject.embedSWF(playerPath, "video-player", "100%", "100%", playerVersion, expInstallPath, flashvars, params, attributes);
}


function initMobileVideo() {
	$("#video-player").append("<a href=\""+videoPathMobile+"\" style=\"display:inline-block;text-align:center;width:100%;height:600px;overflow:hidden;background:url('/media/images/a51-poster.jpg') no-repeat center center;\"></a>");
}


function initPromos() {
	$.ajax({
		url: urlObj.promoXML, 
		type: "GET", 
		dataType: "xml", 
		success: function(xml) {
			var clientCount = $(xml).find("client").length;
			
			$(xml).find("client").each(function(idx) {
				var slug = $(this).attr("slug");
				var client = $(this).find("clientName").text();
				var clientShort = $(this).find("clientShortName").text();
				
				$(this).find("campaign").each(function(idx) {
					var str = "";
					var featured = $(this).attr("featured");
					
					if (featured == "true") {
						var campaign = $(this).attr("id");
						var title = $(this).find("name").text();
						client = $(this).find("featuredPromo").find("title").text();
						var desc = $(this).find("featuredPromo").find("text").text();
						var img = $(this).find("featuredPromo").attr("src");
						
						str += "<div class=\"promo-item\" id=\"item-"+idx+"\">\n";
						str += "<div class=\"info-bar\" id=\"ibar-"+idx+"\">\n";
						str += "<span class=\"icon-info\"><a href=\"javascript:void(0);\"></a></span>\n";
						str += "<span class=\"icon-arrow\"><a href=\"/creativity/campaign-detail.php?client="+slug+"&campaign="+campaign+"\"></a></span>\n";
						str += "<span class=\"info\">\n";
						str += "<span class=\"title\">"+client+"</span>\n";
						str += "<span class=\"desc\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td valign=\"middle\" height=\"80\">"+desc+"</td></tr></table></span>\n";
						str += "</span>\n";
						str += "</div>\n";
						str += "<div><a href=\"/creativity/campaign-detail.php?client="+slug+"&campaign="+campaign+"\"><img src=\""+img+"\" width=\"1399\" height=\"600\" alt=\"\" border=\"0\" /></a></div>\n";
						str += "</div>\n";
					}
					
					$(".promo-slides").append(str);
				});
				
				if (idx == (clientCount-1)) {
					initSlideshow();
				}
				
			});				

		}
		
	});
	
			
}

function initSlideshow() {
	// hide video, show aliseshow
	$("#video").slideUp(300, function() {
		$(this).remove();
		$("#promo").fadeIn(300);
	});
	
	$.fn.cycle.updateActivePagerLink = function(pager, idx) {
		$(pager).find("li").find("a").removeClass("current");
		$(pager).find("li:eq("+idx+")").find("a").addClass("current");
		currentIndex = idx;
	}
	
	$("#promo .promo-slides").cycle({
		fx: "fade", 
		speed: 1000, 
		delay: 2000, 
		timeout: 6000, 
		before: function(currSlideEl, nextSlideEl, opts, flag) {
			collapseInfo();
		},
		pager: "#promo .promo-nav ul",
		activePagerClass: "current", 
		pagerAnchorBuilder: function(idx, slide) {
			//return "#promo .promo-nav ul li:eq("+idx+") a";
			return "<li><a href=\"javascript:void(0);\" rel=\""+idx+"\"></a></li>";
		}
	});

}

function expandInfo(t) {
	// pause slideshow
	//$("#promo .promo-slides").cycle("pause");
	// animate info bar
	$(t).stop(true, true).animate({
		width: "0px"
	}, 250, "easeInOutQuad", function() {
		$(this).parent().find(".icon-arrow").animate({
			width: "80px"
		}, 250, "easeInOutQuad", function() {
			$(this).parent().find(".info").animate({
				width: "900px"
			}, 500, "easeInOutQuad");
		});
	});
}

function collapseInfo() {
	// animate info bar
	$(".icon-arrow").stop(true, true).animate({
		width: "0px"
	}, 125, "easeInOutQuad", function() {
		$(this).parent().find(".info").animate({
			width: "0px"
		}, 300, "easeInOutQuad", function() {
			$(this).parent().find(".icon-info").animate({
				width: "80px"
			}, 250, "easeInOutQuad");
			// resume slideshow
			//$("#promo .promo-slides").cycle("resume");
		});
	});
}


function endVideo() {
	initPromos();
}


function checkForMobile() {
	// check user agent string for iPhone, iPod, iPad & Android
	if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/Android/i))) {
		return true;
	} else {
		return false;
	}
}


