//Product Gallery related functions

function switch_image(image_link_obj){
	if (current_link_idx == image_link_obj.parent().attr('tabIndex'))
				return false;
	image_url = image_link_obj.attr('href');
	$('div#gallery_script_picts .big_pict').fadeOut('slow',
		function (){
			if (urls_cache[image_url] == undefined){
				urls_cache[image_url] = 1;
	 				//Let's create an img so the browser will download the image:
				$('<img />').attr('src', image_url)
			    .load(function(){ //attach onload to set background-image
			       $('div#gallery_script_picts .big_pict').css('background', 'url('+image_url+')' );
			       $('div#gallery_script_picts .big_pict').fadeIn('slow');
			    });
			    return false;
			}
			$('div#gallery_script_picts .big_pict').css('background', 'url('+image_url+')' );
			$('div#gallery_script_picts .big_pict').fadeIn('slow');
		});
	$('div#gallery_script_picts .product_galery_pict').css('opacity', '0.4');
	$('div#gallery_script_picts .product_galery_pict').css('filter', 'alpha(opacity=40)');
	image_link_obj.parent().css('opacity', '1');
	image_link_obj.parent().css('filter', 'alpha(opacity=100)');
	$('div#gallery_script_picts .caption_text div').text(image_link_obj.next().text());
	current_link_idx = image_link_obj.parent().attr('tabIndex');
	return false;
}


function switch_on_timer(){
	total_images = $('div#gallery_script_picts .product_galery_pict_link').length;
	new_current_link_idx = current_link_idx;
	new_current_link_idx++;
	if (new_current_link_idx >= total_images)
		new_current_link_idx =0;
	switch_image($('div#gallery_script_picts .product_galery_pict_link:nth('+new_current_link_idx+')'));
	gallery_timer = setTimeout("switch_on_timer()", timer_inderval);

}

function init_product_gallery(){
	if (!($('div#gallery_script').length && $('div#gallery_script_picts').length))
		return;
	$('div#gallery_script .product_gallery_entry').click(function() {
			$('div#gallery_script').slideUp("slow");
			$('div#gallery_script_picts').slideDown("slow");
			gallery_timer = setTimeout("switch_on_timer()", timer_inderval);
			return false;
	});

	$('div#gallery_script_picts .main_menu').click(function() {
			$('div#gallery_script').slideDown("slow");
			$('div#gallery_script_picts').slideUp("slow");
			clearTimeout(gallery_timer);
			return false;
	});



	if ($('div#gallery_script_picts .product_galery_pict_link').length){
		$('div#gallery_script_picts .product_galery_pict_link').each(function(index){
			image_url = $(this).attr('href');
			$('<img />').attr('src', image_url);
			urls_cache[image_url] = 1;
			$(this).parent().attr('tabIndex', index);
			//alert(index);
		});


		$('div#gallery_script_picts .product_galery_pict_link').click(function(){
			clearTimeout(gallery_timer);
			return switch_image($(this));
		});
		$('div#gallery_script_picts .product_galery_pict').hover(
			function(){
				$(this).css('opacity', '1');
				$(this).css('filter', 'alpha(opacity=100)');
			},
			function(){
				if (current_link_idx == $(this).attr('tabIndex'))
					return;
				$(this).css('opacity', '0.4');
				$(this).css('filter', 'alpha(opacity=40)');
			}
		)
		$('div#gallery_script_picts .big_pict')
		.css('background', 'url('+$('div#gallery_script_picts .product_galery_pict_link').attr('href')+')')
		.hover(
			function(){
				$(this).find('.product_gallery_caption').fadeIn('slow');
			},
			function(){
				$(this).find('.product_gallery_caption').fadeOut('slow');
			}
		)

		//product_gallery_caption
		$('div#gallery_script_picts .product_galery_pict:first').css('opacity', '1');
		$('div#gallery_script_picts .product_galery_pict:first').css('filter', 'alpha(opacity=100)');
		$('div#gallery_script_picts .caption_text div').text($('div#gallery_script_picts .product_galery_pict_link:first').next().text());
	}


}

