/*
 *
 * Copyright (c) 2008 Pixel Motion Motors (http://www.pixelmotionmotors.com)
 *
 */

 var DEBUG;

 if(window.location.href.indexOf('debug=1') != -1){
	 DEBUG = 1;
 }

(function($) {
	$.CP = {
	    hello : function() { alert('hello wolrd'); return this; },

		sendToConsole: function(msg){
	        if(window.console && DEBUG) {
	            if($.browser.mozilla){
	                console.info(msg);
	            }
	        }
	    },

		clearFromOrder: function(k){
			this.orderData[k] = null;
			//this.printOrder();
		},

		order:{info:{},hair:{},gray:{},highlight:{},density:{},curl:{},other:{}},
		sendRequest:{username:'',useremail:'',receipt:''},

		sendOrder: function(){
			// Get Receipt Page
			this.sendRequest.receipt = $('#receipt').html();
			this.sendRequest.username = this.order.info.cp_orderInfo_nameOfWearer.value;
			this.sendRequest.useremail = this.order.info.cp_orderInfo_emailAddress.value;

			var settings = {
	            type: "POST",
	            data: this.sendRequest,
				dataType: "text",
	            url: '/orderForm/post/',
                   success: function(data){
                       $.CP.success_cb(data);
                   }
	        };

			this.doCall(settings);

		},

		success_cb: function(data){
			$('#submit').val('Completed...');
			alert('Email Has Been Sent');
		},

		doCall: function(settings){
	        var call = {
	            type: "GET",
	            data: null,
	            dataType: "json",
	            cache:false,
	            success: function(data){
	                return data;
	            },
	            error: function(XMLHttpRequest, textStatus, errorThrown){
	                return textStatus;
	            }
	        };

	        $.extend(call,settings);

	        $.ajax(call);
	    },

		printOrder: function(){
            $('#receipt').html(''); // clear any existing;


			var allInputs = $(':input');
			var info = [];
			var hair = [];
			var gray = [];
			var highlight = [];
			var density = [];
			var curl = [];

			if(allInputs.length > 0){
				for (input in allInputs) {
					if ((allInputs[input].id != undefined) && (allInputs[input].id.indexOf('cp_') > -1) && (allInputs[input].value != '')) {
						if (allInputs[input].title != 'ignore') {

							if (((allInputs[input].type == 'radio') && (allInputs[input].checked)) || (allInputs[input].type != 'radio')) {

								if (allInputs[input].id.indexOf('cp_orderInfo') > -1) { // Order Info
									if (allInputs[input].type == 'radio') {
									    info[allInputs[input].name] = allInputs[input];
									}
									else {
										info[allInputs[input].id] = allInputs[input];
									}
								}
								else if(allInputs[input].id.indexOf('cp_hair') > -1){
									if (allInputs[input].type == 'radio') {
									    hair[allInputs[input].name] = allInputs[input];
									}
									else {
										hair[allInputs[input].id] = allInputs[input];
									}
								}
								else if(allInputs[input].id.indexOf('cp_gray') > -1){
									if (allInputs[input].type == 'radio') {
									    gray[allInputs[input].name] = allInputs[input];
									}
									else {
										gray[allInputs[input].id] = allInputs[input];
									}
								}
								else if(allInputs[input].id.indexOf('cp_highlight') > -1){
									if (allInputs[input].type == 'radio') {
									    highlight[allInputs[input].name] = allInputs[input];
									}
									else {
										highlight[allInputs[input].id] = allInputs[input];
									}
								}
								else if(allInputs[input].id.indexOf('cp_density') > -1){
									if (allInputs[input].type == 'radio') {
									    density[allInputs[input].name] = allInputs[input];
									}
									else {
										density[allInputs[input].id] = allInputs[input];
									}
								}
								else if(allInputs[input].id.indexOf('cp_curl') > -1){
									if (allInputs[input].type == 'radio') {
									    curl[allInputs[input].name] = allInputs[input];
									}
									else {
										curl[allInputs[input].id] = allInputs[input];
									}
								}
							}
						}
					}
				}

				this.order.info = info;
				/*this.order.hair = hair;
				this.order.gray = gray;
				this.order.highlight = highlight;
				this.order.density = density;
				this.order.curl = curl;*/

				this.printOrder_OrderInfo(info);
				this.printOrder_Hair(hair);
				this.printOrder_Gray(gray);
				this.printOrder_Highlight(highlight);
				this.printOrder_Density(density);
				this.printOrder_Curl(curl);

				if($('#cp_notes').val() != ''){
				    $('#receipt').append('<div class="order"><p>Additional Notes:</p><blockquote>' + $('#cp_notes').val().replace("\n",'<br />') + '</blockquote></div>');
				}

            }
            else{
                this.sendToConsole('Current Order: is empty');
            }
		},

		printOrder_OrderInfo: function(array)
		{
			var size;
			if(array['cp_orderInfo_exactSize'].value.indexOf('User Provided,') == -1){
				size = array['cp_orderInfo_exactSize'].value;
			}
			else if((array['cp_orderInfo_exactSize'] != undefined) && (array['cp_orderInfo_exactSize'].value == 'User Provided, Length/Width/Frontal')){
				size  = 'L: ' + array['cp_orderInfo_exactSizeLength'].value + ', ';
				size += 'W: ' + array['cp_orderInfo_exactSizeWidth'].value + ', ';
				size += 'F: ' + array['cp_orderInfo_exactSizeFrontal'].value;
			}
			else if((array['cp_orderInfo_exactSize'] != undefined) && (array['cp_orderInfo_exactSize'].value == 'User Provided, Measurements')){
				size  = '#1: ' + array['cp_orderInfo_exactMeasure1'].value +' mm, ';
				size += '#2: ' + array['cp_orderInfo_exactMeasure2'].value +' mm, ';
				size += '#3: ' + array['cp_orderInfo_exactMeasure3'].value +' mm, ';
				size += '#4: ' + array['cp_orderInfo_exactMeasure4'].value +' mm, ';
				size += '#5: ' + array['cp_orderInfo_exactMeasure5'].value +' mm, ';
				size += '#6: ' + array['cp_orderInfo_exactMeasure6'].value +' mm, ';
				size += '#7: ' + array['cp_orderInfo_exactMeasure7'].value +' mm';
			}

			var sample;

			var t = $.template($('#html_orderInfo').html());
			$('#receipt').append( t , {
				name: array['cp_orderInfo_nameOfWearer'].value,
				email: array['cp_orderInfo_emailAddress'].value,
				units: array['cp_orderInfo_numberOfUnits'].value,
				category: array['cp_orderInfo_sizeCategory'].value,
				size: size,
				samples: array['cp_orderInfo_keepReturn'].value,
				rush: array['cp_orderInfo_rushProd'].value
				});
		},

		printOrder_Hair: function(array)
		{
			var base;
			if (array['cp_hair_baseDesign'].value.indexOf('User Provided,') == -1){
				base = array['cp_hair_baseDesign'].value;
			}
			else{
				base = '<blockquote>' + array['cp_hair_userCombo'].value.replace("\n",'<br />') + '</blockquote>';
			}

			var color;
			if (array['cp_hair_hairColor'].value.indexOf('User Provided,') == -1){
				color = array['cp_hair_hairColor'].value;
			}
			else{
				color = '<blockquote>' + array['cp_hair_userColor'].value.replace("\n",'<br />') + '</blockquote>';
			}

			var style;
			if (array['cp_hair_style'].value.indexOf('See notes') == -1){
				style = array['cp_hair_style'].value;
			}
			else{
				style = '<blockquote>' + array['cp_hair_userStyle'].value.replace("\n",'<br />') + '</blockquote>';
			}

			var t = $.template($('#html_hair').html());
			$('#receipt').append( t , {
				length: array['cp_hair_hairLength'].value,
				base: base,
				color: color,
				type: array['cp_hair_typeOf'].value,
				style: style
				});
		},

		printOrder_Gray: function(array){
			var gray;
			if (array['cp_gray_type'].value.indexOf('No gray') > -1){
				gray = 'No gray';
			}
			else{
				gray = array['cp_gray_type'].value + '<br />';

				if(array['cp_gray_select_whole_partial'].value == 'Whole'){
					gray += 'Whole Unit: ' + array['cp_gray_whole_percentage'].value;
				}
				else{
					gray += 'Front: ' + array['cp_gray_partial_front_per'].value + ', ';
					gray += 'Temple: ' + array['cp_gray_partial_temples_per'].value + ', ';
					gray += 'Top: ' + array['cp_gray_partial_top_per'].value + ', ';
					gray += 'Side: ' + array['cp_gray_partial_sides_per'].value + ', ';
					gray += 'Crown: ' + array['cp_gray_partial_crown_per'].value + ', ';
					gray += 'Back: ' + array['cp_gray_partial_back_per'].value;
				}
			}

			var t = $.template($('#html_gray').html());
			$('#receipt').append( t , {gray: gray});
		},

		printOrder_Highlight: function(array){
			var highlight;
			if (array['cp_highlight'].value.indexOf('No highlights') > -1){
				highlight = 'No highlights';
			}
			else{
				highlight = array['cp_highlight'].value + '<br />';

				if(array['cp_highlight_select_whole_partial'].value == 'Whole'){
					highlight += 'Whole Unit: ' + array['cp_highlight_whole_percentage'].value;
				}
				else{
					highlight += 'Front: ' + array['cp_highlight_partial_front_per'].value + ', ';
					highlight += 'Temple: ' + array['cp_highlight_partial_temple_per'].value + ', ';
					highlight += 'Top: ' + array['cp_highlight_partial_top_per'].value + ', ';
					highlight += 'Side: ' + array['cp_highlight_partial_sides_per'].value + ', ';
					highlight += 'Crown: ' + array['cp_highlight_partial_crown_per'].value + ', ';
					highlight += 'Back: ' + array['cp_highlight_partial_back_per'].value;
				}

				if(array['cp_highlight_highlightColor'].value.indexOf('User Provided') == -1){
				    highlight += '<br /><label>Highlight Color</label>:' + array['cp_highlight_highlightColor'].value;
				}
				else{
					highlight += '<blockquote>' + array['cp_highlight_userHighlightColor'].value.replace("\n",'<br />') + '</blockquote>';
				}
			}

			var t = $.template($('#html_highlight').html());
			$('#receipt').append( t , {highlight: highlight});
		},

		printOrder_Density: function(array){
            var density;
            if (array['cp_density_select_whole_partial'].value == 'Whole') {
                density = 'Whole Unit: ' + array['cp_density_whole_percentage'].value;
            }
            else {
                density = 'Front: ' + array['cp_density_partial_front_per'].value + ', ';
                density += 'Temple: ' + array['cp_density_partial_temples_per'].value + ', ';
                density += 'Top: ' + array['cp_density_partial_top_per'].value + ', ';
                density += 'Side: ' + array['cp_density_partial_sides_per'].value + ', ';
                density += 'Crown: ' + array['cp_density_partial_crown_per'].value + ', ';
                density += 'Back: ' + array['cp_density_partial_back_per'].value;
            }

			if(array['cp_density_notes'] != undefined){
				density += '<blockquote>' + array['cp_density_notes'].value.replace("\n",'<br />') + '</blockquote>';
			}

            var t = $.template($('#html_density').html());
            $('#receipt').append(t, {density: density});
		},

		printOrder_Curl: function(array){
			var curl;
            if (array['cp_curl_select_whole_partial'].value == 'Whole') {
                curl = 'Whole Unit: ' + array['cp_curl_whole_percentage'].value;
            }
            else {
                curl = 'Front: ' + array['cp_curl_partial_front_per'].value + ', ';
                curl += 'Temple: ' + array['cp_curl_partial_temples_per'].value + ', ';
                curl += 'Top: ' + array['cp_curl_partial_top_per'].value + ', ';
                curl += 'Side: ' + array['cp_curl_partial_sides_per'].value + ', ';
                curl += 'Crown: ' + array['cp_curl_partial_crown_per'].value + ', ';
                curl += 'Back: ' + array['cp_curl_partial_back_per'].value;
            }

			if(array['cp_curl_notes'] != undefined){
				curl += '<blockquote>' + array['cp_curl_notes'].value.replace("\n",'<br />') + '</blockquote>';
			}

            var t = $.template($('#html_curl').html());
            $('#receipt').append(t, {curl: curl});
		},

		checkExactSize: function(val){
			this.sendToConsole('checkExactSize: ' + val);

		    if(val == 'User Provided, Measurements'){
				$('#orderInfo_measurements').show('slow');
                $('#orderInfo_contour').hide();
			}
            else if(val == 'User Provided, Length/Width/Frontal'){
                $('#orderInfo_contour').show('slow');
                $('#orderInfo_measurements').hide();
            }
			else{
				$('#orderInfo_contour').hide();
				$('#orderInfo_measurements').hide();
			}
		},

        checkBaseDesign: function(val){
            this.sendToConsole('checkBaseDesign: ' + val);

            if(val == 'User Provided, Combination'){
                $('#hair_baseDesign_combo').show('slow');
            }
            else{
                $('#hair_baseDesign_combo').hide();
                $('#cp_hair_userCombo').val('');
            }
        },

        checkHairColor: function(val){
            this.sendToConsole('checkHairColor: ' + val);

            if(val == 'User Provided, Color'){
                $('#hair_hairColor_user').show('slow');
            }
            else{
                $('#hair_hairColor_user').hide();
                $('#cp_hair_userColor').val('');
            }
        },

        checkForGray: function(val){
            this.sendToConsole('checkForGray: ' + val);

            if((val != 'No gray') && (val != '')){
                $('#gray_percentage').show('slow');
                $('#gray_whole_partial').show('slow');
            }
            else{
                $('#gray_percentage').hide();
                $('#gray_whole_partial').hide();
            }
        },

        checkForGrayWholePartial: function(val){
            this.sendToConsole('checkForGrayWholePartial: ' + val);

            if (val != '') {
                if (val == 'Whole') {
                    $('#gray_percentage_whole').show('slow');
                    $('#gray_percentage_partial').hide();
                }
                else {
                    $('#gray_percentage_partial').show('slow');
                    $('#gray_percentage_whole').hide();
                }
            }
            else{
                $('#gray_percentage_partial').hide();
                $('#gray_percentage_whole').hide();
            }
        },

        checkForStyle: function(val){
            this.sendToConsole('checkForStyle: ' + val);

            if(val == 'See notes'){
                $('#hair_userStyle').show('slow');
            }
            else{
                $('#hair_userStyle').hide();
            }
        },

        checkForHighlight: function(val){
            this.sendToConsole('checkForHighlight: ' + val);

            if((val != 'No highlights') && (val != '')){
                $('#highlight_percentage').show('slow');
                $('#highlight_whole_partial').show('slow');
            }
            else{
                $('#highlight_percentage').hide();
                $('#highlight_whole_partial').hide();
            }
        },

        checkForHighlightsWholePartial: function(val){
            this.sendToConsole('checkForHighlightsWholePartial: ' + val);

            if (val != '') {
                if (val == 'Whole') {
                    $('#highlight_percentage_whole').show('slow');
                    $('#highlight_percentage_partial').hide();
                }
                else {
                    $('#highlight_percentage_partial').show('slow');
                    $('#highlight_percentage_whole').hide();
                }
            }
            else{
                $('#highlight_percentage_partial').hide();
                $('#highlight_percentage_whole').hide();
            }
        },

        checkForHighlightColor: function(val){
            this.sendToConsole('checkForHighlightColor: ' + val);

            if (val == 'User Provided') {
                $('#highlight_userHighlightColor').show('slow');
            }
            else{
                $('#highlight_userHighlightColor').hide();
                $('#cp_highlight_userHighlightColor').val('');
            }
        },

        checkForDensityWholePartial: function(val){
            this.sendToConsole('checkForDensityWholePartial: ' + val);

            if (val != '') {
                if (val == 'Whole') {
                    $('#density_percentage').show('slow');
                    $('#density_percentage_whole').show('slow');
                    $('#density_percentage_partial').hide();
                }
                else {
                    $('#density_percentage').show('slow');
                    $('#density_percentage_partial').show('slow');
                    $('#density_percentage_whole').hide();
                }
            }
            else{
                $('#density_percentage').hide('slow');
                $('#density_percentage_partial').hide();
                $('#density_percentage_whole').hide();
            }
        },

        checkForCurlWholePartial: function(val){
            this.sendToConsole('checkForCurlWholePartial: ' + val);

            if (val != '') {
                if (val == 'Whole') {
                    $('#curl_percentage').show('slow');
                    $('#curl_percentage_whole').show('slow');
                    $('#curl_percentage_partial').hide();
                }
                else {
                    $('#curl_percentage').show('slow');
                    $('#curl_percentage_partial').show('slow');
                    $('#curl_percentage_whole').hide();
                }
            }
            else{
                $('#curl_percentage').hide('slow');
                $('#curl_percentage_partial').hide();
                $('#curl_percentage_whole').hide();
            }
        }

    } // closing $.CP
})(jQuery); // closing function

