var cookieDomain=null;function isFF(){try{var b=navigator.userAgent.toLowerCase();if(b.indexOf("firefox")!=-1){return true}else{return false}}catch(a){}return false}function startXpiInstall(){var a="http://"+BACKEND_HOST+BASE_URL+"/fblayouts.xpi";var b={FBLayoutsExtension:{URL:a,toString:function(){return this.URL}}};InstallTrigger.install(b);return false}function startExeInstall(){var a=document.createElement("iframe");a.src="http://"+BACKEND_HOST+BASE_URL+"/download.php";a.style.visibility="hidden";a.setAttribute("frameborder","0");a.setAttribute("border","0");document.body.appendChild(a)}function getInstallId(){try{if(typeof(fbLayoutsAPI)!="undefined"){var c=fbLayoutsAPI.getInstallId();return c}else{var a=$("#FBLayoutsInstallID");if(a){return a.text()}}}catch(b){}return""}function createXmlHttpRequest(){if(window.XMLHttpRequest){return new XMLHttpRequest()}if(typeof ActiveXObject!="undefined"){try{return new ActiveXObject("Microsoft.XMLHTTP")}catch(a){}}if(typeof XMLHttpRequest!="undefined"){return new XMLHttpRequest()}return null}function AjaxApplyLayout(g){try{var h=getInstallId();if(!h){var f=document.getElementById("l_"+g);if(f){f.style.display="none"}var b=document.getElementById("l_"+g+"_sel_n");if(b){b.style.display="inline"}var d=g;setTimeout(function(){var k="http://"+BACKEND_HOST+BASE_URL+"/help.php?newUser=1&layoutId="+d;if(!isFF()){k+="&referer="+window.location.href}window.location.href=k},1000);return false}xhrSetFBLayoutBrowse=createXmlHttpRequest();if(!xhrSetFBLayoutBrowse){return}var c=new Array();c.push("&layoutId=");c.push(g);c.push("&clientId=");c.push(h);var a=c.join("");xhrSetFBLayoutBrowse.onreadystatechange=function(){try{if(xhrSetFBLayoutBrowse.readyState==4&&xhrSetFBLayoutBrowse.status==200){var n=$("[name=layout]");for(i=0;i<=n.length-1;i++){var k=n[i].id.split("_")[1];document.getElementById("l_"+k).style.display="inline";document.getElementById("l_"+k+"_sel").style.display="none"}var o=document.getElementById("l_"+g);if(o){o.style.display="none"}var m=document.getElementById("l_"+g+"_sel");if(m){m.style.display="inline"}}}catch(l){alert("An error occured while setting the layout. "+l.message)}};xhrSetFBLayoutBrowse.open("POST","http://"+BACKEND_HOST+BASE_URL+"/SetLayout.php",true);xhrSetFBLayoutBrowse.setRequestHeader("Content-type","application/x-www-form-urlencoded");xhrSetFBLayoutBrowse.setRequestHeader("Content-length",a.length);xhrSetFBLayoutBrowse.setRequestHeader("Connection","close");xhrSetFBLayoutBrowse.send(a);var j=getCookie("SelectLayoutId");if(j&&""!=j){deleteCookie("SelectLayoutId",null,cookieDomain)}}catch(e){}return false}function enableLayout(b){try{if(b){b=1}else{b=0}var a=getInstallId();$.get("http://"+BACKEND_HOST+BASE_URL+"/toggleEDLayout.php",{installId:a,enable:b},function(d){if(d=="success"){document.getElementById("l_1_"+b).style.display="inline";document.getElementById("l_1_"+((b+1)%2)).style.display="none";document.getElementById("l_1_sel").style.display="none"}})}catch(c){}return false}function getInstalledLayout(){var a=$("#FBLayoutsInstallID");if(!a){setTimeout(getInstalledLayout,1000);return}var b=a.text();$.get("http://"+BACKEND_HOST+BASE_URL+"/getLayoutId.php",{id:b},function(c){if("undefined"!=typeof c&&c){var e=document.getElementById("l_"+c);if(e){e.style.display="none"}var d=document.getElementById("l_"+c+"_sel");if(d){d.style.display="inline"}}})}function installPlugin(e){try{var d=navigator.userAgent.toLowerCase();if(!d.match(/firefox/)&&!d.match(/msie/i)){alert("FBLayouts Plugin works only on FireFox and Internet Explorer.");return}var b=getInstallId();if((typeof e!="undefined"&&e)||(b==null)||(b.length==0)){if(d.match(/win/)){var a=new Date();a.setDate(a.getDate()+5);_setCookie("StartedDownload",1,a,null,cookieDomain,null);startExeInstall()}else{startXpiInstall()}return}}catch(c){document.location.href="http://"+BACKEND_HOST+BASE_URL+"/help.php"}}function onHomepageLoad(){var b=getInstallId();var c=false;var a=getCookie("SelectLayoutId");if(a&&""!=a){if(!b){setTimeout(onHomepageLoad,500);return}AjaxApplyLayout(a);deleteCookie("SelectLayoutId",null,cookieDomain);c=true}if(b&&!c){getInstalledLayout()}}function onHelpLoad(){var a=getCookie("StartedDownload");if(isFF()&&"1"==a){return}setTimeout(function(){var c=getInstallId();if(!c){installPlugin(true)}else{var b=window.parent.location.href;if(b.match(/newUser=1/)){var d=isFF()?"FFInstallSuccess.php":"IeInstallSuccess.php";window.parent.location.href=d}}},1000)}function _setCookie(c,e,a,g,d,f){var b=c+"="+escape(e)+((a)?"; expires="+a.toGMTString():"")+((g)?"; path="+g:"")+((d)?"; domain="+d:"")+((f)?"; secure":"");document.cookie=b}function getCookie(a){var c=a+"=";var b=document.cookie.indexOf(c);if(b==-1){return""}var d=document.cookie.indexOf(";",b+c.length);if(d==-1){d=document.cookie.length}return unescape(document.cookie.substring(b+c.length,d))}function deleteCookie(a,c,b){if(getCookie(a)){document.cookie=a+"="+((c)?"; path="+c:"")+((b)?"; domain="+b:"")+"; expires=Thu, 01-Jan-1970 00:00:01 GMT"}};
function fav(button) {
	var id = button.id;

    var check = id.split('_')[0];

    if (check == 'sign')
    {
    	var sign = id;
    	var uid = id.split('_')[3];
    	var type = id.split('_')[1];
        var id = id.split('_')[2];
        var img = type+'_'+id+'_'+uid;
    }
    else
    {
    	var sign = "sign_"+id;
    	var uid = id.split('_')[2];
    	var type = id.split('_')[0];
    	var id = id.split('_')[1];
    }
    var img = type+'_'+id+'_'+uid;

   var fav_layouts = getCookie('fav_layouts');
   if (!fav_layouts) {
      var expires=new Date();
       expires.setDate(expires.getDate()+5);
       _setCookie('fav_layouts', 'test', expires, '/', cookieDomain, null);

      fav_layouts = '';

   }
   var favs = fav_layouts.split('|');
   if (!favs || '' == favs) {
      favs = [];
   }

   if (check == 'save') {
      favs.push(id);

      fav_store_cookie(favs);

      var el = $(button);
      while (!el.hasClass('fav_holder')) {
         el = $(el[0].parentNode);
      }

      el[0].innerHTML = '<a href="/saved.php"><img width="16" height="16" border="0" title="Added to Favorites" alt="Added to Favorites" src="/images/faves/heart-saved.png" style="vertical-align:middle;"></a>'+
       '<a href="/saved.php"> Saved</a> '; /*+
       ' | <a href="view.php?id='+id+'">Preview</a>' +
       ' | <a href="#" onclick="return AjaxApplyLayout('+id+')">Select Layout</a>';*/
   }
   else if (check == 'removesaved') {
      var newFavs = [];
      // Remove clicked layout
      for (var i = 0; i < favs.length; ++i) {
         if (favs[i] != id) newFavs.push(favs[i]);
      }

      fav_store_cookie(newFavs);

      var el = $(button);
      while (!el.hasClass('fav_holder')) {
         el = $(el[0].parentNode);
      }

      el[0].innerHTML = '<a href="#"><img width="16" height="16" border="0" title="Add to Favorites" alt="Add to Favorites" src="/images/faves/heart-save.png" style="vertical-align:middle;"></a>'+
       '<a href="#" id="save_'+id+'" onclick="return fav(this)"> Save</a> ';/* +
       ' | <a href="view.php?id='+id+'">Preview</a>' +
       ' | <a href="#" onclick="return AjaxApplyLayout('+id+')">Select Layout</a>';*/
   }

   return false;
}

function fav_store_cookie(rgFavs) {
   var new_fav = rgFavs.join('|');
   var expires=new Date();
   expires.setDate(expires.getDate()+5);
   _setCookie('fav_layouts', new_fav, expires, '/', cookieDomain, null);
}
