if(typeof(console) == "undefined"){
	var console = {
		log : function(){}
	}
}

function onScroll() {
	var top = jQuery(window).scrollTop();
	Winter2011.update(top);
}

function onResize() {
	var coordinates = $("winter2011").getCoordinates();
	console.log("coordinates.left: "+coordinates.left);
	//var position = $("winter2011").position();
	//console.log("offset: "+position.x);
	$("fbShare").setStyle("left", coordinates.left+870);
}

var isMobile;
function initWinter2011() {
	console.log("initWinter2011");
	
	// will need to check for other mobile devices as well:
	var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
	//if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
	if (mobile) {
	   isMobile = true;
	}
	
	var ieVersion = getIEVersion();
	//console.log("ieVersion: "+ieVersion);
	//alert("ieVersion: " + ieVersion);
	if (ieVersion < 9 && ieVersion > 0) {
		
		var source = new Swiff("templates/core/video/feature.swf", {width:424, height:270});
		var vidContainer = new Element("div", {id:"mobileVideo1"});
		vidContainer.inject($("collage1"));
		source.inject(vidContainer);
		
		var source = new Swiff("templates/core/video/showx1.swf", {width:294, height:166});
		var vidContainer = new Element("div", {id:"mobileVideo2"});
		vidContainer.inject($("collage2"));
		source.inject(vidContainer);
		
		var source = new Swiff("templates/core/video/vintage80.swf", {width:282, height:187});
		var vidContainer = new Element("div", {id:"mobileVideo3"});
		vidContainer.inject($("collage3"));
		source.inject(vidContainer);
		
		var source = new Swiff("templates/core/video/vintage88.swf", {width:310, height:150});
		var vidContainer = new Element("div", {id:"mobileVideo4"});
		vidContainer.inject($("collage4"));
		source.inject(vidContainer);
	}
	
	
	// testing:
	//isMobile = true;
	
	if (!isMobile) {
		Winter2011.init();
		onScroll();
		embedVideo1();
		
		jQuery(window).bind('scroll', function() {
			onScroll();
		});
		
		jQuery(window).resize(function() {
			onResize();
		});
		
	} else {
		
		$("collectionCta").addEvent("click", function() { window.location.hash = "wall"; });
		
		$("collage1").empty();
		$("collage2").empty();
		$("collage3").empty();
		$("collage4").empty();
		
		var vidContainer = new Element("div", {id:"mobileVideo1"});
		vidContainer.inject($("collage1"));
		
		var vidContainer = new Element("div", {id:"mobileVideo2"});
		vidContainer.inject($("collage2"));
		
		var vidContainer = new Element("div", {id:"mobileVideo3"});
		vidContainer.inject($("collage3"));
		
		var vidContainer = new Element("div", {id:"mobileVideo4"});
		vidContainer.inject($("collage4"));
		
		embedVideo1();
		embedVideo2();
		embedVideo3();
		embedVideo4();
	}
	onResize();
	
	$("fbShare").addEvent("click", shareFb);
}

function embedVideo(sourceID, onCanPlay, sources, container, width, height, poster) {
	if (!container) container = $("winter2011");
	var video = new Element("video", {id:sourceID, loop:"true", autoplay:"true", controls:"true", oncanplaythrough:onCanPlay, onended:"this.play()", width:width, height:height});
	
	if (poster) video.setProperty("poster", poster);
	
	var len = sources.length;
	for (var i=0; i < len; i++) {
		var sourceObj = sources[i];
		
		if (sourceObj.fileType == "swf") {
			//var source = new Element("embed", {src:sourceObj.file, width:sourceObj.width, height:sourceObj.height});
			//var source = new Swiff(sourceObj.file, {width:sourceObj.width, height:sourceObj.height});
		} else {
			var source = new Element("source", {src:sourceObj.file, type:sourceObj.fileType});
		}
		
		source.inject(video);
	};
	
	var videoContainer = new Element("div", {"class":"videoContainer"});
	video.inject(videoContainer);
	videoContainer.inject(container);
}

var video1Embed = false;
function embedVideo1() {
	console.log("embedVideo1");
	var sources = [
		{file:"templates/core/video/feature.mp4", fileType:"video/mp4"},
		{file:"templates/core/video/feature.ogv", fileType:"video/ogg"}
	];
	var container = (isMobile) ? $("mobileVideo1") : null;
	var poster = (isMobile) ? "templates/core/images/feature-poster.jpg" : null;
	embedVideo("videoSource1", "video1Setup()", sources, container, 424, 270, poster);
	video1Embed = true;
}

