var bg_idCookie = new jsCookie();
bg_idCookie.path="/";
bg_idCookie.domain="";
bg_idCookie.secure=false;
bg_idCookie.name="bg_id";
bg_idCookie.expires="0,0,0,0"; // Minutes, Hours, Days, Years

var regionCookie=new jsCookie();
regionCookie.path="/";
regionCookie.domain="";
regionCookie.secure=false;
regionCookie.name="hg_region";
regionCookie.expires="0,0,0,1"; // Minutes, Hours, Days, Years

$(document).ready(function(){
var scroll_area;
var FLAG;

sc_init();


	$( '.adress div.c span' ).click( function(){
		_par = $(this).parent();
		$( '.adress div.c' ).removeClass('here');
		_par.addClass('here');
	});

function setBgCookie( id ) {

	bg_idCookie.value = id;
	bg_idCookie.set( );	

}
function showHideArrows( newL ) {
	if ( newL == '0' ) {
		$('#scr_left').css({"display":"none"});
	}
	else if( newL == -2004 ) {
		$('#scr_right').css({"display":"none"});
	}
	else {
		$('#scr_left').css({"display":"block"});
		$('#scr_right').css({"display":"block"});
	}
}


$(".select li").hover(
	  function () {
		  this.className = 'hover';
	  }, 
	  function () {
		  this.className = '';
	  }
);


$(".scroller .block").hover(
	function () {
		$(this).children(".ramka").show();
	}, 
	function () {
		$(this).children(".ramka").hide();
	}
);



$('#scr_left').click(function(){	
	//текущее положение скроллера
	L = $("#wrap_area").position().left;
/*
	if(L <= maxL+168)
	{
		newL = '0';
	}*/

	if(L < 0)
	{
		newL = (L+167);
		//листаем
		gogo(newL);
	}
	return false;
	
});

$('#scr_right').click(function(){
	//текущее положение скроллера
	L = $("#wrap_area").position().left;
	//определяем в какую сторону листать
	/*
	if(L >= 0)
	{
		newL = maxL;
	}*/

	if(L > maxL)
	{
		newL = (L-167);
		gogo(newL);
	}
	return false;
	//листаем
	//$('#scr_right').css({"display":"none"});
});

function gogo(newL) //листалка
{
	if (!FLAG){
		FLAG = true;
		if($(".here")[0])
		{
			//$(".here")[0].className = 'block';
			$("#bt").attr({"src": '#', "class":""});
		}	
		$("#wrap_area").animate({ 
			left : newL
		}, 500, function(){FLAG = false;})
	}

	showHideArrows( newL );
	//alert(newL);
}

function sc_init() 
{
	scroll_area = $('#scroll_area').width();//видимая область
	maxw = ($(".block").size())*$(".block").width()+167; //вся область
	maxL = -maxw+scroll_area +20; //максимально докуда листать

	if ( $('#flash').is(':visible') == false ){
		if ( bg_idCookie.exists( ) ) {

			if ( hideFlashElement == 1 ) {

				bg_idCookie.get( );
				bgId = bg_idCookie.value;

				$('.block:eq('+(bgId-1)+')').attr({"class":"block here"});
				$("#allPage").attr("class", $('.here i').attr("class"));

				blocksNum = $(".block").size() - ( bgId - 1 );

				if ( blocksNum < 4 ) {
					delta = 4;
				}
				else {
					delta = blocksNum;
				}

				//if ( ($('.here').position().left+$("#wrap_area").position().left) > 501) {
					$("#wrap_area").animate({ 
						left : $("#wrap_area").position().left-167*( $(".block").size() - delta ) 
					}, 400 , function(){ showbotle(); var newL = $("#wrap_area").position().left; showHideArrows( newL ); });
				//}

				setBgCookie( bgId );

			}
			else {

				// изначально открываем
				$('.block:eq(2)').attr({"class":"block here"});
				$("#allPage").attr("class", $('.here i').attr("class"));
				setBgCookie( 3 );
				showbotle();

			}

		}
		else {
			// изначально открываем
			$('.block:eq(2)').attr({"class":"block here"});
			$("#allPage").attr("class", $('.here i').attr("class"));

			setBgCookie( 3 );
			//showbotle();
		}
	}

	InitRegion();

}

//клик по блоку
$('.block').click(function(){

	if ( $(this).hasClass("here") ) {
		return false;
	}

	$("#bt img").attr({"src": ""});
	$("#bt img").hide();

	if($(".here")[0])
	{
		$(".here")[0].className = 'block'; //снимаем предыдущий активный
	}
	this.className = "block here";
														   
	if(($('.here').position().left+$("#wrap_area").position().left) > 501) //если кликаем по крайнему блоку
	{
		$("#wrap_area").animate({ 
			left : $("#wrap_area").position().left-167
		}, 400 , function(){ showbotle(); });
	}
	else
	{
		showbotle();
	}
});

function InitRegion() {
	
	if ( regionCookie.exists( ) ) {

		regionCookie.get( );
		regId = regionCookie.value;

		if ( regId == "hgReg1" ) {
			$(".select_dd span")[0].innerHTML = "Беларусь";
			if ( document.location.href != 'http://hg-produkt.com/' ) {
				document.location.href = 'http://hg-produkt.com/';
			}
		}
		else if ( regId == "hgReg2" ) {
			$(".select_dd span")[0].innerHTML = "Россия";
			if ( document.location.href != 'http://hg-produkt.com/' + 'rus/' ) {
				document.location.href = 'http://hg-produkt.com/' + 'rus/';
			}
		}
		else if ( regId == "hgReg3" ) {
			$(".select_dd span")[0].innerHTML = "Украина";
			if ( document.location.href != 'http://hg-produkt.com/' + 'ua/' ) {
				document.location.href = 'http://hg-produkt.com/' + 'ua/';
			}
		}

	}
	else {
		$(".select_dd span")[0].innerHTML = "Беларусь";
	}

}

//ставим бэк и показываем бутылку
function showbotle()
{
		$("#allPage").attr("class", $('.here i').attr("class"));

		$("#bt").css({"left":$('.here').position().left+$("#wrap_area").position().left+280})
		$("#bt").addClass('show_img');

		$("#bt img").addClass('nopngfix');
		//$("#bt img").hide();
		$("#bt img").attr('src', $('.here i').text()).load(function () {

			$("#bt img").css('width', "205" );
			$("#bt img").css('height', "310" );
			$("#bt img").removeClass('nopngfix');
			$("#bt img").show();

		});

		bgId = Math.round( $('.here').position().left / 167 ) + 1;
		setBgCookie( bgId );

}



	/*раскрытие селекта*/
	$('.select_dd').click(function(){
		el = this.getElementsByTagName('UL')[0];
		(el.parentNode.className == 'select_dd') ? el.parentNode.className = 'select_dd open' :  el.parentNode.className = 'select_dd';
		$(el).slideToggle(300);
		
		
	});
	
	/*выбор внутри селекта*/
	$('.select_dd li').click(function(){
		el2 = this.parentNode.parentNode.getElementsByTagName('span')[0];
		regionCookie.value = this.id;
		regionCookie.set();
		el2.innerHTML = this.innerHTML;
		document.location.href = document.location.href;
	});
	
	/*Закрытие селекта при убирание мыши*/
	$('.select_dd ul').bind("mouseleave",function(){
		$(this).slideUp(300);
		this.parentNode.className = 'select_dd';
	});
	
	//$("#bt img").attr({"src": $('.here i').text()});
	//$("#bt").css({"left":$('.here').position().left+$("#wrap_area").position().left+280})
	//$("#bt").attr({"class":"show_img"});
});

//set active for flash
function select_bt()
{

	$("#bt").css({"left":$('.here').position().left+$("#wrap_area").position().left+280})
	$("#bt").addClass('show_img');

	$("#bt img").addClass('nopngfix');
	$("#bt img").hide();
	$("#bt img").attr('src', $('.here i').text()).load(function () {

		$("#bt img").css('width', "205" );
		$("#bt img").css('height', "310" );
		$("#bt img").removeClass('nopngfix');
		$("#bt img").show();

	});	

}

