var pop_status = 1;
$(document).ready(function() {
  var country_status = 0;
  var viewed_popup = $.cookie("viewed_popup");


  $("#ctl00_ctl00_header_cb p").click(function() {
	if(country_status == 0) {
		$("#ctl00_ctl00_cphMain_navTop_pnlSearch span").hide();
		$("#ctl00_ctl00_header_cb ul").slideDown();
		country_status = 1;
	}
	else {
		$("#ctl00_ctl00_header_cb ul").slideUp();
		$("#ctl00_ctl00_cphMain_navTop_pnlSearch span").show();
		country_status = 0;
	}
	return false;
  });
  $("#ctl00_ctl00_header_cb a").click(function() {
	return true;
  });



  $("#ctl00_header_cb p").click(function() {
	if(country_status == 0) {
		$("#ctl00_cphMain_navTop_pnlSearch span").hide();
		$("#ctl00_header_cb ul").slideDown();
		country_status = 1;
	}
	else {
		$("#ctl00_header_cb ul").slideUp();
		$("#ctl00_cphMain_navTop_pnlSearch span").show();
		country_status = 0;
	}
	return false;
  });
  $("#ctl00_header_cb a").click(function() {
	return true;
  });

  $("#email_submit").click(function() {
	var address = $("#email_address").val();
	if(isValidEmailAddress(address)) {
	
		var msg = 'Click OK to accept our <a href="/landing.aspx?id=691" target="_new">Privacy Policy</a> and complete your newsletter registration.</p>';
		$.prompt(msg,{ buttons: { Cancel: false, OK: true }, callback: mycallbackfunc });
	}
	else {
		$.prompt('Please enter a valid email address.');
	}
	return false;
  });
  $("#email_address").focus(function() {
	var address = $(this).val();
	if(address == 'EMAIL ADDRESS') {
		$(this).val('');
	}
  });
  $("#email_address").blur(function() {
	var address = $(this).val();
	if(address == '') {
		$(this).val('EMAIL ADDRESS');
	}
  });

  $('#rotating_images').cycle({ 
	fx:    'fade', 
	timeout: 8000,
	speed:  2000 
  });

  var first_click = 0;
  var interacted = 0;
  var timeout_dock = 10000;

  //turn initial animation off
  viewed_popup = 1;
  if(viewed_popup != 1) {
    var date = new Date();
    date.setTime(date.getTime() + (60 * 60 * 1000));
    $.cookie("viewed_popup", '1', { path: '/', expires: date })

    var height = $(window).height();
    var width = $(document).width();
    height = (height / 2) - 85 + 216;
    height = height + "px";
    width = (width / 2) + 150;
    width = "+=" + width + "px";
    $("#popup").css("top",height);

    var doc_height = $(document).height();
    $("#popup_bg").fadeTo("slow",0.4);
    $("#popup_bg").css("height", doc_height + "px");
    var animation_time = 1000;
    setTimeout(function(){ 
	  $("#popup").animate( {"left": width }, animation_time, "swing");
      }
    , 2000 ); 
  }
  else {
    first_click = 0;
    interacted = 0;
    timeout_dock = 0;
    var width = $(document).width();
    width = (width / 2) + 150;
    $("#popup").css("left",width);
    $("#popup_close").html("SHOW ");
  }

  $("#popup_close").click(function() {
    if(first_click == 0) {
      first_click = 1;
      pop_status = 0;
      var this_offset = $("#popup").offset();
      var this_top = this_offset.top;
      this_top = this_top + 164;
      this_top = "-=" + this_top + "px";
      $("#popup").animate({"top": this_top}, animation_time, "swing");
      $("#popup_bg").fadeOut("slow");
      $("#popup_close").html("SHOW ");
    }
    else if(pop_status == 1) {
      pop_status = 0;
      $("#popup").animate({"top": "-=164px"}, animation_time, "swing");
      $("#popup_bg").fadeOut("slow");
      $("#popup_close").html("SHOW ");
    }
    else {
      pop_status = 1;
      $("#popup_bg").fadeTo("slow",0.4);
      $("#popup").animate({"top": "+=164px"}, animation_time, "swing");
      $("#popup_close").html("HIDE X");
    }
  });
  $("#popup_email").focus(function() {
    interacted = 1;
    var this_email = $(this).val();
    if(this_email == "EMAIL ADDRESS") {
	$(this).val("");
    }
  });
  $("#popup_email").blur(function() {
    var this_email = $(this).val();
    if(this_email == "") {
	$(this).val("EMAIL ADDRESS");
    }
  });
  $("#popup_submit").click(function() {
    var this_address = $("#popup_email").val();
    if( isValidEmailAddress(this_address) ) {
        //$.prompt('Thank you for signing up for SRS communications.');
	$.get("/newsletter_signup.asp", { email: this_address}, function(data) {
			$.prompt('Thank you for signing up for SRS communications.');
		}
	);
	//$.prompt('Thank you for signing up for SRS communications.',{ callback: mycallbackfunc2 });
    }
    else {
	$.prompt('Please enter a valid email address.');
    }
    return false;
  });
  setTimeout(function(){ 
	if(interacted == 0) {
		first_click = 1;
		pop_status = 0;
		var this_offset = $("#popup").offset();
		var this_top = this_offset.top;
		this_top = this_top + 164;
		this_top = "-=" + this_top + "px";
		$("#popup").animate({"top": this_top}, animation_time, "swing");
		$("#popup_bg").fadeOut("slow");
		$("#popup_close").html("SHOW ");
	}
    }
  , timeout_dock ); 
  $(window).resize(function() {
    var width = $(document).width();
    width = (width / 2) + 150;
    $("#popup").css("left",width);
  });


  function mycallbackfunc2(v,m,f){
    if(v == true) {
      pop_status = 0;
      var this_offset = $("#popup").offset();
      var this_top = this_offset.top;
      this_top = this_top + 164;
      this_top = "-=" + this_top + "px";
      $("#popup").animate({"top": this_top}, animation_time, "swing");
      $("#popup_bg").fadeOut("slow");
      $("#popup_close").html("SHOW ");
      first_click = 1; //just in case
  }
}
});
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
function mycallbackfunc(v,m,f){
	var address = $("#email_address").val();
	if(v == true) {
		$.get("/newsletter_signup.asp", { email: address }, function(data) {
					$.prompt('Thank you for signing up for SRS communications.');
			}
		 );
	}
}
$(document).click(function() {
	$("#ctl00_ctl00_header_cb ul").hide();
	$("#ctl00_ctl00_cphMain_navTop_pnlSearch span").show();
	$("#ctl00_header_cb ul").hide();
	$("#ctl00_cphMain_navTop_pnlSearch span").show();
	country_status = 0;
});

