// Crikey JS

	$(document).ready(function(){		
		
		$(".searchText").bind("blur", inputBlur);
		$(".searchText").bind("focus", inputFocus);
		
		//Initialize the font sizer for the site.
		var options = { min: -1, max: 2};
		$(".textSize").css({display: "inline"});
		$.FontSizer.Init(options);
		
		// init addComment Panel
		$(".btnAddComment").show();
		$(".addCommentText").keyup( function() {updateCharsLeft()} );
		//$(".submitCommentButton").click( function() {
			//processCommentComplete();
			//$(this).attr({disabled: "true"}).val("Adding Comment...");
			//} );

		// change edition
		$(".home//.calendarButton").css({display: "inline"});
		$(".calendarButton").toggle(function(){
			$("ul.calendarList").slideToggle("fast");
		},function(){
			$("ul.calendarList").slideToggle("fast");
		});
		
		// transparent buttons
		$(".transparentHover//img").mouseover(function(){$(this).css({opacity: ".7"})});
		$(".transparentHover//img").mouseout(function(){$(this).css({opacity: "1"})});
		
		$(".loginCancel").click( function() { 
			$("#content").css({opacity: "1"});
			$(".leftCol").css({opacity: "1"});
			$(".loginPanel").hide();
		});
		
		// search Tabs
		
		$(".searchPanel").tabs();
		$(".searchPanel//h5").hide();
		$(".searchPanel//.loading").hide();
				
		
		//Subscribe Page 
		
		// as the part 1 next button should also response to the GIFT subscription,
		// this function should not be bound to the click event by the jquery.
		// it goes to the traditional way.
		//Part 1 next button
		/*$("#nxtStep01").click(function() {
            movetoPage2();     
	    });*/
    	
    	//Part 2 next button
	    $("#nxtStep02,#part3").click(function() {	 

	     if (Page_ClientValidate('vgAccount') == true)
            {
                //Cookies are used here to enable page refresh to come back to part 3 when u chg payment mode
               //$.cookie('CurrentStep', 'Step03');             
                movetoPage3();            
            }    
	    });
	    
	    //Part 2 prev button
	   	$("#prevStep02,#part1").click(function() {
            movetoPage1();     
	    });
    	
    	//Part 3 prev button
	    $("#prevStep03a,#prevStep03b,#part2").click(function() {
            movetoPage2();        
	    });

      if($("#CurrentStep").val() == "Step03") {
    	    movetoPage3();  
	  }  	
	});
	
	
	function movetoPage1() {
	        $("#CurrentStep").val("Step01");
	        
		    $("#Step01").removeClass();		
            $("#Step01").addClass("subFormShow");   
              
            $("#Step02").removeClass();		
            $("#Step02").addClass("subFormHide");            
              
            $("#Step03").removeClass();		
            $("#Step03").addClass("subFormHide");
	}
	
	
    function movetoPage2() {
            $("#CurrentStep").val("Step02");
            
	        $("#Step02").removeClass();		
            $("#Step02").addClass("subFormShow");
            
            $("#Step01").removeClass();		
            $("#Step01").addClass("subFormHide");   
            
            $("#Step03").removeClass();		
            $("#Step03").addClass("subFormHide");
	}		
	
	
	function movetoPage3() {
	//To ensure you dont go directly to part3 without completing part2
//	 if (Page_ClientValidate('vgAccount') == true)
//        {
            $("#CurrentStep").val("Step03");
            
	       $("#Step03").removeClass();		
           $("#Step03").addClass("subFormShow");
           	       
           $("#Step01").removeClass();		
           $("#Step01").addClass("subFormHide");             
           
           $("#Step02").removeClass();		
           $("#Step02").addClass("subFormHide");       
//       }
//       else
//       {
//            $.cookie('CurrentStep',null);
//           if (window.Page_Validators)
//             for (var vI = 0; vI < Page_Validators.length; vI++)
//             {
//                var vValidator = Page_Validators[vI];
//                vValidator.isvalid = true;
//                ValidatorUpdateDisplay(vValidator);
//             }
//            movetoPage1();
//       }
	}	
	
// date select 

	function clickDateSelect() {
		$("ul.calendarList").slideToggle( function() {
			// put call to ajax here
		});
	}	
	
// animate search bar 	

	function inputFocus() {
		$(this).animate({width: 200}, 300);
	}	
	
	function inputBlur() {
		if ($(this).val() == "") {
			$(this).animate({width: 100}, 150);
		}
	}
	
