// -----------------------------------------------------------------------------
$(document).ready(function() {

// -----------------------------------------------------------------------------
// personal cover select
// -----------------------------------------------------------------------------
   $(".personalcoverselect").change(function() {
     id = this.id;
     id = id.slice(19); 
     //alert(id);
     if(this.value == 1)
       $("#personalcover"+id).css("display","block");
     else
     {
       $("#personalcover"+id).css("display","none");
       //clear radio selections
       $("#personalcover"+id+" input").attr("checked", "");
     }
   });

   $("#personalcover table input").change(function() {
     $('#starsandprice').html('recalculating...');
     document.forms.buy.action='?step=2'; 
     document.forms.buy.submit();
   });
// -----------------------------------------------------------------------------
// info buttons
// -----------------------------------------------------------------------------
	// click
	$(".infobutton,.info,.infobutton_small").click(function(){
    infopanelSet(this.id);
    return false;
	});
// -----------------------------------------------------------------------------
// whatscovered
// -----------------------------------------------------------------------------
   $(".whatscovered").click(function() { // .whatsCoveredButton
     var offset = $("#results").offset();
     id = this.id;
//     id = id.slice(12);
     whatscoveredSet(offset,id);
     this.blur();
     return false;
   });
	//Click out event
	$("#whatscoveredpanel").click(function(){
    whatscoveredStatus = 1;
		whatscoveredSet();
    return false;
	});
	//Press Escape event
	$(document).keypress(function(e){
		if(e.keyCode==27 && whatscoveredStatus==1){
      whatscoveredStatus = 1;
			whatscoveredSet();
      return false;
		}
	});
// -----------------------------------------------------------------------------
// personalcover
// -----------------------------------------------------------------------------
   $(".personalcoverinfo").click(function() {
     id = this.id;
//     id = id.slice(12);
     personalcoverSet();
     this.blur();
     return false;
   });
// -----------------------------------------------------------------------------
// vrm lookup
// -----------------------------------------------------------------------------
   $("#vrm_lookup_button").click(function() {
      this.blur();
      var vrm = $("#vrm").val();
      if ( vrm.length < 2 ) 
      {
        alert ("Please enter your vehicle registration first");
        $("#vrm").focus();
      }
      else
      {
        $("#vrm_lookup").submit();
      }
      return false;
   });
// -----------------------------------------------------------------------------
// CL login module (right column)
// -----------------------------------------------------------------------------
   $("#cl_login_button").click(function() {
      this.blur();
      $("#rhlogin").submit();
      return false;
   });
//
/*   $("#cl_login_button").click(function() {
 
        $("rhlogin").keypress(function(e) {
            // Enter pressed?
            if(e.which == 10 || e.which == 13) {
                this.rhlogin.submit();
            }
        });
      return false;
   });*/
// -----------------------------------------------------------------------------
// back to top of page link
// -----------------------------------------------------------------------------
   if ( $("#backtotop") ) 
   {
     $("#backtotop").css("display", "none");
     if ( $("#backtotop").scrollTop() > $(window).height() )
     {
       $("#backtotop").css("display", "block");
     }
   }

// -----------------------------------------------------------------------------
// homepage fastquote background images
// -----------------------------------------------------------------------------
// Impliment a change based on future date:
		var n = '2'
		var myDate = new Date();
		myDate.setFullYear(2010,09,01); // Month starts at zero for Jan -> 11 for Dec!
		var today = new Date();
		if ( today >= myDate ) {
			n = '3';
		}
   var no = Math.floor(Math.random()*11) + 1; // returns  1 through 11
   if ( no < 10 ) no = "0" + no; //add zero prefix in file name
   $("#fast_quote_panel").css('background-image','url(/frontend/images/home'+n+'/home_'+no+'.gif)');
// -----------------------------------------------------------------------------
// homepage matrix
// -----------------------------------------------------------------------------
// NB :  need to sort out image preloading for this device...will smooth display
//   r_1 = Image(); r_1.src = "/frontend/images/home/grid/grid_r_1.gif";
//   l_1 = Image(); l_1.src = "/frontend/images/home/grid/grid_1_1_on.gif";
   
   $(".grid_l").mouseover(function() {
     no = this.id.substr(7)
     $("#grid_r").attr('src','/frontend/images/home'+n+'/grid/grid_r_'+no+'.gif');
//     $("#grid_r").attr('src',r_1.src);
     $("#grid_l_"+no).attr('src','/frontend/images/home'+n+'/grid/grid_l_'+no+'_on.gif');
//     $("#grid_l_"+no).attr('src',l_1.src);
   });
   $(".grid_l").mouseout(function() {
     no = this.id.substr(7)
     $("#grid_r").attr('src','/frontend/images/home'+n+'/grid/grid_r_off.gif');
     $("#grid_l_"+no).attr('src','/frontend/images/home'+n+'/grid/grid_l_'+no+'.gif');
   });
// -----------------------------------------------------------------------------
// debugger
// -----------------------------------------------------------------------------
  if ( DEVELOPER == 1 )
  {
  printvarscontrol = ( $.cookie("printvars") == "block" ) ? true : false;
  display = ( printvarscontrol ) ? "block": "none";
  $.cookie("printvars2", display, { path: '/' } );
  $("#printvars").css({
    "width": ( $(window).width() - $("#container").width() ),
    "height": $(window).height(), 
    "overflow": "scroll" ,
    "display": display 
    });
  if ( printvarscontrol ) $("#container").css({    "margin-left": ( $(window).width() - $("#container").width() )  });
  else $("#container").css({    "margin-left": "auto"  });
  $("#printvarscontrol,#printvars").click(function() {
    printvarscontrol = ( printvarscontrol ) ? false: true;
    display = ( printvarscontrol ) ? "block": "none";
    $.cookie("printvars", display, { path: '/' } )
    if ( printvarscontrol ) 
    {
      $("#printvars").fadeIn("slow");
      $("#container").css({    "margin-left": ( $(window).width() - $("#container").width() )  });
    }
    else 
    {
      $("#printvars").fadeOut("slow");
      $("#container").css({    "margin-left": "auto"  });
    }
    return false;
  });
  }
  $.cookie("__utma", null, { domain: 'stridebird.com' } )
  $.cookie("__utmb", null, { domain: 'stridebird.com' } )
  $.cookie("__utmc", null, { domain: 'stridebird.com' } )
  $.cookie("__utmz", null, { domain: 'stridebird.com' } )
}); // end of document ready set
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------





