//Share Clickback Tracking
var addthis_config = {
    data_track_clickback: true
};

//Global function for links opening in new window
$("a[rel*='external']").live('click',function(){window.open(this.href);return false;});

//Twitter Widget Configuration
(function($){
	var updateTwitter = function(){
		var US = $("#twitter").hasClass("us"),
			date = US ? "August 5, 2010" : "August 12, 2010",
			link = US ? "http://www.miracletreatday.com" : "http://www.miracletreatday.com/ca/";
		var message = escape("I plan to buy a Blizzard treat from @dairyqueen on #miracletreatday "+date+" to help kids in need. "+link);
		$('.twtr-ft').find('a').eq(0).addClass('png_bg').attr({href:'http://twitter.com/home?status='+message,rel:'external'}).removeAttr('target');
	};
	
	setTimeout(updateTwitter,1500);
})(jQuery);

//Form Input
(function($){
	$(function(){
		var startVal = $('#zip').attr('placeholder');
		$('#zip')
			.bind('focus click',function(){
				$(this).val('');
			})
			.bind('blur',function(){
				if($(this).val() == '') $(this).val(startVal);
			})
			.val(startVal);
		
		$("#submit:not('.active')").live('click.disable',function(){ return false; });
		$("#submit.active").die('click.disable');
	});
})(jQuery);

//Flash player
(function($){
	$(function(){
		var domainPath = $("#USdom").length ? "" : "../",
			playVideo = function(url){
				document.getElementById('flash_content').playAssetByURL(url);
			},
			cueVideo = function(){
				document.getElementById('flash_content').cueAssetByURL("/videos/MiracleStory_Tobin.mp4");
			},
			loadPosterFrame = function(){
				document.getElementById('flash_content').loadPosterFrame("/images/posterFrame.png");
			},
			callbackFn = function(e){
				//If the callback worked
				if(e.success){
					//Append Slideshow
					appendSlideShow();
					
					//Attach the click events to play the videos
					$("li","#video-selection").find("a").bind('click',function(){
						playVideo(this.href + '.mp4');
						return false;
					});
					
				} else if(!!document.createElement('video').canPlayType) {
					$('#video').empty();
					
					//Append Slideshow
					appendSlideShow();
					
					$('#video')
					  	.prepend(
					    	$('<div id="html5player" />')
					      		.append(
					        		$("<video id='html5video' controls />")
					          			.attr({
					            			'width':'417',
					            			'height':'240'
					          			})
										.append(
											$('<source />')
												.attr({
													'src': $("li:first","#video-selection").find("a").attr('href') + '.mp4',
													'type':'video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
												})
											)
										.append(
											$('<source />')
												.attr({
													'src':$("li:first","#video-selection").find("a").attr('href') + '.ogv',
													'type':'video/ogg; codecs="theora, vorbis"'
												})
											)
									)
							
						);

					var myVideo = document.getElementById('html5video');
					
					//Attach the click events to play the videos
					//To work cross-browser, an established video element must have it's sources removed and re-appended for the load method to work.
					//Chrome will work simply by altering the src attribute of the <source> tag, but firefox will error.
					$("li","#video-selection").find("a").bind('click',function(){
						myVideo.pause();
						$(myVideo)
							.empty()
							.append(
								$('<source />')
									.attr({
										'src': this.href + ".mp4",
										'type':'video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
									})
								)
							.append(
								$('<source />')
									.attr({
										'src': this.href + ".ogv",
										'type':'video/ogg; codecs="theora, vorbis"'
									})
								);
						myVideo.load();
						myVideo.play();
						return false;
					});
				}				
			},
			appendSlideShow = function(){
				//Append the slideshow of videos
				var homePage = $("#CAIndex").length;
				var appendString = "<h3>Watch how Children's Miracle Network and Dairy Queen have helped kids!</h3>";
					appendString += '<div id="video-selection">';
					appendString += '<ul>';
					appendString += '<li><a href="/videos/MiracleStory_SpencerFINAL" title="Hear Spencer&rsquo;s Story"><img src="/images/previews/spencer2.jpg" width="99" height="56" alt="Spencer"/></a></li>';
					appendString += homePage > 0 ? '<li><a href="/videos/Gainor-CA" title="Hear Gainor&rsquo;s Story"><img src="/images/previews/gainor.jpg" width="99" height="56" alt="Gainor"/></a></li>' : '<li><a href="/videos/Gainor-CMN" title="Hear Gainor&rsquo;s Story"><img src="/images/previews/gainor.jpg" width="99" height="56" alt="Gainor"/></a></li>';
					appendString += '<li><a href="/videos/MiracleStory-ErikKnottsHD-5min" title="Hear Erik&rsquo;s Story"><img src="/images/previews/erik.jpg" width="99" height="56" alt="Erik"/></a></li>';
					appendString += '<li><a href="/videos/MiracleStory_KatieDAray" title="Hear Katie&rsquo;s Story"><img src="/images/previews/katie.jpg" width="99" height="56" alt="Katie"/></a></li>';
					appendString += '<li><a href="/videos/MiracleStory_Tobin" title="Hear Tobin&rsquo;s Story"><img src="/images/previews/tobin.jpg" width="99" height="56" alt="Tobin"/></a></li>';
					appendString += '<li><a href="/videos/MarcusMiracleStory" title="Hear Marcus&rsquo;s Story"><img src="/images/previews/marcus.jpg" width="99" height="56" alt="Marcus"/></a></li>';
					appendString += '<li><a href="/videos/MiracleStory-MilcaHD" title="Hear Milca&rsquo;s Story"><img src="/images/previews/milca.jpg" width="99" height="56" alt="Milca"/></a></li>';
					appendString += '<li><a href="/videos/MiracleStory_NateHD" title="Hear Nate&rsquo;s Story"><img src="/images/previews/nate.jpg" width="99" height="56" alt="Nate"/></a></li>';
					appendString += '</ul>';
					appendString += '<a id="prev" href="#" title="Previous">Previous</a>';
					appendString += '<a id="next" href="#" title="Next">Next</a>';
					appendString += '</div>';
				$("#video").append(appendString);
				
				//Attach next / previous event handlers for the slideshow
				var $slideshow = $("ul","#video-selection"), index = 0;
				$("#prev").bind('click',function(){
					if(index > 0) {
						$slideshow.animate({'left': '+=103px'});
						index--;
					}
					return false;
				});
				$("#next").bind('click',function(){
					if(index < 4) {
						$slideshow.animate({'left': '-=103px'});
						index++;
					}
					return false;
				});
				
				//Run PNG fix if IE6 for next / previous links
				if ($.browser.msie && $.browser.version == 6){ 
					DD_belatedPNG.fix('#prev, #next');
				}
			};
		
		var flashvars = {
			assetPath: "/videos/MiracleStory_SpencerFINAL.mp4",
			posterPath: "/images/posterFrame.png",
			videoBackgroundColorHex: "#333333",
			onInitFunctionName: "onSWFInit"
		};

		var params = {
			allowFullScreen: "true",
			allowScriptAccess: "always",
			bgcolor: "#FFFFFF"
		};
	
		var attributes = {
			id: "flash_content"
		};
		
		function onSWFInit(){
			alert("Test");
		};
		
		var randomnumber=Math.floor(Math.random()*1000000);
	
		swfobject.embedSWF("/swfs/DQMTDPlayer.swf?"+randomnumber, "player", "416", "250", "9.0.0", "", flashvars, params, attributes, callbackFn);
				
	});
})(jQuery);