/**
 * Catalog page js methods.
 *
 * @copyright 2009-2012 Liz Earle Beauty Co. All rights reserved.
 */
 
 
 /**
  * Declare LE namespace
  */
(function(lizearle) {
       
    //@Public 
    lizearle.colour = {
        
        /**
         * initialise range-page swatches. Handles range page modal reload.
         *       
         * @return void
         */
        initSwatches:function(){
            var elemid=jQuery('.added-product').attr('id');
            if(undefined != elemid){
                var selectedid=elemid.substring(elemid.indexOf('-')+1);
                jQuery('#range-swatch-' + selectedid).siblings().removeClass('selected');
                jQuery('#range-swatch-' + selectedid).addClass('selected');
                swatchimage = jQuery('#formulation-' + selectedid).html();
                jQuery('.products-grid .range-select').val(selectedid);	
            }
        },
        /**
         * Behaviour for the range-page swatches.
         *       
         * @return void
         */
        miniSwatch:function () {
					jQuery(this).click(function(event) {
						jQuery(this).siblings().removeClass('selected');
						jQuery(this).addClass('selected');	
                        var imageid = jQuery(this).find('div.formulation').attr('id');
						var prod_id = imageid.substring(imageid.indexOf('-')+1);
						jQuery(this).parents(".product-info").find(".range-select").val(prod_id);
					});
    	},
        /**
         * extend the select change behaviour to include the swatches.
         *       
         * @return void
         */
        bindSwatchSelect:function () {
					jQuery(this).change(function(event) {
						imageid = jQuery(this).val();
						jQuery(this).parents(".product-info").find('li.range-swatch').removeClass('selected');
						jQuery('#range-swatch-' + imageid).addClass('selected');
					  });
        } 
    };
     
}( window.lizearle = window.lizearle || {}));


function changeQsValue(textObject, delta) {
        var myVal = parseInt(textObject.value);
        if (myVal == NaN) {
                myVal = 0;
        } else {
                myVal = myVal + delta;
        }
        /* check that the quantity value is not negative */
        if (myVal < 0) {
                myVal = 0;
        }
        // hack to enable qty limits on qs pages, since they still contain their
		// own version of embeded qty blocks until markup is fixed
        // due to markup issues on that page(s) the jquery event is triggered 14
		// times on one click !
		if (myVal > 99) {
			myVal = 99;
		}
        textObject.value = myVal;
        return;
}

function registerVideoView(videoID){
	if(("undefined" !== typeof window.YWATracker)){
		var NT = YWA.getTracker(window.YWATracker.PID);
		NT.setAction("06");
		NT.submit_action();
	}
}

function changeValue(textObject, delta) {
    var myVal = parseInt(textObject.value);
    if (myVal == NaN) {
            myVal = 0;
    } else {
            myVal = myVal + delta;
    }
     // check that the quantity value is not negative
    if (myVal < 1) {
            myVal = 1;
    }
    textObject.value = myVal;
    return;
}