// animate comments panel

	function openCommentsPanel() {
		$(".btnAddComment").hide(); 
		$(".addComment").slideDown('normal', function (){
			$(".commentUser").focus();
		});
		if ($("body").attr("class") == "memberMode") {
			updateCharsLeft();
		}
		else {
			$(".commentLogin").click(function(){
				$('body').ScrollTo(500, 'easeout');
				loginGreyout();
				return false;
			});		
		}
		
		
	};
	
	function processCommentComplete() {
		$(".addComment").slideUp(function(){
			var dt = "<dt><span>Test</span>Today</dt>";
			var dd = "<dd>"+$(".addCommentText").val()+"</dd>";
			$(".commentsModule//dl").prepend(dt+dd)
			$(".commentsModule//dt:eq(0)").hide();
			$(".commentsModule//dd:eq(0)").hide();
			$(".commentsModule//dt:eq(0)").fadeIn();
			$(".commentsModule//dd:eq(0)").fadeIn();
		});
	}
	
	function updateCharsLeft() {
		var field = $(".addCommentText").val();
		var maxlimit = 2000;
				
		if (field.length > maxlimit) { // if too long...trim it!
			field = field.substring(0, maxlimit);
			$(".addCommentText").val(field);
		}
		// otherwise, update characterCount span
		else {
		$(".characterCount//span").empty();
		$(".characterCount//span").append(maxlimit - field.length);
		}
	}
	
	function closeCommentsPanel() {
		$(".addCommentText").val(''); 
		$(".addComment").slideUp('fast', function (){
			$(".btnAddComment").show();
		});
	}
	
// back to top link

	function backToTop() {
		$('body').ScrollTo(1000, 'easeout');
	}
	
// open login panel and grey out screen

	function loginGreyout() {
		$(".loginPanel").show();
		$("#content").css({opacity: ".2"});
		$(".leftCol").css({opacity: ".2"});
		$(".loginPanel//input:eq(0)").focus();
	}
	
	//To retain scroll bar position across page loads
	
     function SaveScrollXY() {       
         Delete_Cookie("scrollXPos");
         Delete_Cookie("scrollYPos");
         
         var cookieXValue =  document.documentElement.scrollLeft;            
         var cookieYValue =   document.documentElement.scrollTop;
         
         var today = new Date();
         var expire = new Date();
         
         //cookie expires in 3 min
         expire.setTime(today.getTime() + 1000 * 60 * 3);
         
         Set_Cookie("scrollXPos", cookieXValue, expire);
         Set_Cookie("scrollYPos", cookieYValue, expire);
	}
	
      function RefreshPage(t) {
           if(t) 
              refresh = setTimeout("document.location='" + window.location + "';", t*1000*60);
    }
	
	  //scroll back to previous scrollbar position after page reload
	function ResetScrollPosition() {	      	         
		    window.scrollTo(Get_Cookie("scrollXPos"), Get_Cookie("scrollYPos"));
		    RefreshPage(3);
    }
    
	function Get_Cookie( check_name ) {
        // first we'll split this cookie up into name/value pairs
        var a_all_cookies = document.cookie.split( ';' );
        var a_temp_cookie = '';
        var cookie_name = '';
        var cookie_value = '';
        var b_cookie_found = false; // set boolean t/f default f
    	
        for ( i = 0; i < a_all_cookies.length; i++ )
        {
	        // now we'll split apart each name=value pair
	        a_temp_cookie = a_all_cookies[i].split( '=' );
    		
    		
	        // and trim left/right whitespace while we're at it
	        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    	
	        // if the extracted name matches passed check_name
	        if ( cookie_name == check_name )
	        {
		        b_cookie_found = true;
		        // we need to handle case where cookie has no value but exists (no = sign, that is):
		        if ( a_temp_cookie.length > 1 )
		        {
			        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
		        }
		        // in cases where cookie is initialized but no value, null is returned
		        return cookie_value;
		        break;
	        }
	        a_temp_cookie = null;
	        cookie_name = '';
        }
        if ( !b_cookie_found )
        {
	        return null;
        }
    }	
    
    function Set_Cookie( cookie_name, cookie_value, expire ) {        
         document.cookie = cookie_name+"="+escape(cookie_value)
                         + ";expires="+expire.toGMTString();              
     }	
    
   // this deletes the cookie when called
    function Delete_Cookie( name, path, domain ) {
        if ( Get_Cookie( name ) ) document.cookie = name + "=" +
        ( ( path ) ? ";path=" + path : "") +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
	
	
//	
////Check if all controls within the validation group is valid, 
//// if valid move to next page on subscription form
//function checkValid( valGroup,  nxtPage)
//{
//  
//    var currentPage = document.location.href;
//    var urlWithoutAnchor = currentPage.split('#');

//     if (Page_ClientValidate(valGroup) == true)
//    {
//      alert('checkValid');
//        window.location = urlWithoutAnchor[0] + '#' + nxtPage;
//    } 
//}


		    
	
	