////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// INFO PANEL POP UP
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
// control var
var infopanelStatus = 0;

function infopanelSet(btnID){
	// remove panel
	if(infopanelStatus==1){
		$("#infopanel").fadeOut("slow");
		infopanelStatus = 0;
    return false;
	}
  // load contents into pop up
  $("#infopanel").html("loading...");
  $.get("/ajax/infopanel/?id="+btnID,
    function(data){
      $("#infopanel").html(data);
    });
  var offset = $("#"+btnID).offset();
	// data for positioning
  var windowWidth = $(window).width(); 
  var windowHeight = $(window).height(); 
  var scrollX = $(window).scrollTop();
	var popupHeight = $("#infopanel").height();
  var w =  ( btnID == "info_changepolicy" ) ? 475 : 380;
	var popupWidth  = $("#infopanel").width();
//  alert ( "windowWidth "+windowWidth+"\n"+"windowWidth "+windowWidth+"\n"+"windowHeight "+windowHeight+"\n"+"popupHeight "+popupHeight+"\n"+"popupWidth "+popupWidth+"\n" );
  // set pop-up position
	var tgtTop = offset.top + 10;
  // adjust if too low
//  alert( "tt: "+tgtTop +" wh:"+ windowHeight +" sx:"+ scrollX +" ph"+ popupHeight);
  if ( tgtTop > windowHeight + scrollX - popupHeight )
  	var tgtTop = windowHeight + scrollX - popupHeight - 10;
  // adjust if too high (v thin viewport, unlikely!)
  if ( tgtTop < scrollX )
  	var tgtTop = scrollX;
    // set to left or right of button, depending if button on left or right hand side of page...  
  if ( offset.left > (windowWidth/2) )
	  var tgtLeft= offset.left-popupWidth - 20
  else
	  var tgtLeft= offset.left + 30
	//position relative to button
	$("#infopanel").css({
		"position": "absolute",
		"top": tgtTop,
		"left": tgtLeft,
    "width": "360px" 
	});
  // load panel
	if(infopanelStatus==0){
		$("#infopanel").css({
			"opacity": "1"
		});
		$("#infopanel").fadeIn("slow");
		infopanelStatus = 1;
	}
	//only need force for IE6
  /*	$("#popupBG").css({
		"height": windowHeight
	}); */
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// WHATS COVERED POP UP
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
// control var
var whatscoveredStatus = 0;

// Show Panel and background
function whatscoveredSet(offset,id){
  if ( whatscoveredStatus==1 )
  {
  	//disables popup only if it is enabled
  	if(whatscoveredStatus==1){
    	$("#popupBG").fadeOut("slow");
    	$("#whatscoveredpanel").fadeOut("slow");
    	whatscoveredStatus = 0;
  	}
    return;
  }
	// data for positioning
  var windowWidth = $(window).width(); 
  var windowHeight = $(document).height(); 
  var scrollX = $(window).scrollTop();
	var popupHeight = $("#infopanel").height();
	var popupWidth  = $("#infopanel").width();
  tgtTop = ( scrollX > offset.top ) ? scrollX : offset.top;
	//centering
	$("#whatscoveredpanel").css({
		"position": "absolute",
		"top": tgtTop,
		"left": offset.left - 0
	});
	//only need force for IE6
	$("#popupBG").css({
		"position": "fixed",
		"height": windowHeight,
		"width":  windowWidth
	});
  //whatscoveredLoad(id)
	//loads popup only if it is disabled
	if(whatscoveredStatus==0){
		$("#whatscoveredpanel").css({
			"opacity": "1.0"
		});
		$("#popupBG").css({
			"opacity": "0.4"
		});
		$("#popupBG").fadeIn("slow");
		$("#whatscoveredpanel").fadeIn("slow");
		whatscoveredStatus = 1;
    // ajax ...
	  // $.get("http://www.teststart.co.uk/includes/ajax/whatscovered.php?i="+id,
      $.get("/ajax/whatscovered/?i="+id,
      function(data){
//        $("#whatscoveredpanel").html(data)
      document.getElementById("whatscoveredpanel").innerHTML =data
      });
	}
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// PERSONAL COVER POP UP
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
// control var
var whatscoveredStatus = 0;

// Show Panel and background
function personalcoverSet(){
  var offset = $("#warningtriangle").offset();
  if ( whatscoveredStatus==1 )
  {
  	//disables popup only if it is enabled
  	if(whatscoveredStatus==1){
    	$("#popupBG").fadeOut("slow");
    	$("#whatscoveredpanel").fadeOut("slow");
    	whatscoveredStatus = 0;
  	}
    return;
  }
	// data for positioning
  var windowWidth = $(window).width(); 
  var windowHeight = $(document).height(); 
  var scrollX = $(window).scrollTop();
	var popupHeight = $("#whatscoveredpanel").height();
	var popupWidth  = $("#whatscoveredpanel").width();
  tgtTop = scrollX + 40;
	//centering
	$("#whatscoveredpanel").css({
		"position": "absolute",
		"top": tgtTop + 40,
		"left": offset.left 
	});
  //whatscoveredLoad(id)
	//loads popup only if it is disabled
	if(whatscoveredStatus==0){
		$("#whatscoveredpanel").css({
			"opacity": "1.0"
		});
		$("#popupBG").css({
			"opacity": "0.4"
		});
		$("#popupBG").fadeIn("slow");
		$("#whatscoveredpanel").fadeIn("slow");
		whatscoveredStatus = 1;
    // ajax ...
    $.get("/ajax/personalcover/",
      function(data){
//        $("#whatscoveredpanel").html(data)
      document.getElementById("whatscoveredpanel").innerHTML =data
      });
	}
}
////////////////////////////////////////////////////////////////////////////////


