$.noConflict();
// Logos
jQuery(document).ready(function(){


	var path = location.pathname;
	
	if(path.indexOf("mums") != -1) {
		processMumShirtBuilder();
	} else if(path.indexOf("babys") != -1) {
		processBabyShirtBuilder();
	}	
});

function processBabyShirtBuilder() {

	if(jQuery.browser.msie) {
		jQuery("label").click(function(){		
			if (jQuery(this).attr("for") != "")	{				
				jQuery("#" + jQuery(this).attr("for")).click();
			}
		});
	}

	//Setup Product Builder Default Selections
	jQuery("#options_11_2").attr('checked', true); // Colour
	jQuery("#options_12_2").attr('checked', true); // Logo
	jQuery("#select_10").val("45"); // Month
	jQuery("#select_9").val("44"); // Size
	jQuery("#options_13_2").attr('checked', true); // Style


	// Get the Month Variation
	if(getUrlParam("variation") != null) {
		var monthVariation = getUrlParam("variation");
		
		if(monthVariation=="jan") {
			jQuery("#select_10").val("45"); 
		} else if(monthVariation=="feb") {
			jQuery("#select_10").val("47"); 
		} else if(monthVariation=="mar") {
			jQuery("#select_10").val("48"); 
		} else if(monthVariation=="apr") {
			jQuery("#select_10").val("49"); 
		} else if(monthVariation=="may") {
			jQuery("#select_10").val("50"); 
		} else if(monthVariation=="jun") {
			jQuery("#select_10").val("51"); 
		} else if(monthVariation=="jul") {
			jQuery("#select_10").val("52"); 
		} else if(monthVariation=="aug") {
			jQuery("#select_10").val("53"); 
		} else if(monthVariation=="sep") {
			jQuery("#select_10").val("54"); 
		} else if(monthVariation=="oct") {
			jQuery("#select_10").val("55"); 
		} else if(monthVariation=="nov") {
			jQuery("#select_10").val("56"); 
		} else if(monthVariation=="dec") {
			jQuery("#select_10").val("57"); 
		}
	}


	// Add image layer to product image 
	jQuery("p.product-image").prepend('<img id="shirtLogo" style="margin-top: 150px; margin-left:70px; z-index:4000;" src="/media/images/product-builder/logos/born-blue-jan-white.png" />');


	// Shirt Colour
	jQuery("label[for='options_11_2']").attr('class', 'colourWhite');
	jQuery("label[for='options_11_2']").text('');	
	jQuery("#options_11_2").change(chooseBabyImage);	
	if(jQuery.browser.msie) {
		jQuery("#options_11_2").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_11_2']").attr("style", "margin-left:-20px; ");
		jQuery("#options_11_2").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_11_2").toggle();
	}

	// Shirt Style
	jQuery("label[for='options_13_2']").attr('class', 'styleTee');
	jQuery("label[for='options_13_2']").text('');
	jQuery("#options_13_2").change(chooseBabyImage);
	if(jQuery.browser.msie) {
		jQuery("#options_13_2").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_13_2']").attr("style", "margin-left:-20px; ");
		jQuery("#options_13_2").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_13_2").toggle();
	}	

	// Logo Colour
	jQuery("label[for='options_12_2']").attr('class', 'logoBorn-blue');
	jQuery("label[for='options_12_2']").text('');
	jQuery("#options_12_2").change(chooseBabyImage);
	if(jQuery.browser.msie) {
		jQuery("#options_12_2").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_12_2']").attr("style", "margin-left:-60px; margin-top:-15px;");
		jQuery("#options_12_2").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_12_2").toggle();
	}

	jQuery("label[for='options_12_3']").attr('class', 'logoBorn-pink');
	jQuery("label[for='options_12_3']").text('');
	jQuery("#options_12_3").change(chooseBabyImage);
	if(jQuery.browser.msie) {
		jQuery("#options_12_3").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_12_3']").attr("style", "margin-left:-60px; margin-top:-15px;");
		jQuery("#options_12_3").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_12_3").toggle();
	}

	jQuery("label[for='options_12_4']").attr('class', 'logoBorn-red');
	jQuery("label[for='options_12_4']").text('');
	jQuery("#options_12_4").change(chooseBabyImage);
	if(jQuery.browser.msie) {
		jQuery("#options_12_4").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_12_4']").attr("style", "margin-left:-60px; margin-top:-15px;");
		jQuery("#options_12_4").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_12_4").toggle();
	}

	// Change image on month select
	jQuery("#select_10").change(chooseBabyImage);

	chooseBabyImage();

}

