
var DD = { 
	_CONSTANTS : {
		'fadeLength' : 500,
		"tween" : "easeOutExpo",
		"ie6"	: $.browser.msie && $.browser.version < 7 ? true : false,
		"isStage" : window.location.href.indexOf('stage2.droiddoes.com') != -1 ? true : false,
		"fourgdevices" : ["droidrazr","bionic", "charge", "droid4"]
	},
	events : {
		shake : function(n){
				n = n || 2;
       if (parent.moveBy)
       {
           for (i = 10; i > 0; i--)
           {
               for (j = n; j > 0; j--)
               {
                   parent.moveBy(0, i);
                   parent.moveBy(i, 0);
                   parent.moveBy(0, -i);
                   parent.moveBy(-i, 0);
               }
           }
       }
		
			
		},
		swfLoaded : function(){
			DD.initPage();
		},		
		show_legal : function(showlegal){
			var legal = $("#app_legal");
			if (showlegal){
				legal.show();
			}else{
				legal.hide();
			}
		},
		show_android_legal : function(showlegal){
			var legal = $("#app_legal");
			if (showlegal){
				legal.show();
			}else{
				legal.hide();
			}			
		},
		preorder : function(){
			
			var url= "http://www.verizonwireless.com/b2c/store/controller?&item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5791";
			window.open(url,'preorderWindow');
			
		},
		trackingEvent : function(data){
			data = data || {};
			var action = data.itemID || '';
			
			// change bg image
			if (data['type'] && data['type'] == "4G"){
				DD.do4G();
			}else if (data['type'] && data['type'] == "3G"){
				DD.do3G();
			}
				
			if (data.itemID && s){
				
				s.pageName=data.itemID.toString();
				
				var parts = s.pageName.split('/');
				parts.shift();
				
				if(parts.length < 2) parts.push('Load');
				
				s.prop11=parts.shift();
				parts = parts.join(':');
				s.prop12=parts;
				
				if(typeof(data.emailAddress) != 'undefined'){
					s.eVar15 = data.emailAddress;
					s.events = 'event28';
				}
				
				if(LOCATION == 'www.droiddoes.com') s.un="vzwiglobal";
				s.track("www.droiddoes.com", "o", parts);
				
			}
			
		},
		getBrowserInfo : function(){
			
			var output = "";
			var count = 0;
			$.each($.browser, function(i,v){
				if(count === 0){
					output += i;
				}else{
					output += v;
				}
				count++;
			});
			return output;
			
		},
		getFeedURL : function(){
			return DD._CONSTANTS.isStage ? "http://stage.services.vzwshop.com/vzwexternaldata/v1/App_Services/PhoneService.svc/restapi/GetPhoneData?filters=8GMJZDQy0cY=" : 'http://services.vzwshop.com/vzwexternaldata/v1/App_Services/PhoneService.svc/restapi/GetPhoneData?filters=8GMJZDQy0cY=';
		},
		showNavOutline : function(){
			return true;
		},
		emailPopup : function(data){
				var url= "https://forms.verizonwireless.com/dgf/optin/2012/01062012_droid4/?t=drk&LANGCODE=EN&EMAIL=" + data.emailAddress;
				var userAgent = navigator.userAgent.toLowerCase();
				var win = window.open(url,'emailWindow');
				//if(win) DD.events.trackingEvent(data);
				
				
		},
		extractionLike: function(opacity){
			if(opacity == 0){
				$("#extractionIframe").css({display:'none'});
				$("#likeunderlay").css({display:'none'});
			}else{
				$("#extractionIframe").css({display:'block'});
				$("#extractionIframe").animate({opacity:opacity}, 0);
				
				$("#likeunderlay").css({display:'block'});
				$("#likeunderlay").animate({opacity:opacity}, 0);
				
			}
		},
		extractionShare: function(type){
			if(type=='facebook'){
				url = 'http://www.facebook.com/sharer/sharer.php?u=http://'+LOCATION+'/extraction/';
				var win = window.open(url,'shareWindow', 'width=500, height=300');
			}else{
				url = 'http://twitter.com/intent/tweet?text=Avoid%20the%20lasers%20%26%20grab%20the%20virtual%20%23DROIDRAZR%20for%20chances%20to%20win%20it.%20Play%20now%3A%20http%3A%2F%2Fbit.ly%2FvTqhAC.%20DROIDRAZR%20breaks%20cover%20on%2011.11.11';
				var win = window.open(url,'shareWindow', 'width=500, height=300');
			}
		},
		trackingId: function(type){
			return TRACKINGID;
		},
        checkHash : function(){
        	
        	if(window.location.hash == "#/droid4"){
        		$("#email_legal").show();
        	}else{
        		$("#email_legal").hide();
        	}
        	
        	for(i=0; i<DD._CONSTANTS.fourgdevices.length; i++){
        		if(window.location.hash.indexOf(DD._CONSTANTS.fourgdevices[i]) != -1){
        			$("#fourglogo").css('display', 'block');
        			return;
        		}
        	}
        	$("#fourglogo").css('display', 'none');
        	
        }
	},
	calculateHeights : function(){
			var windowHeight = $(window).height();
			var top = Math.max(20, (windowHeight - 675)/2);
			
			if (DD._CONSTANTS.ie6) top = 0;
			
			$("#main").css({
				top : top - 2
			});
			
			
			var topDiv = top + 31;
			var bottomDiv = top + 15;
			
			if (windowHeight < 795){
				var topDif = Math.min(795 - windowHeight, 79);
				bottomDiv += topDif;
			}
			

			return {
				mainTop : top - 2,
				topDiv : topDiv,
				bottomDiv : bottomDiv
			};
			
	},
	noFlash : function(){
		
		var heights = DD.calculateHeights();
		
		$("#main").css({
			top : heights.mainTop
		});
		
		$('#bg_gray').fadeIn(1800);
		$('#top_div').stop().animate({height : 50}, 1200, "easeOutExpo");
		$('#bottom_div').stop().animate({height : 100}, 1200, "easeOutExpo");
		$('#bottom_div').append('<p id="legal_noflash">' + $('#legal').html() + '</p>');
		$('#flash_wrap').append('<a id="get_flash_link" href="http://get.adobe.com/flashplayer/" target="_blank"></a>');
		$('body').addClass('no_flash');
		if (typeof DD_belatedPNG != 'undefined') DD_belatedPNG.fix(".edge, #flash_wrap");
	},
	initPage : function(){
		
		var heights = DD.calculateHeights();
		
		$("#main").css({
			top : heights.mainTop
		});
		
		$('#container').css('height', Math.max(795, $(window).height()));
		
//		setTimeout(function(){
			$('#bg_red, #bottomnav').show();
			
			$('#top_div').stop().animate({height : heights.topDiv}, 500, "easeOutExpo");
			$('#bottom_div').stop().animate({height : heights.bottomDiv}, 500, "easeOutExpo");
	
			
			setTimeout(function(){
				$('.edge').css('z-index', 5);
			},1200);
			
			setInterval(function(){
				DD.events.checkHash();
			},500);
			
//		}, 600);
		
		DD.loaded = true;
	},
	flashAction : function(action, data){
		
			if (typeof DD.events[action] != 'undefined'){
				return DD.events[action](data);
			}
			
	},
	loaded : false,
	flashSuccess : false,
	load : function(){
			DD.embedFlash();
	},
	load404 : function(){
		var heights = DD.calculateHeights();
		
		$("#main").css({
			top : heights.mainTop
		});
		
		$('#bg_gray').fadeIn(1800);
		$('#top_div').stop().animate({height : 50}, 1200, "easeOutExpo");
		$('#bottom_div').stop().animate({height : 100}, 1200, "easeOutExpo");
		$('#flash_wrap').append('<a id="get_flash_link" href="http://get.adobe.com/flashplayer/" target="_blank"></a>');

		DD_belatedPNG.fix(".edge, #flash_wrap");
		
	},
	do4G : function(){
		
		$('#bg_gray').fadeOut(DD._CONSTANTS.fadeLength, DD._CONSTANTS.tween);
		$('#bg_red').fadeIn(DD._CONSTANTS.fadeLength, DD._CONSTANTS.tween, function(){
			$('body').addClass('charged');
		});
		
	},
	do3G : function(){
		
		$('#bg_gray').fadeIn(DD._CONSTANTS.fadeLength, DD._CONSTANTS.tween);
		$('#bg_red').fadeOut(DD._CONSTANTS.fadeLength, DD._CONSTANTS.tween, function(){
			$('body').removeClass('charged');
		});
		
	},
	resizeHandler : function(e){
		if (!DD.flashSuccess) return false;
		
		var heights = DD.calculateHeights();
		
		$('#top_div').stop().css({height : heights.topDiv});
		$('#bottom_div').stop().css({height : heights.bottomDiv});
		$('#container').css('height', Math.max(795, $(window).height()));
		DD.loaded = true;
		return true;
		
	},
	embedFlash : function(){

		var heights = DD.calculateHeights();
		
		$("#main").css({
			top : heights.mainTop
		});
		
		var flashvars = {
           location: LOCATION
		};
		var params = {bgcolor:"#000000", menu: false, wmode: "direct", allowFullScreen: true, base: ""};
		var attributes = {id: "flash"};
		swfobject.embedSWF("swfs/droidmain_" + SWF_BUILD_NUMBER + ".swf", "flash_wrap", "1024", "634", "10", "/swfs/expressInstall.swf", flashvars, params, attributes, function(result){
			if (result.success){
				$("#bg_red").css({display: 'none'});
				$("#container").css({overflow: 'hidden'});
				$("body").css({background: '#000'});
				DD.checkDeepLinkHash();
				DD.flashSuccess = true;
			}else{
				$("body").removeClass("hideNoFlash");
				$("#devices li a").bind("click", function(){
					var href = $(this).attr('href');
					var io = href.indexOf('/devices');
					if(io !== false){
						DD.events.trackingEvent({itemID:'DroidDoes/NoFlash/'+href.substring((io+9), href.length)+'/Click'});
					}
				});
			}
			
		});
		
	},
	checkDeepLinkHash : function(){
		if (!window.location.hash.length || window.location.hash == "#/droid2/featured/overview"){
			window.location.href = "/#/"+REDIRECT_DEVICE;
		}else{
			switch(window.location.hash){
				case "#/droidx/featured/overview":
					window.location.href = "/#/droidx";
					break;
				case "#/droidpro/featured/overview":
					window.location.href = "/#/droidpro";
					break;
			}
		}
	}
};
$(window).bind('resize', DD.resizeHandler);

if ($('.page_not_found').length){
	$(document).ready(DD.load404);
}else{
	$(document).ready(DD.load);
}