jQuery.noConflict();
jQuery(document)
		.ready(
				function() {
					jQuery(".quantity-box").focusout(function() {
						var myVal = parseInt(jQuery(this).val());
						/* check that the quantity value is not negative */
						if (myVal < 0) {
							jQuery(this).val(0);
						}
						/* check that the max quantity value is not reached */
						if (myVal > maxqty) {
							jQuery(this).val(maxqty);
						}
					});

					
					jQuery('select').each(function(index) {
						// alert(index +': '+jQuery(this).val());
							if (!jQuery(this).val()) {

								var filter = jQuery(this).get(0).id;
								group = filter.substring(23, 27);
								jQuery('#out-of-stock_' + group).show();
								jQuery('#add_btn_' + group).hide();
								jQuery('#qty-label_' + group).hide();
								jQuery('#addtocart_form_' + group).hide();
								jQuery('#quantity_btns_' + group).hide();
								jQuery('#qty_box_' + group).hide();
								// jQuery('#add_btn').hide();
							}
						});

					jQuery('select').click(function() {

						if (!jQuery(this).val()) {
							var filter = jQuery(this).get(0).id;
							group = filter.substring(23, 27);
							jQuery('#out-of-stock_' + group).show();
							jQuery('#add_btn_' + group).hide();
							jQuery('#qty-label_' + group).hide();
							jQuery('#addtocart_form_' + group).hide();
							jQuery('#quantity_btns_' + group).hide();
							jQuery('#qty_box_' + group).hide();
						} else {
							var filter = jQuery(this).get(0).id;
							group = filter.substring(23, 27);
							if (jQuery(this).val()) {
								jQuery('#out-of-stock_' + group).hide();
								jQuery('#add_btn_' + group).show();
								jQuery('#qty-label_' + group).show();
								jQuery('#addtocart_form_' + group).show();
								jQuery('#quantity_btns_' + group).show();
								jQuery('#qty_box_' + group).show();
								jQuery('.products-grid #qty_box_' + group).hide();
							}
						}
					});

					var clicks = 0;
					jQuery(".zoom")
							.click(
									function(event) {
										if (clicks == 0)
											setTimeout(
													function() {
														if (clicks == 1) {
															if (event.target.id != "") {
																var testClick = event.target.id
																		.split("-");
																if (testClick[0] == 'imagemodal'
																		|| testClick[0] == 'image') { // only
																	// if
																	// the
																	// modal
																	// a
																	// href
																	// was
																	// clicked.

																	jQuery
																			.ajax( {
																				type : "POST",
																				url : "/CatalogProduct/Modal/largeImageModal/",
																				data : {
																					id : testClick[1]
																				},
																				success : function(
																						result) {

																					jQuery(
																							'#largeimage-modal-ajax')
																							.html(
																									result);
																					jQuery(
																							'#largeimage-modal-ajax')
																							.hide();
																					jQuery(
																							'#largeimage-modal-ajax')
																							.modal(
																									{
																									position : [
																												"15%",
																												"25%" ], 
																									closeHTML:"", 
																									overlayClose:true
																									});
																					// $('loading-whatisthis-modal').hide();
																					clicks = 0;
																				}
																			});
																}
																return false;
															}
														} else {
															clicks = 0;
															return false;
														}
														clicks = 0;
													}, 250);
										clicks++;
										return false;
									});

					jQuery('#send').click(function(event) {

						jQuery.ajax( {

							type : "GET",
							url : this.href,
							success : function(result) {

								jQuery('#largeimage-modal-ajax').html(result);
								jQuery('#largeimage-modal-ajax').hide();
								jQuery('#largeimage-modal-ajax').modal({closeHTML:"", overlayClose:true});
								// $('loading-whatisthis-modal').hide();

						}
						});
						return false;
					});
		

					jQuery('#sendfriend').live("click", function(event) {

						jQuery('#sendfriend').addClass("current");
						jQuery('#share').removeClass("current");
						jQuery('#share-tab').hide();
						jQuery('#send-tab').show();
						return false;
					});
					jQuery('#share').live("click", function(event) {

						jQuery('#share').addClass("current");
						jQuery('#sendfriend').removeClass("current");
						jQuery('#send-tab').hide();
						jQuery('#share-tab').show();
						return false;
					});

					jQuery('div.swatch-tooltip:first').show();

					jQuery('.swatch').click(function(event) {
						jQuery('.swatches').find('li.swatch').removeClass('selected');
						jQuery(this).addClass('selected');	
						jQuery('div.swatch-tooltip').hide();
						jQuery(this).find('div.swatch-tooltip').show();
						swatchimage = jQuery(this).find('div.formulation').html();
						imageid = jQuery(this).find('div.formulation').attr('id');
						jQuery('#texture').html(swatchimage);
						id = imageid.split("-");

						jQuery('.range-select').val(id[1]);
					});
					
					if(jQuery('.added-product').attr('id')){
						productid=jQuery('.added-product').attr('id');
						id=productid.split("-");
						selectedid=id[1];
						jQuery('.swatches').find('li.swatch').removeClass('selected');
						jQuery('#swatch-' + selectedid).addClass('selected');
						jQuery('div.swatch-tooltip').hide();
						jQuery('#swatch-'+selectedid).find('div.swatch-tooltip').show();
						swatchimage = jQuery('#formulation-' + selectedid).html();

						// imageid=jQuery(this).find('div.formulation').attr('id');
							jQuery('#texture').html(swatchimage);
							jQuery('.range-select').val(selectedid);
							
						
					}
					
					
					
					jQuery('.range-select').change(function(event) {
						imageid = jQuery(".range-select").val();

						jQuery('.swatches').find('li.swatch').removeClass('selected');
						jQuery('#swatch-' + imageid).addClass('selected');
						jQuery('div.swatch-tooltip').hide();
						jQuery('#swatch-'+imageid).find('div.swatch-tooltip').show();
						swatchimage = jQuery('#formulation-' + imageid).html();

						// imageid=jQuery(this).find('div.formulation').attr('id');
							jQuery('#texture').html(swatchimage);
							id = imageid.split("-");

						});
						
			/* Range Page Colour Swatches */
            
            lizearle.colour.initSwatches();
            
            // miniSwatch behaviour
			jQuery('.range-swatches li').each(lizearle.colour.miniSwatch);	
			  
            // bind the swatches to the group select
            jQuery('.products-grid .range-select').each(lizearle.colour.bindSwatchSelect);	
					
				
					
					
					var clicks = 0;
					jQuery(".video").click(function(event) {
						
						if (clicks == 0)setTimeout(function() {
							if (clicks == 1) {
								if (event.target.id != "") {
									var testClick = event.target.id.split("-");
									if (testClick[0] == 'videomodal'|| testClick[0] == 'image') { 
										// only if the modal a
										// href was clicked.
										jQuery.ajax( {
											type : "GET",
											url : "/CatalogProduct/Modal/videoModal/id/"+testClick[1],
										success : function(result) {
											jQuery('#video-modal-ajax').html(result);
											jQuery('#video-modal-ajax').hide();
											jQuery('#video-modal-ajax').modal({closeHTML:"", overlayClose:true});
											clicks = 0;
											registerVideoView(testClick[1]);
										}
										
									});
										
								}
								return false;
							}
						} else {
							clicks = 0;
							return false;
						}
							clicks = 0;
						}, 250);
						clicks++;
						return false;
					});
					
					jQuery('#colour-accuracy').click(function(event){
						jQuery('#colour-statement').html();
						jQuery('#colour-statement').hide();
						jQuery('#colour-statement').modal({closeHTML:"", overlayClose:true});
					});
				});
				
		
		

jQuery('#sendtofriend-submit').live("click", function(event) {

	dataString = jQuery("#product_sendtofriend_form").serialize();
	formAction = jQuery("#product_sendtofriend_form").attr('action');

	jQuery.ajax( {
		type : "POST",
		url : formAction,
		data : dataString,
		success : function(result) {

			jQuery('#send-message').html(result);

		}
	});

	return false;

});

