// jQuery based plugin to change the color of elements. 

var swatch_path='http://www.sonata.com.sg/image/data/site-images/color-swatches/';
var thumb_path='http://www.sonata.com.sg/image/data/site-images/option-thumbs/';

var fabric_option ='sl-fabric-option';

var body_option_id ='sl-body-option';
var panel_option_id='sl-panel-option';
var piping_option_id='sl-piping-option';
var secondary_option_id='sl-secondary-option';


var color_option_id='sl-color-option';
var option_panel = new Array();
var option_body = new Array();
var option_piping = new Array();
var option_secondary = new Array();
var option_color= new Array();

var body_target="colorbox-body";
var panel_target="colorbox-panel";
var piping_target="colorbox-piping";
var color_target="colorbox-body";
var secondary_target="colorbox-secondary";

var total_add_price=0;
var total_price=0;

var testing=0;

// Declare Functions



function capitaliseFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}

/*function attach_color_thumbnail (option_id){
	$(option_id).each(function(){

		$('option', this).each(function() {

	
			$(this).css('background-image',("url("+thumb_path+option_trimmer($(this).attr('class'))+'-'+option_trimmer($(this).text())+'.jpg'+")"));
			$(this).css('background-repeat','no-repeat');

			$(this).css('background-position','90% 50%');

		})
	
	});
}*/

function trim_price (option) {
	if(option){
	var price = new Array();
	price[0]=option.match(/[\+\-]/gi);
	price[1]=option.match(/[0-9]+[\.][0-9]+(?!\$)/gi);
	if(price[0]=='-') price[2]=-parseFloat(price[1]);
	else if(price[0]=='+') price[2]=parseFloat(price[1]);
	else return 0;
	
	return price[2];
	}
}

function option_trimmer(option){
	if(option){
	option=option.replace(/[\(\+\$0-9\)\.]/g,"");
	option=option.replace(/sl-/i,"");
	option=option.replace(/^\s+|\s+$/g,"");
	option=option.replace(/[\s]/g,"");
	option=option.toLowerCase();
	}
	return option;

}


function colorpicker(fabric,color,target) {
	var file_name="";
	file_name=swatch_path+fabric+"-"+color+".jpg";
	file_name="url("+file_name+")";	
	$("#"+target).css("background-image",file_name);

}

// This is the only function that removes away the options. Must be careful if NEW FABRIC TYPE is added.

function remove_options() {
$(".sl-cotton").remove();
$(".sl-chiffon").remove();
$(".sl-lycra").remove();
$(".sl-velvet").remove();

}

function show_selected_option(show_class) {
show_class=show_class.replace(/[\(\+\$0-9\)\.]/g,"");
show_class=show_class.replace(/sl-/i,"");
show_class=show_class.replace(/^\s+|\s+$/g,"");

if ($('#'+color_option_id).length) option_color[show_class].appendTo($('#'+color_option_id));
if ($('#'+panel_option_id).length) option_panel[show_class].appendTo($('#'+panel_option_id));
if ($('#'+piping_option_id).length) option_piping[show_class].appendTo($('#'+piping_option_id));
if ($('#'+secondary_option_id).length) option_secondary[show_class].appendTo($('#'+secondary_option_id));
if ($('#'+body_option_id).length) option_body[show_class].appendTo($('#'+body_option_id));	
}

