//destiny branding
var initLogoScaleX;
var initLogoScaleY;
var initLogoY;
var vidreplayInitScale;
//var buttonCreated = false;
var ButtonSizeInfo;// = (destiny.whatDivIsVisible(btnName.children)).id;
//svginfo
var svgNS = "http://www.w3.org/2000/svg"; 

//b38d4b
var destiny = {	
	//Should I add an empty emergency function here so we can overide stuff?
	emergencyOverride:function(){
	/*incase anything needs to be updated - this gets called after all things loaded*/
	log("destiny.emergencyOverride");
	},
	startUp: function(whatToSay)
	{
		log(whatToSay);
	},

	showUpToRoot:function(mc) //UTILS
	{
		var target = mc;
		while(target)
		{
			target.visible = true;
			target.display = true;//displayBlock();
			target = target.parentNode;
		}
	},

	setButton:function(btnName)
	{
		log("setButton--------------------------");
		log("ButtonSizeInfo-------------------------");
		ButtonSizeInfo = (destiny.whatDivIsVisible(btnName.children)).id;
		log("--------------------");
		log(ButtonSizeInfo);
		var btnWid = parseInt($(ButtonSizeInfo).style.width.split("px")[0]);
		var btnHei = parseInt($(ButtonSizeInfo).style.height.split("px")[0]);
		log("button width and height");
		log(btnWid);	log(btnHei);
		log("scaleOfText-------------------------");
		var scaleOfText = window.getComputedStyle($(ButtonSizeInfo))["transform"].split("matrix(")[1].split(",")[0];
		log(scaleOfText);
		var btn_wid = Math.round((btnWid)*scaleOfText);	
		var btn_hei = Math.round((btnHei)*scaleOfText);

		var btn_flash = '<svg xmlns:xlink="http://www.w3.org/1999/xlink" class="do" width="'+btn_wid+'px" height="'+btn_hei+'px" ><g xmlns="http://www.w3.org/2000/svg" ><path stroke="none" fill="#ffffff" fill-opacity="1" d="M0 0 L'+btn_wid+' 0 '+btn_wid+' '+btn_hei+' 0 '+btn_hei+' 0 0"></path></g></svg>';
		var btn_hitarea = '<svg xmlns:xlink="http://www.w3.org/1999/xlink" class="do" width="'+btn_wid+'px" height="'+btn_hei+'px" ><g xmlns="http://www.w3.org/2000/svg" ><path stroke="none" fill="#99999" fill-opacity="0" d="M0 0 L'+btn_wid+' 0 '+btn_wid+' '+btn_hei+' 0 '+btn_hei+' 0 0"></path></g></svg>';
		var divstyle = "position:absolute; transform(1,0,0,1,"+(Math.round((btn_hei*0.5)-scaleOfText))+","+(Math.round((btn_wid*0.5)-scaleOfText))+")";

		var btnflash = document.createElement('div');
			btnflash.id = "btnflash";
			btnflash.style.cssText = divstyle;
			btnflash.innerHTML +=btn_flash;

		var btnhitarea = document.createElement('div');
			btnhitarea.id = "btnhitarea";
			btnhitarea.innerHTML += btn_hitarea;	
	
		$(ButtonSizeInfo).appendChild(btnflash);
		TweenMax.set(btnflash,{x:-(Math.round((btn_wid*0.5))),y:-(Math.round((btn_hei*0.5))-1)}); //btnflash
	 	$(ButtonSizeInfo).appendChild(btnhitarea);
	 	TweenMax.set(btnhitarea,{x:-(Math.round((btn_wid*0.5))),y:-(Math.round((btn_hei*0.5))-1)}); //btnhitarea	
	},

	animateButton:function(btn,functionName,wait,skipAnimation)
	{
	    log("animateButton: "+btn);
	    log(destiny.whatDivIsVisible(btn.children).children[1]);
	    var textInButton = destiny.whatDivIsVisible(btn.children).children[1];
	    var ButtonSizeInfo = (destiny.whatDivIsVisible(btn.children)).id;

	    TweenMax.set(textInButton,{alpha:0});
	   // log(ButtonSizeInfo);
	  	if(skipAnimation)
		    {
		         TweenMax.set(textInButton,{alpha:1});
		         TweenMax.set($(ButtonSizeInfo)._("#btnflash"),{alpha:0, onComplete:destiny.giveButtonAction,onCompleteParams:[btn,functionName]})		    	 
		    } 
		else 
		    {
		   	 	 TweenMax.set(textInButton,{alpha:1,delay:wait+0.25});
		   	 	 fbf.hide($(ButtonSizeInfo).children[0],$(ButtonSizeInfo)._("#btnhitarea"));
		   	 	 TweenMax.from($(ButtonSizeInfo)._("#btnflash"),0.5,{x:0,delay:wait,scaleX:0,ease: Back.easeInOut.config(1),onComplete:fbf.show,onCompleteParams:[$(ButtonSizeInfo).children[0],$(ButtonSizeInfo)._("#btnhitarea")]});
	 			 TweenMax.to(btn._("#btnflash"),0.5,{alpha:0,delay:0.5+wait, onComplete:destiny.giveButtonAction,onCompleteParams:[btn,functionName]});
		    }
	},

	giveButtonAction:function(btn,functionName)
	{
	   log("setUpButton",btn);
	   if(btn._hasClickHandler) return;
	   
	   var cta_path = btn._("#btnhitarea").lastChild._("path");
	   cta_path.mouseEnabled = true;
	   cta_path.buttonMode = true;
	   cta_path.addEventListener("mouseover", destiny.onCtaOver);
	   cta_path.addEventListener("click", function(event){
		   	event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true; ////log("main click", window.clickTag);
		    var target = event.target || event.srcElement;
		    var phase = event.eventPhase;
	   		destiny.onCtaClicked(functionName);
	   });
	   btn._hasClickHandler = true;
	},


	whatDivIsVisible:function(divToCheck)
	{
		log(divToCheck);
		for (var i = 0; i < divToCheck.length; i++) {
			var visCheck = window.getComputedStyle(divToCheck[i]);
			//log(visCheck);
			if(visCheck["visibility"] != "visible") {

			} else 
			{
				return divToCheck[i];
			}
		}
	},


	// GIVE ALL BUTTONS DIFFERENT COLOURING -------------------------------------------------------------------------------------
	//#b38d4b

	// PS4 ----------------------------------------------------------------------------------------------------------------------
	animatePs4:function(mc,wait){ TweenMax.set(mc,{alpha:1}); TweenMax.from(mc,1,{alpha:0,delay:wait}); },
	// RATING -------------------------------------------------------------------------------------------------------------------
	animateRating:function(mc,wait){ TweenMax.from(mc,1,{alpha:0,delay:wait}); },
	// DEV LOGOS ----------------------------------------------------------------------------------------------------------------
	animateDevlogos:function(mc,wait){ TweenMax.from(mc,1,{alpha:0,delay:wait}); },
	// DEV LOGOS ----------------------------------------------------------------------------------------------------------------
	animateDate:function(mc,wait){fbf.show(mc); TweenMax.from(mc,1,{alpha:0,delay:wait});},
	// DATE BOX -----------------------------------------------------------------------------------------------------------------
	setDate:function(isOut){ if(isOut) { fbf.displayNone(mc_date); } else { fbf.displayNone(mc_available);} },
	// UTILS -----------------------------------------------------------------------------------------------------------------
	unStutterAnimationFix:function() //Firefox and IE were stuttering the animation of the BG.
	{
	    for (var i = 0; i < arguments.length; i++) {
	        TweenMax.set(arguments, {
	            rotation: 0.01
	        });
	    };
	},
	// BLUR -----------------------------------------------------------------------------------------------------------------
	blurMe:function(elem, amount, t,delayBlur) 
	{
	    var blurElement = {
	        a: amount
	    };
	    //end the blur at 0
	    TweenMax.to(blurElement, t, {
	        delay: delayBlur,
	        a: 0,
	        onUpdate: function () {
	            TweenMax.set(elem, {
	                webkitFilter: "blur(" + blurElement.a + "px)",
	                filter: "blur(" + blurElement.a + "px)"
	            });
	        }
	    });
	},
	//LOGO ---------------------------------------------------------------------------------------------------------------
	animateLogo:function(mc,wait)
	{
	    log("animateLogo");
	    initLogoScaleX = mc._scaleX;
	    initLogoScaleY = mc._scaleY;
	    initLogoY = mc._y;
	    log(initLogoScaleX);
	    fbf.show(mc);
	    TweenMax.set(mc.children,{alpha:0});
	    TweenMax.to(mc._("#mc_destiny_logo"),0.5,{alpha:1,delay:wait});
	    TweenMax.from(mc,1,{delay:wait,scaleX:initLogoScaleX*1.5,scaleY:initLogoScaleX*1.5,y:"+="+(mc._height*0.5),ease: Expo.easeOut});
	    TweenMax.set(mc._("#mc_destiny_line"),{alpha:1});
	    TweenMax.from(mc._("#mc_destiny_line"),0.25,{scaleX:0,delay:0.5+wait/*,transformOrigin: "50% 50%"*/,ease: Back.easeOut.config(1.7)});  
	    TweenMax.to(mc._("#mc_terr_logo"),1,{alpha:1,delay:(1)+wait});  
	    TweenMax.to(mc._("#mc_icon"),1,{alpha:1,delay:(1.25)+wait});  
	},
	immediateLogo:function(mc) //might need this for banners that don't want fancy logo animation
	{
	    destiny.showUpToRoot(mc);
	    fbf.show(mc);
	    log("immediateLogo:");
	    log(mc.visible);
	    TweenMax.set(mc,{alpha:1,y:initLogoY});   
	    TweenMax.set(mc.children,{alpha:1}); 
	    TweenMax.set(mc._("#mc_destiny_line"),{scaleX:1});       
	    TweenMax.set(mc,{scaleX:initLogoScaleX,scaleY:initLogoScaleX});
	},
	videoWaterMark:function(logox,logoy,logoScale)
	{
	    var watermark = mc_logo.cloneNode(true);
	    mc_video_box.appendChild(watermark);
	    watermark._("#mc_icon").visible = false;
	    TweenMax.set(watermark, {x:logox,y:logoy, scaleX:logoScale, scaleY:logoScale, alpha:0.6});
	  //  logox,logoy,logoScale
	    //TweenMax.set(watermark, {y:30, scaleX:0.25, scaleY:0.25, alpha:0.6}); //scaleX:0.5, scaleY:0.5, 
	},
	
 	setUpCloseButton:function(btn,functionName)
	{
	   log("setUpButton",btn);
	   var cta_path = btn._("#mc_ctahit").lastChild._("path");
	   cta_path.mouseEnabled = true;
	   cta_path.buttonMode = true;
	 //  cta_path.addEventListener("mouseover", codiw.onCtaOver);
	   cta_path.addEventListener("click", function(event){
		   	event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true; ////log("main click", window.clickTag);
		    var target = event.target || event.srcElement;
		    var phase = event.eventPhase;
	   		destiny.onCtaClicked(functionName);
	   });
	},
	 onCtaOver:function(event) {
	    log("onCtaOver");
	    event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true; ////log("main click", window.clickTag);
	    var target = event.target || event.srcElement;
	    var phase = event.eventPhase;
	    var whatbtn = target.parentNode.parentNode.parentNode.parentNode.parentNode.id;
	    log(whatbtn);
	    destiny.btnFlash($(whatbtn));
	},
 	btnFlash:function(btn)
	{
	  TweenMax.set(btn._("#btnflash"),{alpha:0});
	  TweenMax.from(btn._("#btnflash"),0.25,{alpha:1});
	},
 	onCtaClicked:function(functionName) {
	    log("onCtaClicked");
	    functionName();
	},

	//Fancy play btn animation (like the overlay play button on destiny site) --------------------------------------------------------------------
	animateVidReplayBtn:function(thumb,listenerMC,functionToCall)
	{
		log("animateThumbnails");
		fbf.show(thumb);
		fbf.displayBlock(thumb);
		thumb.mouseEnabled = false;
		vidreplayInitScale = thumb._scaleX;
		log("-------------------------------------------"+vidreplayInitScale);
		thumb.style.display = "block";
		thumb.children[0].style.display = "inherit";
		//TweenMax.to(thumb.children[0].children[1],0,{alpha:0});
		TweenMax.to(thumb.children[0],0,{alpha:0,scaleX:1.2,scaleY:1.2}); // outer circle
		TweenMax.to(thumb.children[1],0,{alpha:0,scaleX:1.2,scaleY:1.2}); // inner circle
		TweenMax.to(thumb.children[2],0,{alpha:0,rotation:45,scaleX:1.2,scaleY:1.2}); // hex1
		TweenMax.to(thumb.children[3],0,{alpha:0,rotation:-45,scaleX:1.2,scaleY:1.2}); // hex2
		TweenMax.to(thumb.children[4],0,{alpha:0,scaleX:1.2,scaleY:1.2}); // playbtn

		TweenMax.from(thumb,0.5,{alpha:0,ease: Back.easeOut.config(1.7),scaleY:0});
		TweenMax.to(thumb.children[0],0.5,{delay:0.5,alpha:1,scaleX:1,scaleY:1,ease: Back.easeOut.config(1.7)}); // outer circle
		TweenMax.to(thumb.children[1],0.5,{delay:0.5,alpha:1,scaleX:1,scaleY:1,ease: Back.easeOut.config(1.7)}); // inner circle
		TweenMax.to(thumb.children[2],0.5,{delay:0.75,alpha:1,rotation:0,scaleX:1,scaleY:1,ease: Back.easeOut.config(1.7)}); // hex1
		TweenMax.to(thumb.children[3],0.5,{delay:0.75,alpha:1,rotation:0,scaleX:1,scaleY:1,ease: Back.easeOut.config(1.7)}); // hex2
		TweenMax.to(thumb.children[4],0.5,{delay:1,alpha:1,scaleX:1,scaleY:1,onComplete:destiny.VidReplayListeners,onCompleteParams:[thumb,listenerMC,functionToCall]}); // playbtn*/

	},

	VidReplayListeners:function(thumb,listenerMC,functionToCall)
	{
	   var cta_path = listenerMC;
	   cta_path.mouseEnabled = true;
	   cta_path.buttonMode = true;
	   cta_path.addEventListener("mouseover", function(){destiny.animateRollOverVidReplay(thumb)});
	   cta_path.addEventListener("mouseout", function(){destiny.animateRollOutVidReplay(thumb)});
	   cta_path.addEventListener("click", function(event){
		   	event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true; ////log("main click", window.clickTag);
		    var target = event.target || event.srcElement;
		    var phase = event.eventPhase;
	   		destiny.onCtaClicked(functionToCall);
	   });
	},

	animateRollOverVidReplay:function(thumb)
	{
		TweenMax.killTweensOf(thumb.children[0]);
		TweenMax.killTweensOf(thumb.children[4]);
		log(vidreplayInitScale);
		TweenMax.to(thumb,0.5,{scaleX:vidreplayInitScale*1.1,scaleY:vidreplayInitScale*1.1});
		TweenMax.to(thumb.children[0],1,{alpha:1,scaleX:1.1,scaleY:1.1,ease: Back.easeOut.config(1.7)}); // outer circle
		TweenMax.to(thumb.children[1],0.5,{delay:0.5,alpha:1,scaleX:1.1,scaleY:1.1,ease: Back.easeOut.config(1.7)}); // inner circle
		TweenMax.to(thumb.children[2],1,{alpha:1,rotation:-90,scaleX:1.1,scaleY:1.1,ease: Back.easeOut.config(1.7)}); // hex1
		TweenMax.to(thumb.children[3],1,{alpha:1,rotation:90,scaleX:1.1,scaleY:1.1,ease: Back.easeOut.config(1.7)}); // hex2
		TweenMax.to(thumb.children[4],0.5,{delay:0.5,alpha:1,scaleX:1.1,scaleY:1.1}); // playbtn
	},

	animateRollOutVidReplay:function(thumb)
	{
		TweenMax.killTweensOf(thumb.children[1]);
		TweenMax.killTweensOf(thumb.children[4]);
		log(vidreplayInitScale);
		TweenMax.to(thumb,0.5,{scaleX:vidreplayInitScale,scaleY:vidreplayInitScale});
		TweenMax.to(thumb.children[0],0.5,{delay:0.5,alpha:1,scaleX:1,scaleY:1,ease: Back.easeOut.config(1.7)}); // outer circle
		TweenMax.to(thumb.children[1],1,{alpha:1,scaleX:1,scaleY:1,ease: Back.easeOut.config(1.7)}); // inner circle
		TweenMax.to(thumb.children[2],1,{alpha:1,rotation:90,scaleX:1,scaleY:1,ease: Back.easeOut.config(1.7)}); // hex1
		TweenMax.to(thumb.children[3],1,{alpha:1,rotation:-90,scaleX:1,scaleY:1,ease: Back.easeOut.config(1.7)}); // hex2
		TweenMax.to(thumb.children[4],0.5,{delay:0.5,alpha:1,scaleX:1,scaleY:1}); // playbtn
	}


}