function init_shadow() {
	if ($("span.shadow").length && $("span.shadow").dropShadow) {
		$("span.shadow").dropShadow({
			left: 2,
			top: 2,
			blur: 1,
			opacity: .9,
			color: "black",
			swap: false
		});
	}
	if ($("span.shadow_big").length && $("span.shadow_big").dropShadow) {
		$("span.shadow_big").dropShadow({
			left: 1,
			top: 1,
			blur: 1,
			opacity: .8,
			color: "black",
			swap: true
		});
	}
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	result = result + '';
	var parts = result.split('.');
	parts_1 = parts[1] + '';
	if (parts_1.length < dec) {
		var str = '';
		for (var i = 0; i < (dec - parts_1.length); i++) {
			str+='0';
		}
		parts_1+=str;
	}
	result = parts[0] + '.' + parts_1;
	return result;
}

var current_link_idx = 0;
var timer_inderval = 7000;
var urls_cache = new Array();
var gallery_timer = null;
///////////////////////////////////

function init_golf_offer(){
	if (!$('FORM#add2cart_offer').length)
		return;
	
	if (!$('.offer_products_dd').length)
		return;
	
	$('#add2cart_offer').submit(
		function(){
			if ($(".offer_products_dd[value='']").length){
				alert('Please select at least one product');
				return false;
			}
			
			if (($('#DD1').length && $('#DD2').length) && ($('#DD1').attr('value') == $('#DD2').attr('value'))){
				alert('Please select two different products');
				return false;
			}
			for (i=0; i<$('.offer_products_dd').length; i++){
				dd_obj = $($('.offer_products_dd').get(i));
				selected_value = dd_obj.attr('value');
				if (selected_value==''){
					alert('Please select a product');
					return false;
				}
				selected_ID = dd_obj.attr('ID');
				if (!$('#PRODUCT_'+selected_ID).length){
					alert('Products field is missing!');
					return false;
				}
				values = selected_value.split('--'); 
				SKU = values[0];
				for (j=1; j<values.length; j+=2){
					if (!$('#PRODUCT_'+selected_ID).length)
						continue;
					if (!$('#PRODUCT_OPTION'+j+'_'+selected_ID).length)
						continue;
					
					if ((Number(j)+1) >= values.length)
						continue;
					opt_name = values[j];
					opt_value = values[j+1];
					$('#PRODUCT_'+selected_ID).attr('name', "PRODUCTS[" + SKU + "][]");
					$('#PRODUCT_OPTION'+j+'_'+selected_ID).attr('name', 'OPTIONS['+SKU+'][]['+opt_name+']');
					$('#PRODUCT_OPTION'+j+'_'+selected_ID).attr('value', opt_value);
				}
			}
			
		}
	);
}


function set_installments_text(){
	subtotal = Number($('#SUB_TOTAL_REAL_VALUE').text()).toFixed(2);
	txt = $('#INSTALLMENTS').attr('checked')?Number(subtotal/2).toFixed(2) + ' X 2' : subtotal;
	$('#SUBTOTAL_SPAN').text(txt);
}

function init_view_cart(){
	if (!$('#INSTALLMENTS').length)
		return;
	if (!$('#SUB_TOTAL_REAL_VALUE').length)
		return;
	if (!$('#SUBTOTAL_SPAN').length)
		return;
	$('#INSTALLMENTS').click(
		function(){
			set_installments_text();
		}
	);
	
	if ($('#INSTALLMENTS').attr('checked'))
		set_installments_text();
		
	
	if ($("#DISABLE_MAGOFFER").length){
		$("#DISABLE_MAGOFFER").click(function () {
			if (!$('#FIELD_Q_GOLFFITNESS').length){
				$('#GOLFFITNESS_MAGAZINE').attr('disabled',(this.checked?0:1));
				return;
			}
			$('#Q_GOLFFITNESS').attr('value',(this.checked?0:1));
			$('#FIELD_Q_GOLFFITNESS').attr('value',(this.checked?0:1));
		} );
	}
	
	if ($("#G2F_Color").length && $('#G2F_Size').length && $('#G2F_COLOR_VALUE').length && $('#G2F_SIZE_VALUE').length){
		$("#G2F_Color").attr('value', $('#G2F_COLOR_VALUE').text());
		$("#G2F_Size").attr('value', $('#G2F_SIZE_VALUE').text());
	}


}

//main jquery hook-up function
$(document).ready(
	function() {
		init_product_gallery();
		init_view_cart();
		init_shadow();
		init_golf_offer();
		//init_offer1();
		//init_offer2();
	}
);