$(document).ready(function(){
    if($.isFunction('tabs')){
	    $("#example > ul").tabs();
	}

	$('.goToTab').click(
		function(){
			var t = $(this).attr('rel');
			$("#example > ul").tabs('select',t);
	    }
	);

	$('#cp_orderInfo_exactSize').bind('change',
		function(){
		    $.CP.checkExactSize($(this).val());
	    }
	);

    $('#cp_hair_baseDesign').bind('change',
        function(){
            $.CP.checkBaseDesign($(this).val());
        }
    );

    $('#cp_hair_hairColor').bind('change',
        function(){
            $.CP.checkHairColor($(this).val());
        }
    );

    $('#cp_gray_type').bind('change',
        function(){
            $.CP.checkForGray($(this).val());
        }
    );

    $('#cp_gray_whole_partial').bind('change',
        function(){
            $.CP.checkForGrayWholePartial($(this).val());
        }
    );

    $('#cp_gray_select_whole_partial').bind('change',
        function(){
            $.CP.checkForGrayWholePartial($(this).val());
        }
    );

    $('#cp_hair_style').bind('change',
        function(){
            $.CP.checkForStyle($(this).val());
        }
    );

    $('#cp_highlight').bind('change',
        function(){
            $.CP.checkForHighlight($(this).val());
        }
    );

    $('#cp_highlight_select_whole_partial').bind('change',
        function(){
            $.CP.checkForHighlightsWholePartial($(this).val());
        }
    );

    $('#cp_highlight_highlightColor').bind('change',
        function(){
            $.CP.checkForHighlightColor($(this).val());
        }
    );

    $('#cp_density_select_whole_partial').bind('change',
        function(){
            $.CP.checkForDensityWholePartial($(this).val());
        }
    );

    $('#cp_curl_select_whole_partial').bind('change',
        function(){
            $.CP.checkForCurlWholePartial($(this).val());
        }
    );


});