// IE6 browser detection and redirect
if($.browser.msie){
	userAgent = $.browser.version;
	userAgent = userAgent.substring(0,userAgent.indexOf('.'));
	version = userAgent;
	if( version <= 6 ) {
		window.location = "/error/unsupportedbrowser.html";
	}
}

// Function to expand and collapse content marked appropriatly
function toggleExpand() {
	if( $(".btnExpand").hasClass("btnCollapse") ) {
		$(".btnExpand").removeClass("btnCollapse");
		$(".expand").addClass("collapse").removeClass("expand");					
	} else {	
		$(".btnExpand").addClass("btnCollapse");
		$(".collapse").addClass("expand").removeClass("collapse");
	}
}

function initPage(obj) {
	// Dynamically create button styling
	$('a.button', obj).wrapInner('<span>').wrap('<div class="button">');
	$('a.button2', obj).wrapInner('<span>').wrap('<div class="button2">');
	$('a.buttonS', obj).wrapInner('<span>').wrap('<div class="buttonS">');
	$('a.buttonRight', obj).parent().addClass("buttonRight");
	$('p.alert', obj).append('<div style="clear:both; float:none" />');
    $('div.headerContent',obj).after('<div class="feedbackPanel">Let us know what you think of the new site <a href="/myaccount/SendFeedback.jsf">Share your feedback</a>!</div>');   
}

function playVideo(vid) {
	var v = window.open("../videos/"+vid,"video_player","menubar=1,resizable=1,width=450,height=350");
	return false;
}
function insertVideo(youTubeId) {
	var playerSize = [400,258];
	if( navigator.userAgent.indexOf("iPad") > 0 || navigator.userAgent.indexOf("iPod") > 0 || navigator.userAgent.indexOf("iPhone") > 0 ) 
		document.write( '<iframe class="youtube-player" type="text/html" width="'+playerSize[0]+'" height="'+playerSize[1]+'" src="http://www.youtube.com/embed/'+youTubeId+'?hl=en_US" frameborder="0"></iframe>' );
	else
		document.write( '<div class="youtube"><object width="'+playerSize[0]+'" height="'+playerSize[1]+'"><param name="movie" value="http://www.youtube.com/v/'+youTubeId+'?fs=1&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+youTubeId+'?fs=1&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="'+playerSize[0]+'" height="'+playerSize[1]+'"></embed></object></div>');
}

function writeD(val) { document.write( "$"+val.toFixed(2) ); }
function writeC(val) { document.write( eval(val*100).toFixed(2)+"&cent;" ); }

$(document).ready(function() {
	// Dynamically create button styling
	initPage($(document));

	// Tool Tips
	  $("body").append("<div id='ToolTipDiv'></div>");
	  $("[title]").each(function() {
	    $(this).hover(function(e) {
	      $(this).mousemove(function(e) {
	        var tipY = e.pageY + 15;
	        var tipX = e.pageX + 10;
	        $("#ToolTipDiv").css({'top': tipY, 'left': tipX});
	      });
	      $("#ToolTipDiv")
	        .html($(this).attr('title'))
	        .stop(true,true)
	        .fadeIn("fast");
	      $(this).attr('title','');
	    }, function() {
	      $("#ToolTipDiv")
	        .stop(true,true)
	        .fadeOut("fast");
	      $(this).attr('title', $("#ToolTipDiv").html());
	    });
	  });
	
	// Created rounded corners for IE7 and IE8
	if( $.browser.msie ) {
		$('div.content').css({"background-color":"#bbb", "padding":"2px", "border":"0px", "width":((parseInt($('div.content').css("width"))+20)+"px") }).wrapInner('<div class="contentIeBorder">');
		$('div.content').corner("bottom cc:#ebebeb 10px");
		$('div.contentIeBorder').corner("bottom cc:#bbb 10px");
		$('div.contentLogin').corner("15px");
		$('div.loginBox').corner("top cc:#bfd9f2 12px").corner("bottom cc:#fff 12px");
		$('div.errorMessage').corner("10px");
		$('p.errorMessage').corner("10px");
		$('p.infoMessage').corner("10px");
		$('.warningMessage').corner("10px");
		$('div.panel').corner("10px");
		$('div.loginInfoBox1').corner("5px");
		$('div.contentInfoPanel h2').corner("top 5px cc:#fff");
		$('div.contentInfoPanel div.infoBox1').corner("bottom 5px cc:#fff");
		$('div.contentInfoPanel div.infoBox2').corner("5px cc:#fff");
		$('div.searchFilters').corner("10px");
	}
	
	// Add modal box
	if( $("a.jqmTrigger").length > 0 ) {
		function resizeModal(newload) {
			var tf = $('div.jqModal iframe', parent.document.body);
			if( tf.length != 0 ) {
				tf.height( Math.min( tf.contents().find("body").outerHeight()+15, $(window).height()-(tf.offset().top-$(document).scrollTop())*2-parseInt(tf.css("padding-top"))-parseInt(tf.css("padding-bottom")) ) );
				
				if( !($.browser.msie) )
					$('div.jqModal iframe').css( { 'width':''} );
				var w = tf.contents().width()+(newload=="true"||!($.browser.msie)?parseInt(tf.css("padding-left"))+parseInt(tf.css("padding-right")):0);
				$('div.jqModal iframe').css( { 'width':w+"px", 'margin-left':'-'+(w/2)+'px'} );
				$('div.jqModal div.close').css( {'margin-left':(w/2+10)+'px'} );
			}				
		}
		
		$("a.jqmTrigger").click( function() {
			$("body").append("<div class=\"jqModal\"><div class=\"cover\"></div><iframe frameborder=\"0\" src=\"\"></iframe></div>");
			$(window).bind('resize',resizeModal);
			$('div.jqModal iframe').load( function() {
				$('div.jqModal').fadeIn('fast');
				resizeModal("true");
				$('div.jqModal iframe').contents().find("a.jqmClose").click( function() {
					$('div.jqModal').fadeOut('fast', function() {
						$('div.jqModal').remove();
					});
					$(window).unbind('resize',resizeModal);
				});
				$('div.jqModal iframe').contents().find("a.jqmCloseRefresh").click( function() {
					$('div.jqModal').fadeOut('fast', function() {
						$('div.jqModal').remove();
						location.reload();
					});
					$(window).unbind('resize',resizeModal);						
				});

			}).attr( "src",this.href );
			$('div.jqModal div.close a').click( function() {
				$('div.jqModal').fadeOut('fast', function() {
					$('div.jqModal').remove();
				});
			});
			return false;
		});
	}
	
	$("input.searchBox").focus( function() {
		$(this).addClass('hideBack'); 
	});
	$("input.searchBox").blur( function() {
		if( $(this).attr("value") == "" ) $(this).removeClass('hideBack');
	});
	$("input.searchBox").blur();
	
	// Conditionally load the date pickers
	if(typeof $("input.datepicker").datepicker == 'function')
		$("input.datepicker").datepicker({
			showOn: 'button',
			buttonImage: '../css/images/calendar-icon.png',
			buttonImageOnly: true
		});
		
	// Google Analytics
	 var _gaq = _gaq || [];
	 _gaq.push(['_setAccount', 'UA-18439749-1']);
	 _gaq.push(['_trackPageview']);
	
	 (function() {
	   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	 })();
});
