var flashversion = "9.0.115";
var flashplayerdivid = 'widget';

//alert(navigator.userAgent);
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
	window.location=window.location+'iphone/';
}

if(typeof(swfobject)=='object' && swfobject.hasFlashPlayerVersion(flashversion)){
	//console.log(swfobject.getFlashPlayerVersion());
	
	var head = document.getElementsByTagName('head')[0];
	if(head){
		var flashstyle = document.createElement('style');
		var css = '#'+flashplayerdivid+' p {display: none}';

		flashstyle.setAttribute('type','text/css');
		
		if(head){
			if(flashstyle.styleSheet){//cause you can't put text in the webstandards way in ie
				flashstyle.styleSheet.cssText = css;
			}else{
				flashstyle.appendChild(document.createTextNode(css));
			}
			head.appendChild(flashstyle);
		}
	}
}


function openWindow ( url, windowname, options ) {
	if ( window.open ( url, windowname, options ) ) {
		return true;
	}
	return false;
}

function getUrlVars(){
	var varis = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
	hash = hashes[i].split('=');
	varis.push(hash[0]);
	varis[hash[0]] = hash[1];
	}
	return varis;
}
var vars = getUrlVars();
//alert("section="+vars['section']+"\n"+"gallery="+vars['gallery']+"\n"+"video="+vars['video']+"\n");

$(document).ready(function(){
	resizeFlashContent();
	$('#logos li a').each(function(){
		$(this).click(function(){
			var main = document.getElementById('main');
			if(main && typeof(main.deactivate)=='function')
				main.deactivate();
			sCode.trackFeaturedContentClick($(this).attr('href'), 'district9_soundtrack_'+$(this).attr('title').toLowerCase().replace(/ /g,'_').replace(/\./g,'')+'_button');
			return false;
		});
	});
	$('#more-info a').click(function(){
		var main = document.getElementById('main');
			if(main && typeof(main.deactivate)=='function')
				main.deactivate();
			sCode.trackFeaturedContentClick($(this).attr('href'), 'district9_soundtrack_'+$(this).attr('title').toLowerCase().replace(/ /g,'_').replace(/\./g,'')+'_button');
			return false;
	});
});

$(window).resize(function(){
	resizeFlashContent();
});

function resizeFlashContent(){
	var minheight = 544;
	var headerheight = $('#sonymovieswidenav').outerHeight();
	var footerheight = $('#footer').outerHeight() + $('#d9footer').outerHeight();
	var bodyheight = $('body').outerHeight();
	var contentwidth = $('#content').outerWidth();
	var flashheight = bodyheight - headerheight - footerheight;
	if(flashheight <= minheight){
		$('#flashcontent').css({height: minheight+'px'});
	}else{
		var newflashheight = bodyheight - headerheight - footerheight;
		if($.browser.msie && $.browser.version<7){
			var ienewflashheight = newflashheight - headerheight - footerheight;
			if(ienewflashheight > minheight)
				newflashheight = ienewflashheight;
		}
		$('#flashcontent').css({height: newflashheight+'px'});
	}

	if($.browser.msie && $.browser.version<7){
		if(contentwidth <= 950){
			$('#sonymovieswidenav').css({width:'950px'});
			$('#content').css({width:'950px'});
		}else{
			$('#sonymovieswidenav').css({width:'100%'});
			$('#content').css({width:'100%'});
		}
	}
}


var flashvars = {
//	section: getQueryParamValue("section"),
//	account: "sonypicturesglobal,sonypicturesother",
//	channel: "net:movies",
//	prop3:"net:movies:terminatorsalvation",
//	prop4:"net:terminatorsalvation",
//	prop5:"net:movies:site"
	};

if (navigator.appVersion.indexOf("Mac")!=-1) { if(/Safari/.test(navigator.userAgent)) {flashvars.isFullScreenDisabled='true';} }
//if( vars['gallery'] ){flashvars.isAutoGallery=vars['gallery'];}
//if( vars['video'] ){flashvars.isAutoTrailer=vars['video'];}
if( vars['section'] ){flashvars.section=vars['section'];}



var params = {
	allowFullScreen:'true',
	allowScriptAccess:'always',
	showMenu: 'false',
	wmode: 'transparent'
	};
var attributes = {id:flashplayerdivid};

swfobject.embedSWF("http://widgets.clearspring.com/o/4a8c60d1b58caff2/4a8c967507686a7a/4a8c60d1b58caff2/6d053e65", flashplayerdivid, "300", "390", flashversion,"", flashvars, params, attributes);

share.url = "http://www.district9movie.com/soundtrack";
share.title = "District 9 Official Soundtrack Page";
share.description = "Check out the District 9 Official Soundtrack Page. Listen to tracks and share with your friends.";
share.tweet = "Check out the District 9 Official Soundtrack Page!  http://www.district9movie.com/soundtrack.";
share.delicious.description = "Check out the District 9 official soundtrack page at http://www.district9movie.com/soundtrack.  District 9 is in theaters August 14.";


function deactivate(){
	var main = document.getElementById('main');
	if(main)
		main.deactivate();
	else
		alert("main does not exist");
}

function hideFooter(){
	var d9footer = document.getElementById('d9footer');
	d9footer.style.height = '105px';
	d9footer.style.overflow = 'hidden';
	resizeFlashContent();
}

// Social bookmarking function
function sharethis() {
	var links = document.getElementById('d9footer').getElementsByTagName('a');
	for (i=0; i<links.length; i++) {
		var link = links[i];
		if( (link.getAttribute('rel') == 'facebook') || (link.getAttribute('rel') == 'myspace') || (link.getAttribute('rel') == 'twitter') || (link.getAttribute('rel') == 'delicious') || (link.getAttribute('rel') == 'digg') || (link.getAttribute('rel') == 'stumbleupon'))
		{
			link.onclick= function() {
				share.send(this.getAttribute('rel'));		
				return false;
			}
		}

	/*	
		if( (link.getAttribute('rel') == 'external') ){
			extendOnClick(link, function(evt,obj) {
				var parent = obj.parentNode;
				if (typeof s == "object" && parent.getAttribute('id')) {
					var property = s.prop4.substring(s.prop4.lastIndexOf(':') + 1);
					sCode.trackOutboundClick(obj.getAttribute('href'), property + '_' + parent.getAttribute('id') + '_link');
				}
			});
		}
	*/	
	}
	$("#9footer a[rel='external']").bind("click",function(){
		this.unbind();
		var parent = $this.parent().get(0);
				if (typeof s == "object" && parent.getAttribute('id')) {
					var property = s.prop4.substring(s.prop4.lastIndexOf(':') + 1);
					sCode.trackOutboundClick(obj.getAttribute('href'), property + '_' + parent.getAttribute('id') + '_link');
				}

	});
}

// Initializing functions on load
swfobject.addDomLoadEvent(sharethis);