/**
 * @type {Image[]} 
 */
var aProdImages = new Array();
var iPrevProd = 0;


function changeProdImage( event ) {
	$('#prod'+iPrevProd).removeClass('current');
	$('#prod'+event.data.iCurrent).addClass('current');
	$('#prodImage').attr( 'src', aProdImages[event.data.iCurrent].src );
//	$('#prodHref').attr( 'href', '' );
	iPrevProd = event.data.iCurrent;
}

$(document).ready(function() {
	/**
	 * Слайд-шоу.
	 */
	var sContent = '<img src="/images/slideshow/55.jpg" width="209" height="273" border="0"/>' +
			'<img id ="fullimg" src="/images/slideshow/5.jpg" width="209" height="273" border="0"/>' +
			'<img src="/images/slideshow/44.jpg" width="209" height="273" border="0"/>' +
			'<img id ="fullimg" src="/images/slideshow/4.jpg" width="209" height="273" border="0"/>' +
			'<img src="/images/slideshow/33.jpg" width="209" height="273" border="0"/>' +
			'<img id ="fullimg" src="/images/slideshow/3.jpg" width="209" height="273" border="0"/>' +
			'<img src="/images/slideshow/22.jpg" width="209" height="273" border="0"/>' +
			'<img id ="fullimg" src="/images/slideshow/2.jpg" width="209" height="273" border="0"/>' +
			'<img src="/images/slideshow/11.jpg" width="209" height="273" border="0"/>' +
			'<img id ="fullimg" src="/images/slideshow/1.jpg" width="209" height="273" border="0"/>';
	$('#slideshow').html( sContent );
	$('#slideshow').cycle({
		'fx': 'fade',
		'speed': 2000,
		'timeout': 1000
	});
	
	/**
	 * Динамические картинки на Каминные топки и печи.
	 */
	aProdImages[0] = new Image(200, 181);
	aProdImages[1] = new Image(200, 181);
	aProdImages[2] = new Image(200, 181);
	aProdImages[3] = new Image(200, 181);
	aProdImages[0].src = '/images/prod-uniflam.jpg';
	aProdImages[1].src = '/images/prod-laudel.jpg';
	aProdImages[2].src = '/images/prod-invicta.jpg';
	aProdImages[3].src = '/images/prod-drova.jpg';
	$('#prod0').bind( 'mouseover', {'iCurrent': 0}, changeProdImage );
	$('#prod1').bind( 'mouseover', {'iCurrent': 1}, changeProdImage );
	$('#prod2').bind( 'mouseover', {'iCurrent': 2}, changeProdImage );
	$('#prod3').bind( 'mouseover', {'iCurrent': 3}, changeProdImage );
	
	/**
	 * Всплывающие подсказки.
	 */
	$(".tip").mouseover(showTips);
	$(".tip").mouseout(hideTips);
});

/**
 * Всплывающие подсказки.
 */
function hideTips (event) {
	$("#popup").css({top: 0, left: 0, display:"none" });
	$("#popup").html();
}

function showTips(event) {
	var tiptext = jQuery(event.target).attr("tiptext");
	if ( tiptext && tiptext.length > 1) {
		var imgSize = 470;
		var popup = jQuery("#popup");
		var iPosX = 495;
		var iPosY = 50;
		popup.html('<img src="'+ tiptext +'" width="'+ imgSize +'" height="'+ imgSize +'" alt="Загрузка...">');
		if ( $.browser.name == 'msie' && $.browser.version < 7 )
			iPosY += document.documentElement.scrollTop;
		popup.css({top: iPosY, left: iPosX, display:"block"});
	}
}

/**
 * Корзина.
 */
function updateCartInfo( data ) {
	$('#goodsInCartCount').html(data.count);
	$('#goodsInCartPrice').html(data.price);
	$.noticeAdd({text:'Товар добавлен в корзину', stay:false, inEffectDuration:300, stayTime:2000});
}

function updateCart( data ) {
	$('#prouction_table').html(data);
	$(".tip").mouseover(showTips);
	$(".tip").mouseout(hideTips);
	$.getJSON('/ajax/info/', {}, function(data){
		if ( !data.count ) {
			$('#cart_title').html('У вас пока нет покупок');
			$('#order_form').html('&nbsp;');
		}
		$('#goodsInCartCount').html(data.count);
		$('#goodsInCartPrice').html(data.price);
	});
}

function checkCount( id, field_id ) {
	var field = $('#'+field_id);
	var val = field.attr('value');
	if ( !parseInt(val, 10) ) {
		val = 1;
		field.attr('value', 1);
	} else if ( parseInt(val, 10) < 1 ) {
		val = 1;
		field.attr('value', 1);
	} else if ( val > 99 ) {
		val = 99;
		field.attr('value', 99);
	}
	$.getJSON('/ajax/changeCount/'+id+'/'+val+'/', {}, function(data){
		$('#goodsInCartCount').html(data.count);
		$('#goodsInCartPrice').html(data.price);
		$('#totalPrice').html(data.price);
	});
}
