
$(document).ready(function(){
	// redirect?
	PicksRedirect.init();
	PicksShare.init();
	initializeFlashApp();
	
	if(picksCookie.firstVisit()){ // first visit
		GetActionTag("http://view.atdmt.com/jaction/astpps_Introdemonewusers_1");
		$("#demoVideoBtn").click();
	}else{ // return visit
		GetActionTag("http://view.atdmt.com/jaction/astpps_Pickslandingpagereturninguser_1")
	}
	
});

var PicksShare = {
	currentPick:{
		title:"",
		id:"",
		pickShareUrl:"http://www.priceless.com/us/personal/en/picks/30/index.html?id=",
		pickShareShortUrl:"http://www.priceless.com/picks.html?id="
	},
	init:function(){
		// event handler for sharing Picks Appp
		$("#sharePicksAppBtn").click(function(){ PicksShare.sharePicksApp(); return false; });
		
		// event handler for faq overlay
		$("#faqOverlayBtn").click(function(){ PicksShare.showFaq(); return false; });
		
		// event handler for demo video button
		$("#demoVideoBtn").click(function(){ PicksShare.showDemoVideo(); return false; });
		
	},
	sharePicksApp:function(){
		// clear out any pre-existing values
		this.currentPick.id = "";
		this.currentPick.title = "";
		$.Modal({ File:"share-app.html", TransitionType:"fade" });
	},
	showFaq:function(){
		$.Modal({File:"faq.html",TransitionType:"fade"});
	},
	showDemoVideo:function(){
		$.Modal({File:"demo-video-overlay.html",TransitionType:"fade"});
	},
	shareSpecificPick:function(pickId,picktitle){
		// pass info to 'currentPick':
		this.currentPick.id = pickId;
		this.currentPick.title = picktitle;
		$.Modal({ File:"share-pick.html", TransitionType:"fade" });
	},
	setSocialPlatformLinks:function(){
		$("#social_icons > a").each(function(i){
			var regExTitle = /@pickTitle@/g;
			var regExUrl = /@url@/g;
			var h = $(this).attr("href"); // get href
			h=h.replace(regExTitle, escape(PicksShare.currentPick.title)); // update title
			
			// twitter?
			if(this.id=="shareSocial_Twitter"){
				h=h.replace(regExUrl, PicksShare.currentPick.pickShareShortUrl + PicksShare.currentPick.id); // update url
			}else{
				h=h.replace(regExUrl, PicksShare.currentPick.pickShareUrl + PicksShare.currentPick.id); // update url
			}
			$(this).attr("href",h); // set href
			$(this).attr("name",PicksShare.currentPick.id); // set name for omniture
		});

	}
}

// example:
// PicksShare.shareSpecificPick("12345","foo bar");


function initializeFlashApp() {
    
    var state;
    var latitude;
    var longitude;
    var city;
    
    if (google.loader.ClientLocation != null)
    { 
        state = google.loader.ClientLocation.address.region.toUpperCase();
	    latitude = google.loader.ClientLocation.latitude;
	    longitude = google.loader.ClientLocation.longitude;
	    city = google.loader.ClientLocation.address.city;
    }
    else
    {
        state = "";
        latitude = "";
        longitude = "";
        city = "";
    }
   
       
    
		
	var flashVars = {
		embedProfiler:true,
		as_swf_name: "flashContent",
		//googlemapskey: "ABQIAAAA11TkeNM2e-KyyEFf7dvgrRTJfnxF13wRS1gLmbvgVFqaFzGO-hTk0MA6yJ1kBZrZy4bj_DmH08NKkA",
		//googlemapskey:"ABQIAAAA11TkeNM2e-KyyEFf7dvgrRSPLCNam2cZWnJ0KIn6HjeVb5bfwBSbNob3S9ZhJ5Z8rTOXuTf0btLA8g",
		googlemapskey: key,
		assets: "swf/",
		images:"img/",
		model: "xml/Picks3d.xml",
		xmlpath:"xml/",
		//pickID:315705,
		state:state,
		city:city,
		latitude:latitude,
		longitude:longitude
	};
	//add client id if on priceless domains

	if (IsPricelessDomain) { flashVars.clientID = client + "&sensor=false"; }

	// check url for param 'id'
	var u = document.location.toString();
	if(u.indexOf("?id=")>0){
		// param exisits
		var pId = u.substr(u.indexOf("?id=")+4);
		pId = parseInt(pId);
		if(pId+"" != "NaN"){flashVars.pickID = pId;}
    }
	
	

	//create your instance of your .swf
	var params = {
		wmode: "window",
		allowFullScreen : true,
		allowScriptAccess: "always"
	};
	
	var attributes = {
		id: "flash_flash"
	}
	
	swfobject.embedSWF("swf/picks3d.swf", "flashContent", "951", "600", "10.0.0", "expressInstall.swf", flashVars, params, attributes);
	
	//var map=document.getElementById("flashContent");
	//map.setCurrentLocation(latitude,longitude,city,state);
	

}