function processMumShirtBuilder() {

	if(jQuery.browser.msie) {
		jQuery("label").click(function(){		
			if (jQuery(this).attr("for") != "")	{				
				jQuery("#" + jQuery(this).attr("for")).click();
			}
		});
	}

	// Setup Product Builder Default Selections
	jQuery("#select_5").val("22");
	jQuery("#options_6_2").attr('checked', true);
	jQuery("#options_7_2").attr('checked', true);
	jQuery("#options_8_2").attr('checked', true);
	jQuery("p.product-image").prepend('<img id="shirtLogo" style="margin-top: 150px; margin-left:70px; z-index:4000;" src="" />');

	// Look at URL and if the logo parameter is set, then get the value to auto config the builder
	if(getUrlParam("logo") != null) {
		var logoParam = getUrlParam("logo");
		
		if(logoParam=="due") {
			jQuery("#options_6_3").attr('checked', true);
		} else if(logoParam=="coming") {
			jQuery("#options_6_2").attr('checked', true);
		} else if(logoParam=="expecting") {
			jQuery("#options_6_4").attr('checked', true);
		} else {
                    // if nothing else then go to plain shirt
                    jQuery("#options_6_5").attr('checked', true);
                }
	}

	// Get the Month Variation	
	if(getUrlParam("variation") != null) {
		var monthVariation = getUrlParam("variation");
		
		if(monthVariation=="jan") {
			jQuery("#select_5").val("22"); 
		} else if(monthVariation=="feb") {
			jQuery("#select_5").val("23"); 
		} else if(monthVariation=="mar") {
			jQuery("#select_5").val("24"); 
		} else if(monthVariation=="apr") {
			jQuery("#select_5").val("25"); 
		} else if(monthVariation=="may") {
			jQuery("#select_5").val("26"); 
		} else if(monthVariation=="jun") {
			jQuery("#select_5").val("27"); 
		} else if(monthVariation=="jul") {
			jQuery("#select_5").val("28"); 
		} else if(monthVariation=="aug") {
			jQuery("#select_5").val("29"); 
		} else if(monthVariation=="sep") {
			jQuery("#select_5").val("30"); 
		} else if(monthVariation=="oct") {
			jQuery("#select_5").val("31"); 
		} else if(monthVariation=="nov") {
			jQuery("#select_5").val("32"); 
		} else if(monthVariation=="dec") {
			jQuery("#select_5").val("33"); 
		}
	} else {
		jQuery("#select_5").val("22");		
		monthVariation = "jan";
	}

	// Change Image on Month Select
	jQuery("#select_5").change(chooseMumImage);

	// Logo Options

	jQuery("label[for='options_6_2']").attr('class', 'logoComing');
	jQuery("label[for='options_6_2']").text('');
	jQuery("#options_6_2").change(chooseMumImage);
	if(jQuery.browser.msie) {
		jQuery("#options_6_2").click(function() {
			this.blur(); 
			this.focus(); 
		});
		jQuery("label[for='options_6_2']").attr("style", "margin-left:-50px; margin-top: -10px;");
		jQuery("#options_6_2").attr("style", "width:1px; height:1px;");
	} else { 
		jQuery("#options_6_2").toggle();
	}

	
	jQuery("label[for='options_6_3']").attr('class', 'logoDue');
	jQuery("label[for='options_6_3']").text('');
	jQuery("#options_6_3").change(chooseMumImage);		
	if(jQuery.browser.msie) { 
		jQuery("#options_6_3").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_6_3']").attr("style", "margin-left:-50px; margin-top: -10px;");	
		jQuery("#options_6_3").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_6_3").toggle();
	}

	jQuery("label[for='options_6_4']").attr('class', 'logoExpecting');
	jQuery("label[for='options_6_4']").text('');
	jQuery("#options_6_4").change(chooseMumImage);
	if(jQuery.browser.msie) {
		jQuery("#options_6_4").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_6_4']").attr("style", "margin-left:-70px; margin-top: -10px;");
		jQuery("#options_6_4").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_6_4").toggle();
	}

	jQuery("label[for='options_6_5']").attr('class', 'logoBlank');
	jQuery("label[for='options_6_5']").text('');
	jQuery("#options_6_5").change(chooseMumImage);
	if(jQuery.browser.msie) {
		jQuery("#options_6_5").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_6_5']").attr("style", "margin-left:-53px; margin-top: -15px;");
		jQuery("#options_6_5").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_6_5").toggle();
	}
	

    // Color options

	jQuery("label[for='options_7_2']").attr('class', 'colourBlack');
	jQuery("label[for='options_7_2']").text('');	
	jQuery("#options_7_2").change(chooseMumImage);	
	if(jQuery.browser.msie) {
		jQuery("#options_7_2").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_7_2']").attr("style", "margin-left:-30px;");
		jQuery("#options_7_2").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_7_2").toggle();
	}

	jQuery("label[for='options_7_3']").attr('class', 'colourWhite');
	jQuery("label[for='options_7_3']").text('');
	jQuery("#options_7_3").change(chooseMumImage);
	if(jQuery.browser.msie) {
		jQuery("#options_7_3").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_7_3']").attr("style", "margin-left:-20px; ");
		jQuery("#options_7_3").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_7_3").toggle();
	}

    // Shirt Styles
    
        // **** Long Sleeve ****
	jQuery("label[for='options_8_2']").attr('class', 'styleShirt');
	jQuery("label[for='options_8_2']").text('');
	jQuery("#options_8_2").change(chooseMumImage);
	if(jQuery.browser.msie) {
		jQuery("#options_8_2").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_8_2']").attr("style", "margin-left:-20px; ");
		jQuery("#options_8_2").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_8_2").toggle();
	}
        // *** Short Sleeve ****
	jQuery("label[for='options_8_3']").attr('class', 'styleTee');
	jQuery("label[for='options_8_3']").text('');
	jQuery("#options_8_3").change(chooseMumImage);
	if(jQuery.browser.msie) {
		jQuery("#options_8_3").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_8_3']").attr("style", "margin-left:-20px; ");
		jQuery("#options_8_3").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_8_3").toggle();
	}
        // **** Singlet ****
	jQuery("label[for='options_8_4']").attr('class', 'styleSinglet');
	jQuery("label[for='options_8_4']").text('');
	jQuery("#options_8_4").change(chooseMumImage);
	if(jQuery.browser.msie) {
		jQuery("#options_8_4").click(function() {
			this.blur(); 
			this.focus(); 
		});	
		jQuery("label[for='options_8_4']").attr("style", "margin-left:-20px; ");
		jQuery("#options_8_4").attr("style", "width:1px; height:1px;");
	} else {
		jQuery("#options_8_4").toggle();
	}
	
	chooseMumImage();

        //alert(jQuery('[name="options[8]"]').val());

}


