var headerSlideshow = {
	timeout:	6000,
	speed:		1000,
	
	play: function() {
		var obj = $("#togglePausePlayBtn");
		obj.removeClass("paused");
		obj.addClass("playing");
		obj.attr("title","Click to pause");
		$("#sectionWrapper").cycle("resume");
	},
	
	pause: function() {
		var obj = $("#togglePausePlayBtn");
		obj.removeClass("playing");
		obj.addClass("paused");
		obj.attr("title","Click to play");
		$("#sectionWrapper").cycle("pause");
	},
	
	togglePlayPause: function() {
		if($("#togglePausePlayBtn").is(".paused")) {
			headerSlideshow.play();
		} else {
			headerSlideshow.pause();
		}
	},
	
	getUrlParams: function(url, cast) {
		var after_type_cast = {};
		var before_type_cast = {};
		var q = url.replace(/^[^\?]*\?/,'').replace(/\&$/,'').split('&');
		for( var i = q.length - 1; i >= 0; i-- ) {
			var p = q[i].split('='), key = p[0], val = p[1];
			before_type_cast[key] = val;
			// convert floats
			if(/^[0-9.]+$/.test(val))
					val = parseFloat(val);
			// convert booleans
			if(/^(true|false)$/.test(val))
					val = (val == 'true');
			// ingnore empty values
			if(val)
					after_type_cast[key] = val;
			
		} 
		return cast === false ? before_type_cast : after_type_cast; 
	},
	
	loadDialogWindow: function() {
		headerSlideshow.pause();
		var url = $(".info a",this).eq(0).attr("href");
		url = !url ? $(this).attr("href") : url;
		//var urlParams = headerSlideshow.getUrlParams(url);
		var params = url.substring(url.indexOf("?")+1).replace(/&/g,",");
		window.open(url,'Matco', params);
		
		/*
		var announceDialog = $("#AnnouncementDialog");
		
		if(announceDialog.length < 1) {
			announceDialog = $("<div></div>").attr("id","AnnouncementDialog").appendTo("body");
			announceDialog.dialog({
				autoOpen:false, 
				resizable:false, 
				width:urlParams.width, 
				height:urlParams.height, 
				draggable:false, 
				modal:true,
				overlay:{
					backgroundColor:"#000",
					opacity:"0.7"
				}
			});
		}
		announceDialog.html("").css({position:"relative",zIndex:"10000"});
		$("#placeHolder_TBC").hide();
		
		$("<iframe></iframe>").attr("src",url)
							  .attr("width",urlParams.width)
							  .attr("height",urlParams.height)
							  .attr("frameBorder","0")
							  .attr("scrolling","no")
							  .appendTo(announceDialog);
		announceDialog.dialog("open");*/
		return false;
	},
	
	openNewWindow: function() {
		headerSlideshow.pause();
		window.open($(".info a",this).eq(0).attr("href"));
		return false;
	}
}

$(function(){//document is ready
	$("#advertisements .videoWrapper").mousedown(headerSlideshow.pause)
									  .mouseup(headerSlideshow.pause);
	$("#togglePausePlayBtn").click(headerSlideshow.togglePlayPause);
	
	
	var startIndex = 0;
	var nav = $("#advertisements .nav").eq(0);
	$("#advertisements .section").each(function(index){
		$(this).data("index",index);
		if($(this).is(".visible")){
			startIndex = index;
		}
	});
	
	//filter fade for IE6
	if(jQuery.browser.msie && jQuery.browser.version == 6.0) {
		//for IE6, don't 'fade' because it can't handle it...
		headerSlideshow.speed = 1;
	}
	
	$("#advertisements .new-window").click(headerSlideshow.openNewWindow);
	$("#advertisements .new-window-dialog").click(headerSlideshow.loadDialogWindow);
	$("#advertisements .new-window-dialog .info a").click(headerSlideshow.loadDialogWindow);
	
	
	/*more info at: http://malsup.com/jquery/cycle/*/
	$("#sectionWrapper").cycle({
		fx:				'fade',
		speed:			headerSlideshow.speed,
		timeout:		headerSlideshow.timeout,
		pager:			'#advertisements .nav',
		pagerAnchorBuilder: function(index, slide) {
			return $('#advertisements .nav a').eq(index);
		},
		pagerClick: function(index, slide) {
			if($(slide).css("display") != "none") {
				$(slide).data("isActive",true);
			}
			headerSlideshow.pause();
		},
		before: function(index, slide) {
			if(!$(slide).data("isActive")) {
				$("#advertisements .videoWrapper").hide();
			}
			$(slide).data("isActive",false);
			return false;
		},
		after: function(index, slide) {
			$(".videoWrapper", slide).show();
		},
		startingSlide:	startIndex, 
		slideExpr:		'.section'
	});
});