function hideflash(){ $('#flash_flash').css({ left: '-5000px' }); }
function showflash(){$('#flash_flash').css({ left: '0' });}


function GetActionTag(url){
	document.getElementById("action_tag").src = url;
}


var showLocationFaq = false;

function showFaqLocationDetection(){
	showLocationFaq = true;
	$("#faqOverlayBtn").click();
}


var picksCookie = {
	cookieName:"hereBefore",
	firstVisit:function(){
		var c = this.readCookie(this.cookieName);
		if(!c){
			this.createCookie(this.cookieName,"true");
			return true;
		}else{
			return false;
		}
	},
	createCookie:function(name,value){
		var daysBeforeExpiration = 365;
		var date = new Date();
		date.setTime(date.getTime()+(daysBeforeExpiration*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie:function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++){
			var c = ca[i];
			while(c.charAt(0)==' ') c = c.substring(1,c.length);
			if(c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
};


var PicksRedirect = {
	init:function(){
		var mobileDevice = this.isMobileDevice();
		if(!mobileDevice){
			return;
		}else if(mobileDevice == "iPhone"){
			this.iPhone();
		}else{
			this.unsupportedMobile();
		}
	},
	iPhone:function(){
		var urlPick = "/mobile/iphone-share-pick.html?id=";
		var urlApp = "/mobile/iphone-share-app.html";
		var pId = this.getId();
		if(!pId){
			document.location = urlApp;
		}else{
			document.location = urlPick + pId;
		}
	},
	unsupportedMobile:function(){
		var url = "/mobile/unsupported-device.html";
		document.location = url;
	},
	getId:function(){ // check url for param 'id'
		var u = document.location.toString();
		if(u.indexOf("?id=")>0){ // param exisits
			var pId = u.substr(u.indexOf("?id=")+4);
			pId = parseInt(pId);
			if(pId+"" != "NaN"){return pId;}else{return false;}
		}else{ // param does not exist
			return false;
		}
	},
	isMobileDevice:function(){
		var returnValue = false;
		var userAgentNames = new Array("iPod", "iPhone", "BlackBerry", "Motorola", "Nokia", "SAMSUNG-SGH", "SonyEricsson", "LG-", "LG/" );
		if(navigator && navigator.userAgent){ var uaName = navigator.userAgent; for(var i=0; i<userAgentNames.length; i++){ if(uaName.indexOf(userAgentNames[i]) > -1){ returnValue = userAgentNames[i]; } }
		}else{ return false; }
		return returnValue;
	}
}


function pause(s) {
     var videoPlayer = document.getElementById(s);
               try{
                    videoPlayer.stopVideo();
                     //alert('stopVideo');
                }
                catch(e){
          
                // Flash not loaded yet, or doesn't exist
                console.log(e);
                }
          }  