// Image to show based on selected options.
function chooseMumImage() {

	var selectedStyle = jQuery("input[name='options[8]']:checked").val();
	var selectedLogo = jQuery("input[name='options[6]']:checked").val();
	var selectedColour = jQuery("input[name='options[7]']:checked").val();
	var selectedMonth = jQuery("#select_5").val();

	var style = "";
	var colour = "";
	var logo = "";
	var month = getMonthAbbrev(selectedMonth);
	var season = getSeason(month);
	
	//alert("Style: " + selectedStyle);
	//alert("Logo: " + selectedLogo);
	//alert("Colour: " + selectedColour);
	//alert("Month: " + selectedMonth);

	// 39 = white
	// 38 = black

	if(selectedStyle == "41") { // Long
		// Check to see which color is chosen
		style ="long";		
	} else if(selectedStyle == "40") {  // short
		// Check to see which color is chosen
		style = "short";
	} else if(selectedStyle == "42") {  // Singlet
		// Check to see which color is chosen
		style = "singlet";
	}

	if(selectedColour == 38) {
		// long sleeve white
		colour = "white";
	} else if(selectedColour == 39) {
		// long sleeve black
		colour = "black";
	}


	// now select the correct LOGO to show
	if(selectedLogo == 36) { // Coming this
		logo = "coming";
	} else if(selectedLogo == 34) { // Due In
		logo = "due";
	} else if(selectedLogo == 35) { // Expecting in
		logo = "expecting";
	} else { // plain tee
		logo = "plain";
	} 

	// if month is undefined just show jan
	if(month == null) {
		month = "jan";
	}

	
	// Change the shirt image
	jQuery('#image').attr('src','/media/images/product-builder/shirts/'+ colour +'-'+ style +'.jpg');
	
	
	// Change the logo
	if(logo == "plain") {
		jQuery("#shirtLogo").attr("src", '/media/images/1x1.gif');
	}else if(logo == "coming") {
		jQuery("#shirtLogo").attr("src", '/media/images/product-builder/logos/'+ logo +'-'+ season +'-'+ colour +'.png');
	} else {
		jQuery("#shirtLogo").attr("src", '/media/images/product-builder/logos/'+ logo +'-'+ month +'-'+ colour +'.png');
	}
}

