$(document).ready(function() {
	
	/* Write flash to the screen. */
	$("#splash").media({ width:937, height:480, bgColor:"#183394" });
	$("#home_flash").media({ width:500, height:334, bgColor:"#ffffff" });
	$("#home_flash_small").media({ width:221, height:180, bgColor:"#ffffff" });
	$("#right_size_flash").media({ width:453, height:411, bgColor:"#ffffff" });
	$("#roz_flash").media({ width:938, height:431, bgColor:"#183394" });
	$("#view360_flash").media({ width:375, height:470 });
	$("#commercial_video, #outtakes_video").media({ width:452, height:302, bgColor:"#ffffff" });
	$("#print_flash").media({ width:715, height:301, bgColor:"#ffffff" });
	$("#ecard_landing_flash").media({ width:327, height:212, bgColor:"#ffffff" });
	$("#ecard_upload_flash").media({ width:347, height:409, bgColor:"#183394" });
	$("#ecard_flash").media({ width:715, height:406 });
	$("#video_1_flash").media({ width:198, height:174, bgColor:"#ffffff" });
	$("#video_2_flash").media({ width:198, height:174, bgColor:"#ffffff" });
	$("#video_3_flash").media({ width:198, height:174, bgColor:"#ffffff" });
	$("#fitevent_map").media({ width:420, height:266 });
	$("#boob_symphony_flash").media({ width:642, height:261 });
	$("#nursing_flash").media({ width:288, height:288 });
	$("#nursing_product_demo").media({ width:446, height:158 });
	$("#reuse_flash").media({ width:715, height:400 });
	
	/* Clean up the forms */	
	$("input[type=text]").addClass("text");
	$("input[type=image]").addClass("button");
	$("input[type=radio]").addClass("radio");
	
	/* Open all external links in new windows. */
	$("a, area").not("[href^=/]").not("[href^=#]").not("[href^=http://youtube.com]").attr({ target: "_blank" });
	$("[rel=external], area[class=external]").attr({ target: "_blank" });
	
	/* Track the Tape Measure download */
	$("[href=/pdf/measuring_tape.pdf]").click(function () {
		pageTracker._trackPageview('/pdf/measuring_tape');
	});
	
	/* Add the class "last" to the footer list's last elements. */
	$("#container_footer li:last, #container_footer ul > li:last").addClass("last");
	
	/* Add a "hover" class to footer and header lists that are hovered over. */
	$("#container_footer > ul > li, #navigation_top > li, #navigation_left > ul > li, #style_holder img.thumb").each(function() {
		$(this).bind("mouseenter",function(){
			$(this).addClass("hover");
		}).bind("mouseleave",function(){
			$(this).removeClass("hover");
		});
	});
	
	$("div#vote-popup").fadeIn(1000);
	
	/* Assigns and clears the default value in the search box when clicked; brings it back when empty and blured. */
	$("#txtSearch").each(function() {
		if ($(this).val() == "") {
			$(this).val($(this).attr("title"));
		}
	}).focus(function() {
		if ($(this).val() == $(this).attr("title")) {
			$(this).val("");
		}
	}).blur(function() {
		if ($(this).val() == "") {
			$(this).val($(this).attr("title"));
		}
	});
	
	/* STF Form in the Right Size section. */
	$("#perfect_fit_stf_btn, #perfect_fit_sendtofriend_close, #campaign_stf").click(function(e) {
		$("#perfect_fit_sendtofriend").slideToggle("400");
		$(this).blur();
		return false;
	});
	
	$("#commercials_colorssizes").bind("mouseenter",function(){
		$("#commercials_colorssizes img").attr({ src: "/images/commercials.colorssizes.on.png" });
	}).bind("mouseleave",function(){
		if ($("#commercials_colorssizes img").attr("class") != "active") {
			$("#commercials_colorssizes img").attr({ src: "/images/commercials.colorssizes.png" });
		}
	});
	$("#commercials_notsame").bind("mouseenter",function(){
		$("#commercials_notsame img").attr({ src: "/images/commercials.notsame.on.png" });
	}).bind("mouseleave",function(){
		if ($("#commercials_notsame img").attr("class") != "active") {
			$("#commercials_notsame img").attr({ src: "/images/commercials.notsame.png" });
		}
	});
	$("#commercials_nightmare").bind("mouseenter",function(){
		$("#commercials_nightmare img").attr({ src: "/images/commercials.nightmare.on.png" });
	}).bind("mouseleave",function(){
		if ($("#commercials_nightmare img").attr("class") != "active") {
			$("#commercials_nightmare img").attr({ src: "/images/commercials.nightmare.png" });
		}
	});
	$("#commercials_eyesuphere").bind("mouseenter",function(){
		$("#commercials_eyesuphere img").attr({ src: "/images/commercials.eyesuphere.on.png" });
	}).bind("mouseleave",function(){
		if ($("#commercials_eyesuphere img").attr("class") != "active") {
			$("#commercials_eyesuphere img").attr({ src: "/images/commercials.eyesuphere.png" });
		}
	});
	$("#outtakes_oops").bind("mouseenter",function(){
		$("#outtakes_oops img").attr({ src: "/images/outtakes.oops.on.png" });
	}).bind("mouseleave",function(){
		if ($("#outtakes_oops img").attr("class") != "active") {
			$("#outtakes_oops img").attr({ src: "/images/outtakes.oops.png" });
		}
	});
	$("#outtakes_bradancing").bind("mouseenter",function(){
		$("#outtakes_bradancing img").attr({ src: "/images/outtakes.bradancing.on.png" });
	}).bind("mouseleave",function(){
		if ($("#outtakes_bradancing img").attr("class") != "active") {
			$("#outtakes_bradancing img").attr({ src: "/images/outtakes.bradancing.png" });
		}
	});
	$("#outtakes_callthem").bind("mouseenter",function(){
		$("#outtakes_callthem img").attr({ src: "/images/outtakes.callthem.on.png" });
	}).bind("mouseleave",function(){
		if ($("#outtakes_callthem img").attr("class") != "active") {
			$("#outtakes_callthem img").attr({ src: "/images/outtakes.callthem.png" });
		}
	});
	$("#outtakes_usebrafor").bind("mouseenter",function(){
		$("#outtakes_usebrafor img").attr({ src: "/images/outtakes.usebrafor.on.png" });
	}).bind("mouseleave",function(){
		if ($("#outtakes_usebrafor img").attr("class") != "active") {
			$("#outtakes_usebrafor img").attr({ src: "/images/outtakes.usebrafor.png" });
		}
	});
	$("#outtakes_putiton").bind("mouseenter",function(){
		$("#outtakes_putiton img").attr({ src: "/images/outtakes.nightclubchurch.on.png" });
	}).bind("mouseleave",function(){
		if ($("#outtakes_putiton img").attr("class") != "active") {
			$("#outtakes_putiton img").attr({ src: "/images/outtakes.nightclubchurch.png" });
		}
	});
	$("#outtakes_dearbra").bind("mouseenter",function(){
		$("#outtakes_dearbra img").attr({ src: "/images/outtakes.dearbra.on.png" });
	}).bind("mouseleave",function(){
		if ($("#outtakes_dearbra img").attr("class") != "active") {
			$("#outtakes_dearbra img").attr({ src: "/images/outtakes.dearbra.png" });
		}
	});
	$("#outtakes_animalpatterns").bind("mouseenter",function(){
		$("#outtakes_animalpatterns img").attr({ src: "/images/outtakes.animalpatterns.on.png" });
	}).bind("mouseleave",function(){
		if ($("#outtakes_animalpatterns img").attr("class") != "active") {
			$("#outtakes_animalpatterns img").attr({ src: "/images/outtakes.animalpatterns.png" });
		}
	});
	
		
	/* Rollovers on the thumbs. */
	$("table.thumbs a").each(function(){
		$(this).bind("mouseenter",function(){
			$("table.thumbs ." + $(this).attr("class") + " img").css({ borderWidth: "2px", margin: "0px" });
		}).bind("mouseleave",function(){
			$("table.thumbs ." + $(this).attr("class") + " img").css({ borderWidth: "1px", margin: "1px" });
		});
	});
	
	$("#swatch img").each(function(){
		$(this).click(function() {
			$("#style_detail").attr({ src: "/images/styles/color_detail/" + $(this).attr("alt") + ".jpg" });
			return false;
		});
		$(this).bind("mouseenter",function(){
			$(this).addClass("hover");
		}).bind("mouseleave",function(){
			$(this).removeClass("hover");
		});
	});
	
	/* Takes case of the store locator form on the detail pages. */
	$("form#detail_store_locator input.text").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && e.which != 13 && (e.which < 48 || e.which > 57)) {
			return false;
		} else if (e.which != 8 && e.which != 0 && e.which != 13 && $("form#detail_store_locator input.text").val().toString().length >= 5) {
			return false;
		}
	});
	$("form#detail_store_locator").submit(function(event){
		var filter = /^([0-9]{5})$/;
		if (!filter.test($("form#detail_store_locator input.text").val())) {
			alert("Please enter a valid zip code.");
			$("form#detail_store_locator input.text").focus();
			return false;
		}
	});
	
	$("#ecard_upload_form").submit(function(event){
		var filename = $("#ecard_upload_form #pic").val();
		var dot = filename.lastIndexOf(".");
		var ext = filename.substr(dot + 1, filename.length).toLowerCase();
		if (ext != 'jpg' && ext != 'jpeg' && ext != 'png' && ext != 'bmp' && ext != 'gif')
		{
			alert('Allowed file types are PNG, BMP, JPG, and GIF.');
			return false;
		}
	});
	
	/* Advanced search checkboxes. */
	$("#frmAdvancedSearch input.chkT, #frmAdvancedSearch input.chkCu, #frmAdvancedSearch input.chkSt, #frmAdvancedSearch input.chkClr, #frmAdvancedSearch input.chkB, #frmAdvancedSearch input.chkO, #frmAdvancedSearch input.chkCo, #frmAdvancedSearch input.chkF, #frmAdvancedSearch input.chkA, #frmAdvancedSearch input.chkL").each(function() {
		$(this).click(function(e) {
			$("#frmAdvancedSearch input." + $(this).attr('class')).attr('checked', false);
			$(this).attr('checked', true);
		});
	});
	$("#frmAdvancedSearch input#allbrand").click(function(e) {
		if ( $(this).attr('checked') ){
			$("#frmAdvancedSearch input.brandId").attr('checked', false);
		} else {
			$("#frmAdvancedSearch input.brandId").attr('checked', true);
		}
	});
	$("#frmAdvancedSearch input.brandId").each(function() {
		$(this).click(function(e) {
			$("#frmAdvancedSearch input#allbrand").attr('checked', false);
		});
	});	
});

function openWindow(url, name, rs, w, h) {
	var resize = "";
	if (rs == false) {
		resize = "resizable=no,";
	}
	popupWin = window.open(url, name, '' + resize + 'width=' + w + ',height=' + h + ',scrollbars=no');
}

function closePopup() {
	$("div#vote-popup").fadeOut(500);
};