/**
 * 
 * @author Artod http://artod.ru for ООО "Экспресс лаб"
 *
 * Copyright (c) 2009 ООО "Экспресс лаб" http://expresslab.ru
 *
 */

$(document).ready(function() {

	$("a.fancybox").fancybox({
		'zoomOpacity' : false,
		'overlayShow' : true,
		'hideOnContentClick' : false,
		'overlayOpacity' : '0.8',
		'zoomSpeedIn' : '200',
		'zoomSpeedOut' : '200'
	});

	$('a.photo-small').click(function(){
		//$("<img>").attr("src", src);
		//$('a.photo-small').unbind('click');
		//if ($('#photo-big').has('img'));
			//return false;
			
		var src = $(this).find('img').attr('src').replace('&maxWidth=55', '&maxWidth=250');
		//$('#photo-big').css('height', $('#photo-big').css('height'));
		$('#photo-big img').fadeOut('fast', function(){
			var a = $('#photo-big a');
			a.empty();
			a.append('<img src="'+src+'" style="display:none;" alt="" />');
			a.attr('href', src.replace('image_resize.php?fileName=', '').replace('&maxWidth=250', ''));
			$('#photo-big img').load(function(){
				//$('#photo-big').css('height', $('#photo-big img').css('height'));
				$('#photo-big img').fadeIn('fast');
				//$('a.photo-small').click(function(){cl();});
			});
			
		});
		return false;
	});
	/*		var size = $('#size-option option:selected').val();
		//var basket = getCookie('basket');
		if (basket) {
		setCookie('basket', '', -9, '/');
			var t = getCookie('basket').split(';');
			var t2, basket;
			
			$.each(t, function(key, val){
				t2[key] = val.split(',');
				basket[t2[key][0]][t2[key][1]] = t2[key][2];
			});
			alert(basket[3][42]);afsdf;
			//var basket[t[0]][t[1]] = ;
		} else {}*/
	$('#captcha-new').click(
		function(){
			$('#captcha').attr('src', 'CAPTCHA/captcha.php?a=image&amp;t='+Math.random());
			return false;
		}
	);
		
	$('#basket').click(function() {
		var basket = getCookie('basket');
		var size = $('#size-option option:selected').val();
		// setCookie('basket', (basket ? basket : '')+$(this).attr('rel')+','+(size?size:0)+';', 15000, '/');
		$.cookie('basket', (basket ? basket : '')+$(this).attr('rel')+','+(size?size:0)+';', {path: '/'});
		//setCookie('basket', '', -9, '/');
		//alert(getCookie('basket'));
		alert('Товар успешно добавлен в корзину! Можете оформить заказ.');
		return false;
	});
	
	$('a.basket-minus').click(function() {
		var count = $(this).parent().find('span.count');
		var cost = $(this).parent().prev().find('span.cost');
		var sum = $('span.sum');

		count.text(parseInt(count.text())-1);
		sum.text(parseInt(sum.text())-parseInt(cost.text()));
		
		var basket = getCookie('basket').replace($(this).attr('rel'), '');
		// setCookie('basket', basket, 15000, '/');
		$.cookie('basket', basket, {path: '/'});
		
		if(!parseInt($(this).parent().find('span.count').text())) {
			$(this).parent().parent().remove();
		}
		if(!basket) {
			$('#order').html('Корзина пуста<br/><br />');
		}
		return false;
	});
	
	$('a.basket-del').click(function() {
		var count = parseInt($(this).parent().parent().find('span.count').text());
		//alert(count);
		var minus = $(this).parent().parent().find('a.basket-minus');
		for(i=0; count>i; i++) {
			minus.click();
		}
		return false;
	});
	
	$('a.basket-plus').click(function() {
		var count = $(this).parent().find('span.count');
		var cost = $(this).parent().prev().find('span.cost');
		var sum = $('span.sum');

		count.text(parseInt(count.text())+1);
		sum.text(parseInt(sum.text())+parseInt(cost.text()));
		
		var basket = getCookie('basket')+$(this).attr('rel');
		// setCookie('basket', basket, 15000, '/');
		$.cookie('basket', basket, {path: '/'});
		
		return false;
	});
	
	$('#basket-clear').click(function() {
		// setCookie('basket', '', -9, '/');
		$.cookie('basket', '', {path: '/'});
	});
	



});
	function setCookie (name, value, expires, path, domain, secure) {
		  document.cookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	}

	function getCookie(name) {
		var cookie = " " + document.cookie;
		var search = " " + name + "=";
		var setStr = null;
		var offset = 0;
		var end = 0;
		if (cookie.length > 0) {
			offset = cookie.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = cookie.indexOf(";", offset)
				if (end == -1) {
					end = cookie.length;
				}
				setStr = unescape(cookie.substring(offset, end));
			}
		}
		return(setStr);
	}