// Gets options for Baby Shirts and Chooses which shirt and images to show
function chooseBabyImage() {

	var selectedLogoColour = jQuery("input[name='options[12]']:checked").val();
	var selectedMonth = jQuery("#select_10").val();

	var colour = "";
	var logo = "";
	var month = getBabyMonthAbbrev(selectedMonth);

	if(selectedLogoColour=="59") { // Blue
		colour = "blue";		
	} else if(selectedLogoColour=="58") {  // Pink
		colour = "pink";		
	} else if(selectedLogoColour=="60") {  // Red
		colour = "red";		
	}
			
	// Change the shirt image
	jQuery('#shirtLogo').attr('src','/media/images/product-builder/logos/born-'+ colour +'-'+ month +'-white.png');

}

// Gets the month abbreviation for baby products based on month id
function getBabyMonthAbbrev(month) {

	if(month==45) {
		return "jan";
	} else if(month==47) {
		return "feb";
	} else if(month==48) {
		return "mar";
	} else if(month==49) {
		return "apr";
	} else if(month==50) {
		return "may";
	} else if(month==51) {
		return "jun";
	} else if(month==52) {
		return "jul";
	} else if(month==53) {
		return "aug";
	} else if(month==54) {
		return "sep";
	} else if(month==55) {
		return "oct";
	} else if(month==56) {
		return "nov";
	} else if(month==57) {
		return "dec";
	}
}


// Gets the month abbreviatino for mum products based on month id
function getMonthAbbrev(month) {	

	if(month==22) {
		return "jan";
	} else if(month==23) {
		return "feb";
	} else if(month==24) {
		return "mar";
	} else if(month==25) {
		return "apr";
	} else if(month==26) {
		return "may";
	} else if(month==27) {
		return "jun";
	} else if(month==28) {
		return "jul";
	} else if(month==29) {
		return "aug";
	} else if(month==30) {
		return "sep";
	} else if(month==31) {
		return "oct";
	} else if(month==32) {
		return "nov";
	} else if(month==33) {
		return "dec";
	}
}

function getSeason(month){
        var season = "summer";

        if (month == "mar" || month == "apr" || month == "may") {

            season = "autumn";

        } else if (month == "jun" || month == "jul" || month == "aug") {

            season = "winter";

        } else if (month == "sep" || month == "oct" || month == "nov") {

            season = "spring";

        }

        return season;
}

function getUrlParam(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );

  if(results===null) {
    return null;
  } else {
    return results[1];
  }

}