// Main Script Program Body
$(document).ready(function() {

	/*$('select').children('option').change(function(i){

		if($(this).is(':selected')){

			total_add_price_select[i]=trim_price($(this).text());
		}
	});
	
	$('.options').find('input').change(
	
		$(this).each(function (i){
			total_add_price_input[i]=
		
		});
	
	
	
	});*/



	$('.options').children().change(function (){
	

		$('.options').find('input').each(function() {

			if($(this).is(':checked')){

				total_add_price+=trim_price($(this).next('label').text());

			}
		});
		
		$('.options').find('select').each(function() {
		
			
			if($(this).children('option').is(':selected')){
			
				total_add_price+=trim_price($(this).children('option:selected').text());
			
			}
	
		});
		total_price=(base_price+total_add_price)*parseInt($('input[name="quantity"]').val());
		$('#sl-price-with-options').text('$'+Math.round(total_price*100)/100);
		total_price=0;
		total_add_price=0;
		
	});
	
	$('input[name="quantity"]').change(function(){
		$('.options').find('input').each(function() {

			if($(this).is(':checked')){

				total_add_price+=trim_price($(this).next('label').text());

			}
		});
		
		$('.options').find('select').each(function() {
		
			
			if($(this).children('option').is(':selected')){
			
				total_add_price+=trim_price($(this).children('option:selected').text());
			
			}
	
		});
		total_price=(base_price+total_add_price)*parseInt($('input[name="quantity"]').val());
		$('#sl-price-with-options').text('$'+Math.round(total_price*100)/100);
		total_price=0;
		total_add_price=0;

	});
	
	if(
	$('#'+fabric_option).length 
	|| $('#'+color_option_id).length
	|| $('#'+panel_option_id).length
	|| $('#'+body_option_id).length
	|| $('#'+piping_option_id).length
	|| $('#'+secondary_option_id).length) {
		
		// Turns on the available color boxes
		if($('#'+color_option_id).length) {$('#'+body_target).text('Colour').css('display','block');} 
		if($('#'+panel_option_id).length) $('#'+panel_target).css('display','block');
		if($('#'+piping_option_id).length) $('#'+piping_target).css('display','block');		
		if($('#'+secondary_option_id).length) $('#'+secondary_target).css('display','block');									if($('#'+body_option_id).length) $('#'+body_target).css('display','block');
	
	    // Ensures that if only got 1 option such as color, colorbox got enough space. 
	    $('.options').css('min-height','150px');
	

	if($('#sl-fabric-option').length){
		
		if($('#'+color_option_id).length){ 
			option_color['lycra'] = $('#'+color_option_id+" option.sl-lycra").detach();
			option_color['chiffon'] = $('#'+color_option_id+" option.sl-chiffon").detach();
			option_color['cotton'] = $('#'+color_option_id+" option.sl-cotton").detach();	
			option_color['velvet'] = $('#'+color_option_id+" option.sl-velvet").detach();	
		}
		
		
		if($('#'+panel_option_id).length) {
			option_panel['lycra'] = $('#'+panel_option_id+" option.sl-lycra").detach();
			option_panel['chiffon'] = $('#'+panel_option_id+" option.sl-chiffon").detach();
			option_panel['cotton'] = $('#'+panel_option_id+" option.sl-cotton").detach();	
			option_panel['velvet'] = $('#'+panel_option_id+" option.sl-velvet").detach();	
		}
	
		if($('#'+body_option_id).length) {
			option_body['lycra'] = $('#'+body_option_id+" option.sl-lycra").detach();
			option_body['chiffon'] = $('#'+body_option_id+" option.sl-chiffon").detach();
			option_body['cotton'] = $('#'+body_option_id+" option.sl-cotton").detach();	
			option_body['velvet'] = $('#'+body_option_id+" option.sl-velvet").detach();	
		}	
	
		if($('#'+piping_option_id).length) {
			option_piping['lycra'] = $('#'+piping_option_id+" option.sl-lycra").detach();
			option_piping['chiffon'] = $('#'+piping_option_id+" option.sl-chiffon").detach();
			option_piping['cotton'] = $('#'+piping_option_id+" option.sl-cotton").detach();	
			option_piping['velvet'] = $('#'+piping_option_id+" option.sl-velvet").detach();	
		}	
		
		if($('#'+secondary_option_id).length){
			option_secondary['lycra'] = $('#'+secondary_option_id+" option.sl-lycra").detach();
			option_secondary['chiffon'] = $('#'+secondary_option_id+" option.sl-chiffon").detach();
			option_secondary['cotton'] = $('#'+secondary_option_id+" option.sl-cotton").detach();	
			option_secondary['velvet'] = $('#'+secondary_option_id+" option.sl-velvet").detach();	
		}
	}
			
	} else $('.colorbox').css('display','none');
	
	$('#'+fabric_option).change(function (){
		if($('#sl-fabric-option option:selected').val().length){
			$('#sl-fabric-box-a').attr('onclick','javascript:window.open("'+'http://www.sonata.com.sg/colorchart.php?material='+option_trimmer($('#'+fabric_option		+' option:selected').text())+'","colorchart","toolbar=0,status=0,width=750,height=550,scrollbars=1")').css('display','inline').text(capitaliseFirstLetter(option_trimmer($('#'+fabric_option		+' option:selected').text())+' Colours'));
		
					
			show_class=option_trimmer($("#"+fabric_option+" option:selected").text());
			remove_options();
			show_selected_option(show_class);
		}
		else{
			$('#sl-fabric-box-a').attr('href','').css('display','none').text('Select Fabric First');
		}  
		
		

	});

	$("#"+color_option_id).change(function (){
	colorpicker(
		option_trimmer($('#'+color_option_id +" option:selected").attr('class')),
		option_trimmer($("#"+color_option_id+"  option:selected").text()),
		color_target);
	});
	
	$("#"+secondary_option_id).change(function (){
	colorpicker(
		option_trimmer($('#'+secondary_option_id +" option:selected").attr('class')),
		option_trimmer($("#"+secondary_option_id+"  option:selected").text()),
		secondary_target);
	});

	

$("#"+panel_option_id).change(function (){
	colorpicker(
		option_trimmer($('#'+panel_option_id +" option:selected").attr('class')),
		option_trimmer($("#"+panel_option_id+"  option:selected").text()),
		panel_target
		);


});
   
$("#"+body_option_id).change(function (){
	colorpicker(
		option_trimmer($('#'+body_option_id +" option:selected").attr('class')),
		option_trimmer($("#"+body_option_id+"  option:selected").text()),
		body_target
		);


});   


$("#"+piping_option_id).change(function (){
	colorpicker(
		option_trimmer($('#'+piping_option_id +" option:selected").attr('class')),
		option_trimmer($("#"+piping_option_id+"  option:selected").text()),
		piping_target
		);


});

});
