 var selectedTab;  


profile_id_received = false;
try_counter=0;
redirection=0;


    function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
    }
     
    function ltrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }
     
    function rtrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    }
 
        
        menuLinkID = 'Home';
        
 
     $(document).ready(function(){

     	documentInitialHeight = $(document).height();
     	
        $('a').focus( function() {
            $(this).blur();
        });
        
        $("dd.acc_box_content").hide();
        
        $("dt.acc_box a").click(function(){
            $("dd.acc_box_content:visible").slideUp("slow");
            $(this).parent().next(":not(:visible)").slideDown("slow");
            return false;
        });

        $("#headeer").click(function(){
           var sl = (document.getElementById('test').value).substring(document.getElementById('test').selectionStart, document.getElementById('test').selectionEnd);  
           alert(sl);
        });

        $(":text,:password,textarea").not("#searchBox").focus(function() {
            $(this).css("background-color","#EFFFF4");
        });
        $(":text,:password,textarea").not("#searchBox").blur(function() {
            $(this).css("background-color","white");
        }); 
        $(":radio,:checkbox").css('border', 'none');
        
       if($('#header').css('visibility')=='hidden')
        	$('#navContainer').fadeTo("fast", 0.3);
        	
        /*if($('#homefooter').css('visibility')=='hidden')
        	$('#homefooter').fadeTo("fast", 0.1);*/

		showActiveTab('{/literal}{$selectedTab}{literal}');
		if ( $.browser.msie ) {
                if(Math.ceil($.browser.version)==6)
                {	
			pngFixForIE();
		}
		}
		resetFooter();
    });
    
	
	function SendEmail()
	{
		document.location.href = "/bubble/{$bubbleId}/sendlink/";

	}
	var defaultMessage = '';
	//FB.init("124909924192043", "/xd_receiver.htm");

    // for showing nav on home page
    function showNav(){
        $('#header').css('visibility','visible');
        $('#mynav').css('visibility','visible');  
        $('#homefooter').css('visibility','visible');
        $('#navContainer').fadeTo(2000, 1);
        //$('#homefooter').fadeTo(2000, 1);
    }

    // for showing page from flash
    function showPage(callPage){
		switch(callPage){
			case "create":
				LinkSubmit("/bubble/a/create",'createbubble');
				break;
	
			case "inspire":
				LinkSubmit("/index/a/withus",'gift');
				break;
	
			case "gift":
				LinkSubmit("/index/a/protect",'gift');
				break;
	
			case "thought":
				LinkSubmit("/index/a/withlovedone",'gift');
				break;
	
			case "success":			
				//LinkSubmit("/bubble/9175/success",'createbubble');
				break;
		}

    }
    
    function showSuccess(id, singleType)
    {
        if(singleType=='internal')
            LinkSubmit("/bubble/"+id+"/success",'createbubble');
        else
        if(singleType=='ecard')
            LinkSubmit("/bubble/"+id+"/sent",'createbubble');
    }

    // close page on X
    function closePage(){
    	if(window.document.nspcc!=undefined){
        	window.document.nspcc.SetVariable("enterKey", "true"); 
    	}
    	
    }

    // send nav action to flash
    function sendNavAction(navVal){
    	if(window.document.nspcc!=undefined){
        	window.document.nspcc.SetVariable("navSection", navVal);  
    	}
    	
    }

    // view single bubble
    function viewBubble(xmlPath){
    	if(window.document.nspcc!=undefined) {
	        window.document.nspcc.SetVariable("singleXML", xmlPath);
    	}
	        document.getElementById('content').innerHTML = '';
//**//        document.getElementById('homefooter').className = 'home-footer';
    }

	// Sound Action
    function soundAction(actionVal){
		if(actionVal == "ON"){
			if(window.document.nspcc!=undefined) {
				window.document.nspcc.SetVariable("soundStatus", "on");
			}
			document.getElementById('sound_on').className = 'active';
			document.getElementById('sound_off').className = '';
		}
		else if(actionVal == "OFF"){
			if(window.document.nspcc!=undefined) {
				window.document.nspcc.SetVariable("soundStatus", "off");
			}
			document.getElementById('sound_off').className = 'active';
			document.getElementById('sound_on').className = '';
		}
        
    }
        
    function MM_swapImgRestore() { //v3.0
    	if(document.MM_sr==undefined) return;
      	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) {
      	if(menuLinkID!=x.id)
      	{
      		x.src=x.oSrc;
      	}
      }
    }   
    
    function MM_SelectedImgRestore() { //v3.0
      a = new Array('Home','what a gift can do','find out more','create your bubble','see other bubbles','watch our ad');
      j = new Array('/img/home-hov.png','/img/what-hov.png','/img/find-hov.png','/img/create-hov.png','/img/see-hov.png','/img/watch-hov.png');
      document.MM_sr=new Array;
      for(i=0; i<a.length; i++)
      {
      	if ((x=MM_findObj(a[i]))!=null){ 
       	document.MM_sr[i]=x; if(!x.oSrc) x.oSrc=x.src; x.src=j[i];}
	    MM_swapImgRestore();
      }
    }
    
    function MM_preloadImages() { //v3.0
      var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
        for(i=0; i<a.length; i++)
        {
        	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i]; }}
	    }
    }

    function MM_findObj(n, d) { //v4.01
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }

    function MM_swapImage() { //v3.0
      var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
       if ((x=MM_findObj(a[i]))!=null){ 
       //document.getElementById('abc').innerHTML += x + a[i+2] + "<br/>";
       document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
    }
    
    
   /////////////////////////////////////
  function fbs_click() {
u="http://www.whatwillweleave.org.uk";
t=document.title;
window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}

      function twt_click() {
        
     u="http://www.whatwillweleave.org.uk";

	tweet_text= encodeURIComponent("What will you leave children? Your smile? Your protection? Share your thoughts at the new #NSPCC website www.whatwillweleave.org.uk");

      window.open('http://twitter.com/home?status='+tweet_text,'sharer','toolbar=0,status=0,width='+eval($(window).width()*0.85)+',height='+eval($(window).height()*0.85));
        //window.open('http://twitter.com/home?status='+tweet_text,'sharer','toolbar=0,status=0,width=626,height=436');
        return false;
      }
      
      ////////////////////////////
      $(document).ready(function(){
          $('#search2').focus(function(){
             if(/^\s*Enter name\s*$/.test(this.value))
             {
                 this.value = '';
             }
           });
          $('#search2').blur(function(){
             if(/^\s*$/.test(this.value))
             {
                 this.value = 'Enter name';
             }
           });
     });
     
     function SendGA_Link(aTag)
     {
        //var reg = /^.+\/(.+)$/;
        //var url = aTag.href.match(reg)[1];
     
        var pageTracker = _gat._getTracker("UA-10061512-36");
        pageTracker._trackPageview(aTag.href);
     }
     
     function LinkSubmit(url,navSection)
     {
     
        //document.getElementById('HTML_AJAX_Loading').style.display= 'block';  
        AjaxRequestGet(url+"?ajax=yes", "content");
        if(navSection=='homeOver'||navSection=='home'|| navSection=='seebubbles') 
        {

            ShowHome();
            
        }
        else
        {
            showNav();
        }
              
        switch(navSection)
        {
        	case 'home' :
            case 'homeOver':
		        menuLinkID = 'Home';
                navSection = 'home';
        	break;
        	case 'gift' :
            case 'giftOver':
		        menuLinkID = 'what a gift can do';
                navSection = 'gift';
        	break;
        	case 'more' :
            case 'findOver':
		        menuLinkID = 'find out more';
                navSection = 'more';
        	break;
        	case 'createbubble' :
            case 'createBubbleOver':
		        menuLinkID = 'create your bubble';
                navSection = 'createbubble';
        	break;
        	case 'seebubbles' :
		        menuLinkID = 'see other bubbles';
                navSection = 'seebubbles';
        	break;
        	case 'ad' :
            case 'watchAdOver':
		        menuLinkID = 'watch our ad';
                navSection = 'ad';
        	break;
        	default:
		        menuLinkID = 'Home';
                navSection = 'home';
        }
        MM_SelectedImgRestore();
         
    	sendNavAction(navSection);
     }
     
     function resetFooter(){
		a = 110;
		
		$('#homefooter').css('top','');
		$('#flashdiv').css('height','100%');
		$(document).height(documentInitialHeight);
		
		if($('#content').height() > 1 && documentInitialHeight*0.9 < $('#content').height()+a)
		{
			$('#homefooter').css('top',$('#content').height()+a);
			$('#flashdiv').height($(document).height());
		}
		$('#flashdiv').height($(document).height());
     }
     
     function showActiveTab(navSection)
     {     	
        switch(navSection)
        {
        	case 'gift' :
        	case 'giftOver' :
		        menuLinkID = 'what a gift can do';
		        MM_swapImage(menuLinkID,'','/img/what-hov.png',1)
        	break;
        	case 'more' :
        	case 'findOver' :
		        menuLinkID = 'find out more';
		        MM_swapImage(menuLinkID,'','/img/find-hov.png',1)
        	break;
        	case 'createbubble' :
        	case 'createBubbleOver':
		        menuLinkID = 'create your bubble';
		        MM_swapImage(menuLinkID,'','/img/create-hov.png',1)
        	break;
        	case 'seebubbles' :
		        menuLinkID = 'see other bubbles';
		        MM_swapImage(menuLinkID,'','/img/see-hov.png',1)
        	break;
        	case 'ad' :
        	case 'watchAdOver':
		        menuLinkID = 'watch our ad';
		        MM_swapImage(menuLinkID,'','/img/ad-hov.png',1)
        	break;
        	case 'home' :
        	case 'homeOver' :
        	default:
		        menuLinkID = 'Home';
		        MM_swapImage(menuLinkID,'','/img/home-hov.png',1)
        }
        MM_SelectedImgRestore();
     }
     
     
    function SubmitForm(ajax, falshUplaoder, submit)
    {
	
		if(ajax=='')
		{
			 ajax= 'ajax';
		}                                               
    
        //submit.disabled = "disabled";
		var ajax = document.createElement('input');
        ajax.type = 'hidden'; 
        ajax.value = 'yes';
        ajax.name = 'ajax';
        document.myform.appendChild(ajax);
        //document.getElementById('HTML_AJAX_Loading').style.display= 'block';
        AjaxRequestFormPost(document.myform,function(result){
            GetAjaxResult(result,"content");
            if(submit)
				submit.disabled = true;
		    if(falshUplaoder==1)
		    {
				//flashUploader();
		    }

        });
        
    }
    

   function getLoggedInUser() { 
		var id = FB.Connect.get_loggedInUser(); 
		return id;

	    } 

    function ShowHome()
    {
        $('#content').html('');
        
      
        MM_swapImage('Home','','/img/home-hov.png',1)
     
        
        menuLinkID = 'Home';
        MM_SelectedImgRestore();
        
        resetFooter();
        if(window.document.nspcc!=undefined) {
	        window.document.nspcc.SetVariable("skipIntro", "true");
	        window.document.nspcc.SetVariable("enterKey", "true");
        }
        
        sendNavAction('home');
    }


function update_user_box() { 
	var user_box = document.getElementById("user"); 
	// add in some XFBML. note that we set useyou=false so it doesn't display "you" 
	user_box.innerHTML = "<span id='facebookUserId'>" 
		+ "<fb:profile-pic uid=loggedinuser facebook-logo=true></fb:profile-pic>" 
		+ "<fb:name uid=loggedinuser useyou=false></fb:name>." 
		+ "</span>"; 
	// because this is XFBML, we need to tell Facebook to re-process the document 
		if(!profile_id_received)
		{
		
			FB.XFBML.Host.parseDomTree(); 
			
		}
} 

 
function getLoggedInUser() { 
	return  FB.Connect.get_loggedInUser(); 


} 

function urlCheck(str) {
var v = new RegExp();
v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
if (!v.test(str)) {
return false;
}
return true;
}              

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
        output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function searchBubbles()
{
    var keywords = URLEncode($("#searchBox").val());
    
    var path = "/bubble/"+keywords+"/searchxml";
    
    //alert(path);
    window.document.nspcc.SetVariable("searchXML", path);
    
}

function xmlLoaded(){}