var video2Embed = false;
function embedVideo2() {
	console.log("embedVideo2");
	var sources = [
		{file:"templates/core/video/showx1.mp4", fileType:"video/mp4"},
		{file:"templates/core/video/showx1.ogv", fileType:"video/ogg"}
	];
	var container = (isMobile) ? $("mobileVideo2") : null;
	var poster = (isMobile) ? "templates/core/images/poster-showx1.jpg" : null;
	embedVideo("videoSource2", "video2Setup()", sources, container, 294, 166, poster);
	video2Embed = true;
}

var video3Embed = false;
function embedVideo3() {
	console.log("embedVideo3");
	var sources = [
		{file:"templates/core/video/vintage80.mp4", fileType:"video/mp4"},
		{file:"templates/core/video/vintage80.ogv", fileType:"video/ogg"}
	];
	var container = (isMobile) ? $("mobileVideo3") : null;
	var poster = (isMobile) ? "templates/core/images/poster-vintage81.jpg" : null;
	embedVideo("videoSource3", "video3Setup()", sources, container, 282, 188, poster);
	video3Embed = true;
}

var video4Embed = false;
function embedVideo4() {
	console.log("embedVideo4");
	var sources = [
		{file:"templates/core/video/vintage88b.mp4", fileType:"video/mp4"},
		{file:"templates/core/video/vintage88b.ogv", fileType:"video/ogg"}
	];
	var container = (isMobile) ? $("mobileVideo4") : null;
	var poster = (isMobile) ? "templates/core/images/poster-vintage88.jpg" : null;
	embedVideo("videoSource4", "video4Setup()", sources, container, 310, 150, poster);
	video4Embed = true;
}

var setupVideo1 = false;
function video1Setup() {
	if (!setupVideo1) {
		setupVideo1 = true;
		console.log("video1Setup");
		if (!isMobile) {
			Winter2011.video1Setup($("videoSource1"));
			embedVideo2();
		}
	}
}

var setupVideo2 = false;
function video2Setup() {
	if (!setupVideo2) {
		setupVideo2 = true;
		console.log("video2Setup");
		if (!isMobile) {
			Winter2011.video2Setup($("videoSource2"));
			embedVideo3();
		}
	}
}

var setupVideo3 = false;
function video3Setup() {
	if (!setupVideo3) {
		setupVideo3 = true;
		console.log("video3Setup");
		if (!isMobile) {
			Winter2011.video3Setup($("videoSource3"));
			embedVideo4();
		}
	}
}

var setupVideo4 = false;
function video4Setup() {
	if (!setupVideo4) {
		setupVideo4 = true;
		console.log("video4Setup");
		if (!isMobile) {
			Winter2011.video4Setup($("videoSource4"));
		}
	}
}


window.addEvent('domready', function() {
    if (lg_lang == "es" || lg_lang == "it" || lg_lang == "fr") {
		$("collectionCta").setStyle("font-size", "11px");
		$("collectionCta").setStyle("padding-left", "15px");
		$("collectionCta").setStyle("width", "274px");
	}
	
	if (lg_lang == "ru" || lg_lang == "pt-br") {
		$("collectionCta").setStyle("font-size", "10px");
		$("collectionCta").setStyle("padding-left", "15px");
		$("collectionCta").setStyle("width", "274px");
	}
});

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function getIEVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 9.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  //alert( msg );
  return ver;
}

function headline() {
	return "Nike Vision Winter 2011";
}

function shareFb() {
	console.log("shareFb");
	
	var fb = window.open('http://www.facebook.com/sharer.php?s=100&p[url]=http://www.nikevision.com&p[title]='+headline()+'&p[images][0]=http://nikevision.com/images/social/siteThumb.jpg' ,'_fb','width=550,height=450');
	//var fb = window.open('http://www.facebook.com/sharer.php?u=http://nikevision.com&t=Nike Vision Winter 2011' ,'_fb','width=550,height=450');
	centerPopup(fb);
}

function centerPopup(_win) {
	_win.focus();
	_win.moveTo(jQuery(window).width()/2 - 275, jQuery(window).height()/2 - 225)
}



