function setConfig(configObject) {
	config = configObject;
	
	//get ol list and children
    var ol = document.body.getElementsByTagName('ol')[0];
    scenes = ol.children;

	currentSceneIndex = -1;
}

function start() {	
	goToScene(0);
}

function goToSceneID(id) {
	for (var i=0; i < config.length; i++) {
		if (config[i].id === id) {
			goToScene(i);
			return;
		}
	}
}

//function to go directly to any scene
function goToScene(index) {
    
    //go to scene
    startScene(index);

	//set up timer if jumping on done
	if (config[index].jump !== -1) {
		applyTimeout(config[index].jump, config[index].duration);
	}
	
}

function startScene(index) {
    
	//restart current scene without flicker
	if (index === currentSceneIndex) {
		scenes[index].setAttribute('class','run restart');
		setTimeout(function(){
	        scenes[index].setAttribute('class','run');
			},0);
		return;
	}

    //add the class "run" to the scene currently running and remove it from anybody else
    var scene;
    for (var i=0; i < scenes.length; i++) {
        scene = scenes[i];
        if (i === index) {
            scene.setAttribute('class','run');
        } else {
            scene.setAttribute('class','');
        }
        
    }

	currentSceneIndex = index;
}

//set timeout
function applyTimeout(index,time) {
    setTimeout(function(){
       //alert(time)
       goToScene(index);
    },time);
}

	
function hideURLbar()
{
	window.scrollTo(-100,1);
}

function updateOrientation()  
{  
    $('.AN-sObj-stage').css({'height' :  $(window).height() - 40});
	hideURLbar();
}

function replayScene()
{
 	$("video")[0].player.play();
}

var swfObjectStatus = false;

function flashStatus(e)
{

	swfObjectStatus = e.success;
	nonFlashContent();
}

function initContent()
{
	//swfobject.registerObject("polarbear_flash", "10.0.0", "expressInstall.swf", flashStatus);

	if(navigator.userAgent.match(/BlackBerry/i)){
		$('#mobileContent').show();
		$('.vjs-no-video').show();
	}
	else if((navigator.userAgent.match(/Windows Phone/i)) || (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i))){ 
			$('#mobileContent').show();
			VideoJS.setupAllWhenReady({
				controlsHiding: true
			});
			loadHomeVideo();		
	}
	else{
		swfobject.embedSWF("assets/swf/polarbear_intro.swf", "mobileContent", "100%", "100%", "10.0.0", false, flashvars, params, attributes,flashStatus); 
		VideoJS.setupAllWhenReady({
			controlsHiding: true
		});
		loadHomeVideo();
	}
}

function nonFlashContent()
{
//	alert(swfObjectStatus);
	
		$('#mobileContent').show();

	// VideoJS.setupAllWhenReady({
	// 	controlsHiding: true
	// });
	// loadHomeVideo();

	// if((swfObjectStatus == false) && ($.browser.webkit))
	// {
	// 	$('.AN-sObj-stage').css({'height' :  1000});
	// 	hideURLbar();
	// 	
	// 	$('.AN-sObj-stage').css({'height' :  $(window).height() - 40});
	// 	setConfig([{"jump":1,"duration":999999,"id":0}]);
	// 	start();
	// }
	// else
	// {
	// 	
	// }
}

function shareFB()
{
	window.open("http://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.arctichome.com&t=Protect%20the%20Polar%20Bear", "SocialShare", "width=520,height=420,status=no,resizable=no,scrollbars=no");
}

function shareTwitter()
{
	window.open("https://twitter.com/share?url=http%3A%2F%2Fwww.ArcticHome.com&text=This holiday season, the more we text, the more we can protect the polar bear. Learn more on November 1st at", "SocialShare", "width=520,height=420,status=no,resizable=no,scrollbars=no");
}


var loadHomeVideo = function() {

	var video = $('.video-holder');
	var videoElement = $('.video-holder video');
	var uri = $('.video-holder video source').attr('src');
	
	video.show();
	
	video.bind('click', function(){
			console.log('play')
			var tags = { 'uri': uri, 'ti': 'HomePageVideoStart', 'cgn': 'MobileArcticHome', 'cgs': '' };
			webtrends.callTracking(tags);
	});

	videoElement.bind('ended', function(){
		//send WT params for video ended
		var tags = { 'uri': uri, 'ti': 'HomePageVideoEnd', 'cgn': 'MobileArcticHome', 'cgs': '' };
		webtrends.callTracking(tags);
	});

};
var flashvars = {}; 
var params = {}; 
	params.menu = "true"; 
	params.quality = "high"; 
	params.quality = "high" ;
	params.bgcolor = "#ffffff" ;
	params.play = "true" ;
	params.loop = "true" ;
	params.wmode = "window" ;
	params.scale = "noscale" ;
	params.menu = "true" ;
	params.devicefont = "false" ;
	params.salign = "" ;
	params.allowScriptAccess = "sameDomain" ;
	
	
var attributes = {}; 			
var webtrends = {
  //Returns an object of webtrends tags - element is the DOM element that is to be attached to the click event
  getTags: function(element) {
	  
    var el = $(element);
    
    var uri = this.getDcsUri(el);
    var ti;
    var cgn ;
	var cgs;
	if (!el.attr('data-ti')) {
		ti = "";
	} else {
	    ti = el.attr('data-ti').replace(/\s/g, '%20'); 
	}
	if (!el.attr('data-cgn')) {
		cgn = "";
	} else {
	    cgn = el.attr('data-cgn').replace(/\s/g, '%20'); 
	}
	if (!el.attr('data-cgs')) {
		cgs = "";
	} else {
	    cgs = el.attr('data-cgs').replace(/\s/g, '%20'); 
	}
     
    var tags = { 'uri': uri, 'ti': ti, 'cgn': cgn, 'cgs': cgs };
    return tags;
  },
  
  //Takes an object of tags as a param and assembles a string
  //tagObj should include uri, ti, cgs, cgn
  callTracking: function(tagObj) {
    dcsMultiTrack('DCS.dcsuri', tagObj.uri, 'WT.ti', tagObj.ti, 'WT.cg_n', tagObj.cgn, 'WT.cg_s', tagObj.cgs );
  },
  
  getDcsUri: function(element) {
      var url = $(element).attr('href');
      if (url == null || url == '#') {
          url = '#' + $(element).attr('data-wturi');
      }
      return url;
  }

};

		
function tagCall(sContentGroup,sSubContentGroup,sWebClick) {
    dcsMultiTrack('WT.ti', sWebClick, 'WT.cg_n', sContentGroup, 'WT.cg_s', sSubContentGroup);
}

$(document).ready(function(){
	initContent();
	$("a.share-social").live("click", function (e)
	{
    	e.preventDefault();
    	window.open(this.href, "SocialShare", "width=520,height=420,status=no,resizable=no,scrollbars=no");
	});
	
	
  //Testing out some webtrends tracking - will move this to somewhere else
  $('.webtrends').bind('click', function(event){
    var obj = webtrends.getTags(this);
    webtrends.callTracking(obj);

  });
	
});
