$(document).ready(function() {
	$('a.p_lb').jqzoom();
	//$('a.p_lb').lightBox();
	$('a.p_lb_open').lightBox();
	$('a.m_lb_open').click(function() {
		//var new_thumb = $("a.p_lb img").attr("src").replace("/medium/","/thumbs/");
		var new_main = $(this).find("img").attr("src").replace("/thumbs/","/medium/");
		var new_main_big = new_main.replace("/medium/","/");
		//$(this).find("img").attr("src",new_thumb);
		$("a.p_lb").attr("href",new_main_big).find("img").attr("src",new_main);
		$("a.p_lb_open").attr("href",new_main_big).find("img").attr("src",new_main);
	});
	
	bind_group_switch();
	bind_product_group_grid_hover();
});

function bind_group_switch() {
	$('#pg_wrap select.ps1').change(function() {
		product_group_switch();										 
	});
}

function product_group_switch() {
	var pg = {}
	$('#pg_wrap select.group_field').each(function() {
		pg[$(this).attr("class").split(" ")[0].substr(1)] = $(this).val();											   
	});
	
	$.ajax({
	   type: "POST",
	   url: '/includes/ajax_functions.php',
	   data: constructQueryString('get_grouped_product_url',$('#pg_wrap input.GroupID').val(), $.toJSON(pg)),
	   dataType: "html",
	   success: function(result) {
			window.location.href='/products/' + result;
	   }
	 });
}

function bind_product_group_grid_hover() {
	$("#pg_wrap_grid span.colorBox, #pg_wrap_grid #pColors a").hover(function() {
		if($(this).tag() == 'span') {
			var jq = $(this);	
		} else {
			var jq = $(this).siblings('span.colorBox');	
		}
		var pos = jq.position();
		var left = pos['left'] + jq.width() + 5;
		var top = pos['top'] - 105;

		$(this).siblings('div.colorBoxPreview').css('left',left).css('top',top).show();
	 }, function() {
		$(this).siblings('div.colorBoxPreview').hide();
	});
	$("#pg_wrap_grid span.colorBox").click(function() {
		var href = $(this).siblings('a').attr('href');
		window.location.href=href;
	});
}

