/*
	Shirt customisation
*/
if(!MCFC.customisation) { console.warn('PLEASE INCLUDE shop-customisation.js');}
MCFC.customisation.shirt = function () {

	var swf_element = 'product-main-image';
	var sel;
	
	/*
		Initialize the Javascript
		removes panes the we will open 
		later using JS
		Force reset the radio buttons since some
		browsers like to remember the previous settings 
		when using the back button.
	*/
	var init = function () {
	    sel = MCFC.customisation.selectors;
	    $(sel.noprint_radio).attr('checked', true);
		$('#product-main-detail #shirt-customisation h4').show();
		$('#select-player-fields').hide();
		$('#custom-fields').hide();
		bind_behaviour_events();
		MCFC.customisation.embed_swf(swf_element);
		pub.reset();
	};
	
	
	/*
		Check the differenct permutations and
		grab the prices accoringly from the data settings.
	*/
	var update_price = function () {
		var price = MCFC.customisation.data.original_price;
		if (sel.player_radio && $(sel.player_radio).attr('checked')) {
			// player select
			price += MCFC.customisation.data.name_and_number;
		}
		if (sel.customnameandnumber_radio && $(sel.customnameandnumber_radio).attr('checked')) {
		    if (sel.customname_input && $(sel.customname_input).val().length && $(sel.customnumber_input).val().length) {
				price += MCFC.customisation.data.name_and_number;
            } else if (sel.customname_input && $(sel.customname_input).val().length || $(sel.customnumber_input).val().length) {
				price += MCFC.customisation.data.name_or_number;
			}
		}
		if (sel.badge_checkbox && $(sel.badge_checkbox).attr('checked')) {
			// added badges
			price += MCFC.customisation.data.badge;
		}
		MCFC.customisation.set_price(price);
	};
	
	/*
		Bind events for radio buttons, selectboxes
		and when the user types into the input fields.
	*/	
	var bind_behaviour_events = function () {
	    if(sel.noprint_radio) $(sel.noprint_radio).click(pub.no_print);
		if(sel.player_radio) $(sel.player_radio).click(pub.player);
		if(sel.customnameandnumber_radio) $(sel.customnameandnumber_radio).click(pub.custom);
		if(sel.badge_checkbox) $(sel.badge_checkbox).click(pub.badge);
		if(sel.customname_input) $(sel.customname_input).bind('keyup', on_name_input);
		if(sel.customnumber_input) $(sel.customnumber_input).bind('keyup', on_number_input);
		if(sel.selectplayer_dropdown) $(sel.selectplayer_dropdown).change(on_player_change);
	};
	
	/*
		Callbacks for user data input
	*/
	var on_name_input = function () {
		$(this).val(MCFC.customisation.filter_characters($(this).val()));
		update_price();
		document.getElementById(swf_element).setName($(this).val());
	};
	var on_number_input = function () {
		$(this).val(MCFC.customisation.filter_numbers($(this).val()));
		update_price();
		document.getElementById(swf_element).setNumber($(this).val());	
	};	
	var on_player_change = function () {
		var values = $(this).val().split('|');
		document.getElementById(swf_element).setName(values[0]);
		document.getElementById(swf_element).setNumber(values[1]);
	};
		
	var reset_name_and_number = function () {
		document.getElementById(swf_element).setName('');
		document.getElementById(swf_element).setNumber('');
		$('#no-print-wrapper .price').removeClass('active');
		$('#player-wrapper .price').removeClass('active');
		$('#custom-wrapper .price').removeClass('active');
	};
	
	var pub = {
		no_print : function () {
			pub.remove_custom();
			pub.remove_player();
			reset_name_and_number();
			update_price();
			$('#no-print-wrapper .price').addClass('active');
		},
		player : function () {
			reset_name_and_number();
			$('#select-player-fields').show();
			pub.remove_custom();
			update_price();
			$('#player-wrapper .price').addClass('active');
		},
		custom : function () {
		    console.log('custom');
			reset_name_and_number();
			$('#custom-fields').show();
			pub.remove_player();
			update_price();
			$('#custom-wrapper .price').addClass('active');
		},
		remove_player : function () {
		    console.log('remove_player');
		    $(sel.selectplayer_dropdown).val('');
			reset_name_and_number();
			$('#select-player-fields').hide();
			update_price();
		},
		remove_custom : function () {
		    $(sel.customname_input).val('');
		    $(sel.customnumber_input).val('');
			$('#custom-fields').hide();
		},
		badge : function () {
			if($(this).attr('checked')) {
				document.getElementById(swf_element).addBadge();
				$('#badge-wrapper .price').addClass('active');
			}else{
				document.getElementById(swf_element).removeBadge();
				$('#badge-wrapper .price').removeClass('active');
			}
			update_price();
		},
		reset : function () {
		    $(sel.customname_input).val('');
		    $(sel.customnumber_input).val('');
		    $(sel.badge_checkbox).attr('checked', false);
			$(sel.selectplayer_dropdown).val('');
			var el = document.getElementById(swf_element);
			if(el && el.reset){
				el.reset();
			}
			update_price();
		}
	};
	
	MCFC.init.add_on_dom_ready(init);
}();