// # REGEXPS definition
SPACES_REGEXP = /^\s+$/;

var _gaq = _gaq || [];
  if (location.host == "www.glamoo.com") {
	  _gaq.push(['_setAccount', 'UA-25342340-3']);
  } else { //test environments
	  _gaq.push(['_setAccount', 'UA-27255693-1']); //test accounts
  }
  _gaq.push(['_setDomainName', '.glamoo.com']);
  _gaq.push(['_setSiteSpeedSampleRate', 30]); //performance tuning
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

//window.onload = function(){
$(document).ready(function() {
	
	var height = window.screen.height;
    var width = window.screen.width;
    var ratio_desktop = width / height;

    if (ratio_desktop > 1.25) {
        altezzaReale = 670;
        altezza = 800;
    }
    else {
        altezzaReale = 814;
        altezza = 1024;
    }
	
    if($("input[id$='immageBackGround']").get(0)!=null){
    	var imgBackGround = $("input[id$='immageBackGround']").get(0).value;
		var path = $("input[id$='pathAllImages']").get(0).value;
		var preNameImg = $("input[id$='preNameImageBkgBig']").get(0).value;
		//  Initialize Backgound Stretcher
		$(document).bgStretcher({
				images: [path + preNameImg + altezza + imgBackGround],
				imageWidth: 1254, 
				imageHeight: altezzaReale,
				slideShow:false
		});
    }
	    
	$("#btn_menu_1").attr("class", "btn_menu_2_calibri");
    $("#btn_menu_2").attr("class", "btn_menu_2_calibri");
    $("#btn_menu_3").attr("class", "btn_menu_2_calibri");
    $("#btn_menu_4").attr("class", "btn_menu_2_calibri");
    $("#btn_menu_5").attr("class", "btn_menu_2_calibri");
    $("#arrow").attr("class", "arrow_calibri");
    $("#bar_background_bottom").attr("class", "bar_background_bottom_calibri");
	
    window.fbAsyncInit = function() {
        FB.init({ appId: '121871634529617', status: true, cookie: true, xfbml: true });
     // Hack to fix http://bugs.developers.facebook.net/show_bug.cgi?id=20168 for IE7/8/9
        FB.UIServer.setLoadedNode = function (a, b) {     FB.UIServer._loadedNodes[a.id] = b; };
    };
    (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        var linguaSito = $("input[id$='linguaSito']");
    	if(linguaSito != null && linguaSito.length>0)
    	if (linguaSito.get(0).value =='it') {
            e.src = document.location.protocol + '//connect.facebook.net/it_IT/all.js';
        }
        else {
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        }
        e.async = true;
        $('#fb-root').append(e);
    } ());
 
}
);

function removeinput(x) {
        x.value = '';
}  

function loginFB(){    
    FB.login(function(response) {
    	//alert("ingresso nella response");
    	  if (response.session) {
    		  //alert("ingresso alla sessione");
    	    if (response.scope) {
    	    	//alert("setto i permessi");
    	      // user is logged in and granted some permissions.
    	      // scope is a comma separated list of granted permissions
    	    } else {
    	    	//alert("non setto i permessi");
    	      // user is logged in, but did not grant any permissions
    	    }
    	  } else {
    		  //alert("sono fuori dalla sessione");
    	    // user is not logged in
    	  }
    	  //alert("esco dalla funzione login");
    	}, {scope:'email,publish_stream,offline_access,read_stream,friends_birthday,friends_photos'});
    /* All the events registered */
    FB.Event.subscribe('auth.login', function(response) {
    	var fbUidFld = document.getElementById('fb_login_form:fbUid');
    	var fbAccessTokenFld = document.getElementById('fb_login_form:fbAccessToken');
    	fbUidFld.value = response.authResponse.userID;
    	fbAccessTokenFld.value = response.authResponse.accessToken;    	
    	document.getElementById('fb_login_form:login_with_FB').click();
    });
    FB.Event.unsubscribe('auth.logout', function(response) {
        // do something with response
    	alert("ingresso nel UNsubscribe");
        logout();
    });

    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
        	var fbUidFld = document.getElementById('fb_login_form:fbUid');
        	var fbAccessTokenFld = document.getElementById('fb_login_form:fbAccessToken');
        	fbUidFld.value = response.authResponse.userID;
        	fbAccessTokenFld.value = response.authResponse.accessToken;
        	document.getElementById('fb_login_form:login_with_FB').click();
        }
    });
}
function loginFBOverlay(){    
    FB.login(function(response) {
    	//alert("ingresso nella response");
    	  if (response.session) {
    		  //alert("ingresso alla sessione");
    	    if (response.scope) {
    	    	//alert("setto i permessi");
    	      // user is logged in and granted some permissions.
    	      // scope is a comma separated list of granted permissions
    	    } else {
    	    	//alert("non setto i permessi");
    	      // user is logged in, but did not grant any permissions
    	    }
    	  } else {
    		  //alert("sono fuori dalla sessione");
    	    // user is not logged in
    	  }
    	  //alert("esco dalla funzione login");
    	}, {scope:'email,publish_stream,offline_access,read_stream,friends_birthday,friends_photos'});
    /* All the events registered */
    FB.Event.subscribe('auth.login', function(response) {   	
    	$("input[id$='overlayFbUid']").val(response.authResponse.userID);
    	$("input[id$='overlayFbAccessToken']").val(response.authResponse.accessToken);    	
    	$("input[id$='login_with_FB_overlay']").click();
    });
    FB.Event.unsubscribe('auth.logout', function(response) {
        // do something with response
    	alert("ingresso nel UNsubscribe");
        logout();
    });

    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
        	$("input[id$='overlayFbUid']").val(response.authResponse.userID);
        	$("input[id$='overlayFbAccessToken']").val(response.authResponse.accessToken);
            // logged in and connected user, someone you know
        	$("input[id$='login_with_FB_overlay']").click();
        }
    });
}
function settaHidden(element)
{
	var id = element.id;
	if(id=='txtBirthDay')
	{
		$("input[id$='txtDay']").val(element.value);
	}else if(id=='txtBirthMonth')
	{
		$("input[id$='txtMonth']").val(element.value);
	}else if(id=='txtBirthYear')
	{
		$("input[id$='txtYear']").val(element.value);
	}else if(id=='cmbCities')
	{
		$("input[id$='txtCity']").val(element.value);
	}
	else if(id=='select_city')
	{
		$("input[id$='txtCity']").val(element.value);
	}
}

function settaHiddenBuyPage(id_hidden)
{
	var id = id_hidden.id;
	if(id=='expire_month_buy')
	{
		$("input[id$='txtExpDateMonth']").val(element.value);
	}else if(id=='expire_year_buy')
	{
		$("input[id$='txtExpDateYear']").val(element.value);
	}
}

function updateRegType(selected){
	$("input[id$='valueSelected']").val(selected);
	if(selected == "friend"){
		$("#additional_info_code").hide("fast");
		$("#additional_info_friend").show("fast");
	}
	else 
		if(selected == "code"){
			$("#additional_info_friend").hide("fast");
			$("#additional_info_code").show("fast"); 
		}
		else if(selected == "myself"){
			$("#additional_info_friend").hide("fast");
			$("#additional_info_code").hide("fast");
		} else{
			$("#additional_info_friend").hide("fast");
			$("#additional_info_code").hide("fast");
		}
}

//Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
  var vars = [], 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('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
  }
  return vars;
}

function fromMail() {
	var email = getUrlVars()["email"];
	var emailCookie = $.cookie('glamoo_email');
	if (emailCookie == 'true') {
		return true;
	} else {
		if (email && email.length > 0) {
			$.cookie('glamoo_email', 'true', { expires: 1, path: '/'});
			return true;
		}
	}
	return false;
}

/**
 * Blank checker
 * 
 * @param variable the variable value to be checked
 * @returns {Boolean}
 */
function isBlank(variable) {	
	return ( null == variable || "" == jQuery.trim(variable) || SPACES_REGEXP.test(variable) || undefined == variable || "undefined" == variable || "0" === variable || 0 === variable ) ? true : false;
}

