﻿var need_hide = true;

window.ie6 = $.browser.msie &&  parseInt($.browser.version) < 7 ? true : false;

function bookmark(a){ 
  var url = window.document.location; 
  var title = window.document.title; 
  var b = $.browser; 
 
  if (b.msie && 8 >= b.version && b.version >= 4) window.external.AddFavorite(url,title); 
  else if (b.opera) { 
  $(a).attr('href', url); 
  $(a).attr('rel', "sidebar"); 
  $(a).attr('title') = url+','+title; 
  return true; 
  } 
  else if (b.mozilla) window.sidebar.addPanel(title,url,""); 
  else alert("Нажмите Ctrl+D, чтобы добавить страницу в закладки."); 
  return false; 
 }

function countdown(secondsRemaining) {
var timerUpdate=1; //seconds for updating
var stopPropogation=false;
	var days = Math.floor(secondsRemaining / 86400),
	    hours = Math.floor((secondsRemaining - (days * 86400)) / 3600),
	    minutes = Math.floor((secondsRemaining - (days * 86400) - (hours * 3600)) / 60),
	    seconds = secondsRemaining - (days * 86400) - (hours * 3600) - (minutes * 60);
	    
	if(secondsRemaining > 0) {
	
		if(days < 10) { days = '0' + days; }
		if(hours < 10) { hours = '0' + hours; }
		if(minutes < 10) { minutes = '0' + minutes; }
		if(seconds < 10) { seconds = '0' + seconds; }
		
		$('#date_end_d').html(days);
		$('#date_end_h').html(hours);
		$('#date_end_m').html(minutes);
		$('#date_end_s').html(seconds);
		
		secondsRemaining-=timerUpdate;
		
	} else {
	
		//if(secondsRemaining <= 0) {
			$('#date_end_d').html('00');
		$('#date_end_h').html('00');
		$('#date_end_m').html('00');
		$('#date_end_s').html('00');
		stopPropagation=true;
			//window.location.reload();
		
		//}
		
	}
	if(!stopPropogation) window.setTimeout(function() {
	
   		countdown(secondsRemaining);
   		
	}, 1000);
	
}

function hide_form(id)
{
	$('#'+id).hide();
	$('.fade').hide();
	if (window.ie6 && $('#fade_frame')[0])
	{
		$('#fade_frame').css('display', 'none');
	}
}
function show_form(id)
{
	$('#'+id).show();
	if (window.ie6)
	{
		if (!$('#fade_frame')[0])
		{
			$('<iframe id="fade_frame" name="fade_frame" frameborder="0" scrolling="no" align="top" marginheight="0" marginwidth="0" src="/trans.html" style="height: 1000px;position:absolute;z-index:500;width:100%;filter:alpha(opacity=0);"></iframe>').insertBefore('.fade');
		}
		else
		{
			$('#fade_frame').css('display', 'inline');
		}
		$('#fade_frame').css('height',$(document).height()+'px');
		$('#fade_frame').css('height',$(document).width()+'px');
	}
	$('.fade').show();
	$('.fade').css('height',$(document).height()+'px');
	$('#sent_psw_block').hide();
	$('#forgot_psw_block').hide();
	$('#login_block').show();
}

function showVideo(src){
	$('#frame').attr("src",src);
	$('#iframe_container').css({"left": ($(window).width() / 2 - 320 ) + "px"});
	$('#iframe_container').css({'top' : ($(document).scrollTop() + 200)+'px'});
	$('.fade').show();
	$('.fade').css('height',$(document).height()+'px');
	$('#iframe_container').show();
}
function hideVideo(){
	$('#frame').attr("src","/video_view/");
	$('.fade').hide();
	$('#iframe_container').hide();
}

function basket_add(itemID,cant,oTarget){
var VRegExp = new RegExp(/basket+/i);
dLoc=document.location.href;
var isBasket = VRegExp.test(dLoc); 
urlStr = '/index.php';
if(isBasket) urlStr +='?page=basket';
	$.ajax({
				url: urlStr,
				type: 'GET',
				data: 'itemID='+itemID+'&cant='+cant+'&action=addToBasket',
				complete: updateBasketContent,
				success: function(data){
					if($(oTarget).is('.add_to_cart')){
						var q = eval('(' + data + ')');
						$('.message #basket_cant').html(q.tot_col);
						if(!$(oTarget).hasClass('kupit_box_bot_text')){
							$('.basket_layer').show();
							pos=$(oTarget).offset();
							$('.basket_layer').css('left',pos.left-10+'px');
							$('.basket_layer').css('top',pos.top-10+'px');
							setTimeout(function(){
								$('.basket_layer').hide();
							},5000);
						}
					}
					return true;
				}
			});

}

function rate_item(oStarImg,noaction){
if(typeof oStarImg == 'object'){
	var oDiv=$(oStarImg).parent();
	var rate=$(oStarImg).attr('num');
}else{
	var oDiv=$('#ratebar_'+oStarImg);
	var rate=parseInt($(oDiv).attr('rate'));
	if(rate==undefined || isNaN(rate)) return false;
}

var i=0;
var itemID=$(oDiv).attr('itemID');
	$(oDiv).find('img').each(function(){
		i++;
		if(i<=rate) $(this).attr('src', '/img/star1.gif');
	});
	$(oDiv).find('img').unbind();
	$(oDiv).attr('rate',rate);
	$(oDiv).attr('class','rated');
var VRegExp = new RegExp(/work+/i);
dLoc=document.location.href;
var isWork = VRegExp.test(dLoc); 	
	if(noaction==undefined){
		$('#rate_descr').html('ваша оценка');
		$.ajax(
			{
				url: dLoc,
				type: 'POST',
				data: 'itemID='+itemID+'&rate='+rate+'&action='+(isWork?'rate_contest':'rate_item'),
				complete: function(originalRequest){
					if(originalRequest.responseText!=""){
						var q = eval('(' + originalRequest.responseText + ')');
						if(q['rate']){
							$(oDiv).prev().find('.points').text(q['rate']);
							$('div.points').text(q['rate']);
						}
					}
				}
			});
	}
}

var dx = 0;
var scr_left = 0;
var timeout_id;
var globalTop = 200;

function resize()
{
/*	if( $(window).width() < $('.fade').width() ) $('.carusel').width( $(window).width() - 10 );
		else $('.carusel').width( $('.fade').width() - 10 );
	alert($('.carusel').width());
*/
	
	var w = $(document).width();
	if(w > 1600)
	{
		$('#container').width(1600);
		$('.carusel').width( $('#container').width() - 10 );
	}
		else
		{
			$('#container').width("100%");
			if( $(window).width() < $('.fade').width() ) $('.carusel').width( $(window).width() - 10 );
					else $('.carusel').width( $('.fade').width() - 10 );
		}

	var h = $('#container').height();
	$('.fade').height(h+"px");

	$('.form_table').css({"left": ($(window).width() / 2 - $('.form_table').width()/2 ) + "px"});
	$('.form_table').css({'top' : ($(document).scrollTop() + globalTop)+'px'});


	$(window).load(function() {
		//высота флоатов
		var h = 0;
		$('.prod_item_float').each(function(){
			if($(this).height() > h) h = $(this).height();
		});
		$('.prod_item_float').each(function(){
			$(this).css('height',h+'px');
		});
	});
}

function scrolling(){
	$('div.carusel').scrollLeft( $('div.carusel').scrollLeft() + dx );
	timeout_id = setTimeout( 'scrolling();', 1);
}

function updateBasketContent(originalRequest){
var q = eval('(' + originalRequest.responseText + ')');

					if(q['location']) {document.location=q['location']; return false;}
					if(q['tot_col']>0) $('#your_basket').html("<span>ВАША КОРЗИНА ("+q['tot_col']+")</span>");
					else $('#your_basket').html("ВАША КОРЗИНА");
var VRegExp = new RegExp(/basket+/i);					
dLoc=document.location.href;
var isBasket = VRegExp.test(dLoc);
				if(isBasket){
					if(q['tot_price']>0) $('#all_price').html(q['tot_price']+"<span>&nbsp;руб.</span>");
					if(q['count']>0) $('#item_cant_'+q['itemID']).val(q['count']);
					if(q['discount']>0){ 
						$('#discount').html(q['discount']+"<span>&nbsp;руб.</span>");
						if($('.discount').css('display')=='none'){
							$('.discount, .allcost').css('display',"");
						} 
					}else{
						$('.discount, .allcost').css('display',"none");
					}
					if(q['discount_percent']>0) $('#discount_percent').html(q['discount_percent']);
					if(q['tot_user_price']>0) $('#all_user_price').html(q['tot_user_price']+"<span>&nbsp;руб.</span>");
					if(q.insert){
						$('<tr><td colspan="5"><div class="hr6"></div></td></tr>'+q.insert).insertAfter('.basket_table tr:last');
					}
					var delivery_sum=$('#togiftdelivery').attr("sum");
					var sum=new Array();
					$('div.gift_sum').each(function(index, el){
						sum[index]=$(this).attr('sum');
					});
					havesum=false;
					var i=0;
					for(i=0;i<sum.length;i++){
						if(q['tot_user_price']>sum[i]){
							j=(i>0)?sum[i-1]:'none';
							if($('#togift'+j).css('display')=='none' || i==0){
								 $('.togiftmsg').slideUp(100);
							}
							if(i>0 && q['tot_user_price']<j){
								$('#togift'+j+' span.needle').html(j-q['tot_user_price']);
								$('#togift'+j).slideDown(500);
							}
							if($('#gift'+sum[i]).css('display')=='none'){
								$('div.gift_sum').slideUp(500);
								$('#gift'+sum[i]).slideDown(500);
							}
							i=sum.length;
							havesum=true;
						}
					}
						if(!havesum){
							$('div.gift_sum').slideUp('fast');
							$('.togiftmsg').slideUp(100)
						}
						if(q['tot_user_price']>=delivery_sum){
							if($('#togiftdelivery').css('display')!='none') $('#togiftdelivery').slideUp(100);
						}else{
							$('#togiftdelivery span.needle').html(delivery_sum-q['tot_user_price']);
							if($('#togiftdelivery').css('display')=='none')$('#togiftdelivery').slideDown(500);
							if(!havesum){
							last=sum.length-1;
								if(q['tot_user_price']>=sum[last]){
									$('#gift'+sum[last]).slideDown(500);
									$('#togift'+sum[last-1]).slideDown(500);
								}else{
									$('#togift'+sum[last]+' span.needle').html(sum[last]-q['tot_user_price']);
									if($('#togift'+sum[last]).css('display')=='none') $('#togift'+sum[last]).slideDown(500);
								}
							}
						}
			}
}

$(document).ready(function(){
	$('.autohide').hide(); //hide all notice/help elements when JavaScript is On
	$('.autoshow').show(); //show some elements when JavaScript is On
	resize();
	scr_left = ($('.carusel ul').width() / 2) - ($(document).width() / 2);
	window.ie6 = $.browser.msie &&  parseInt($.browser.version) < 7 ? true : false;
	
	$('#psevdopass, #psevdopass2').focus(function(){
		$(this).hide();
		$(this).next().show();
		$(this).next().focus();
	});
	$('input.but').hover(function(){
		$(this).css('background-image','url(/img/but_bg1.gif)');
	},
	function(){
		$(this).css('background-image','url(/img/but_bg.gif)');
	});
	$('#truepass, #truepass2').blur(function(){
		if($(this).val()==''){
			$(this).hide();
			if($(this).prev().attr('erMsg')) $(this).prev().val($(this).prev().attr('erMsg'));
			$(this).prev().show();
		}
	});
	
	$('#truepass, #truepass2').keyup(function(){
		if($(this).val()!=''){
			$(this).prev().val($(this).val());
		}
	});
		
	
	$('div.carusel').scrollLeft( scr_left );
	
	var tmp="";
	$('img').hover(
		function()
		{
			if( (!$(this).hasClass('act')) && ($(this).attr('hover')) )
			{
				$(this).attr('tmp', $(this).attr('src'));
				$(this).attr('src', $(this).attr('hover'));
			}
		},
		function()
		{
			if( (!$(this).hasClass('act')) && ($(this).attr('hover')) ){
				tmp=$(this).attr('tmp');
				$(this).attr('src', tmp);
				$(this).attr('tmp', '');
			}
		}
	);


	$('.b_carusel_prev').hover(
		function()
		{
			dx = -5;
			timeout_id = setTimeout( 'scrolling();', 10);
		},
		function()
		{
			dx = 0;
			clearTimeout(timeout_id);
		}
	);

	$('.b_carusel_next').hover(
		function()
		{
			dx = 5;
			timeout_id = setTimeout( 'scrolling();', 10);
		},
		function()
		{
			dx = 0;
			clearTimeout(timeout_id);
		}
	);

	$('.b_search_bg').hover(
		function()
		{
			$("#b_search3").attr("src", "/img/b_search31.gif");
			$(this).addClass('hovered');
		},
		function()
		{
			$("#b_search3").attr("src", "/img/b_search3.gif");
			$(this).removeClass('hovered');
		}
	);

	$('.sm div.item').hover(
		function()
		{
			num = $(this).attr("num");
			if(!$(this).hasClass("anosubitems"))
			{
				if(!$(this).hasClass("clicked"))
				{
					$("#sm_ar"+num).attr("src", "/img/arrow_sm1.gif");
				}
				if($(this).hasClass("nosubitems"))
				{
					$("#sm_ar"+num).attr("src", "/img/arrow_sm4.gif");
				}
			}
		},
		function()
		{
			num = $(this).attr("num");
			if(!$(this).hasClass("anosubitems"))
			{
				if(!$(this).hasClass("clicked"))
				{
					$("#sm_ar"+num).attr("src", "/img/arrow_sm.gif");
				}
				if($(this).hasClass("nosubitems"))
				{
					$("#sm_ar"+num).attr("src", "/img/arrow_sm3.gif");
				}
			}
		}
	);

	$('.sm div.item a.click').click(function(){
		num = $(this).parent().attr("num");
		if(
			(!$(this).parent().hasClass("clicked")) &&
			(!$(this).parent().hasClass("nosubitems")) &&
			(!$(this).parent().hasClass("anosubitems"))
		)
		{
			$('.item').each(function(){
				$(this).removeClass("clicked");
				n = $(this).attr("num");
				if($("#sm_ar"+n).attr("src") == "/img/arrow_sm2.gif")
					$("#sm_ar"+n).attr("src", "/img/arrow_sm.gif");
			});

			$('#sub'+num).slideDown(150, function(){
				$('.sub').each(function(){
					if( num != $(this).parent().attr('num') ) $(this).slideUp(150);
				});
			});
			
			$(this).parent().addClass("clicked");
			$("#sm_ar"+num).attr("src", "/img/arrow_sm2.gif");

		}
		$(this).blur();
		if(!$(this).parent().hasClass("nosubitems")) return false;
	});
/*
		jQuery('.sm').accordion({
			clearStyle: true,
			header: '.item ',
			navigation: true,
			event: 'mouseover',
			fillSpace: false,
			autoHeight: true,
			animated: 'slide'//(jQuery.browser.msie) ? false : 'slide'
		});
*/


	$('.order_link a.poplink, .cabinet_links a.poplink, .only_logined a.poplink, .links a.poplink').click(function(){
		show_form($(this).attr("open"));
		return false;
	});
	$('.b_close').click(function(){
		hide_form($(this).attr("close"));
		return false;
	});

	$('.b_carusel_prev').hover(
		function()
		{
			$(this).addClass("hovprev");
		},
		function()
		{
			$(this).removeClass("hovprev");
		}
	);
	$('.b_carusel_next').hover(
		function()
		{
			$(this).addClass("hovnext");
		},
		function()
		{
			$(this).removeClass("hovnext");
		}
	);



	$('.plans td .pic2 a').hover(
		function()
		{
			$(this).parent().next().children().children().children().attr('src', '/img/b_zoom1.gif');
			$(this).parent().addClass('hover');
		},
		function()
		{
			$(this).parent().next().children().children().children().attr('src', '/img/b_zoom.gif');
			$(this).parent().removeClass('hover');
		}
	);

	$('.zoom a').hover(
		function()
		{
			$(this).parent().parent().prev().addClass('hover');
		},
		function()
		{
			$(this).parent().parent().prev().removeClass('hover');
		}
	);

	$('a.forgot').click(function(){
		$('#login_block').hide();
		$('#forgot_psw_block').show();
		return false;
	});

	$('a.remind').click(function(){
		$('#login_block').show();
		$('#forgot_psw_block').hide();
		return false;
	});

	$('a img.repare').click(function(){
		$('#forgot_psw_block').hide();
		$('#sent_psw_block').show();
		return false;
	});


	$('.prod_card_pic a').hover(
		function()
		{
			$(this).parent().next().children().children().attr('src', '/img/b_zoom3.gif');
		},
		function()
		{
			$(this).parent().next().children().children().attr('src', '/img/b_zoom2.gif');
		}
	);

	$('.stars img').hover(
		function()
		{
			num = $(this).attr('num');
			i = 0;
			$(this).parent().find('img').each(function(){
				i++;
				if(i<=num) $(this).attr('src', '/img/star1.gif');
			});
		},
		function()
		{	
			var rate=0;
			if($(this).parent().hasClass('rated')){
				rate=$(this).parent().attr('rate');
			}
				i=0;
				$(this).parent().find('img').each(function(){
					i++;
					if(i>rate) $(this).attr('src', '/img/star.gif');
				});
			
		}
	);
	$('.stars img').click(
		function()
		{
			rate_item($(this).get(0));
		}
	);


	$('#read_all_testim a').click(function(){
		$('#read_all_testim').hide();
		$('#hide_all_testim').show();
		$('#all_testimonials').show(400);
		resize();
		return false;
	});
	$('#hide_all_testim a').click(function(){
		$('#read_all_testim').show();
		$('#hide_all_testim').hide();
		$('#all_testimonials').hide(400);
		resize();
		return false;
	});


	$('#show_testim_form a').click(function(){
		$('#show_testim_form').hide();
		$('#hide_testim_form').show();
		$('#post_form').show(400);
		resize();
		return false;
	});
	$('#hide_testim_form a').click(function(){
		$('#show_testim_form').show();
		$('#hide_testim_form').hide();
		$('#post_form').hide(400);
		resize();
		return false;
	});
	
	$('.fade').click(function(){
		$('.form_table').hide();
		$('.fade').hide();
		$('#iframe_container').hide();
		$('#frame').attr("src","/video_veiew/");
		return false;
	});
	
	$('.add_to_cart').click(function(){
		itemID=$(this).attr('itemID');
		cant=$('#item_cant').val()>0?$('#item_cant').val():"1";
		basket_add(itemID,cant,$(this));
		/*curA=$(this).find("a").html('товар в корзине!');
		setTimeout(function(){
			$(curA).html('добавить в корзину');
		}, 3000);*/
		
		if($(this).parent().parent().attr('id')=='similar_items'){
			$(this).parent().remove();
			if($('#similar_items div').length<=0) $('#similar_items').remove();
		}
		return false;
	});
	$('input.cant').live('blur', function(){
		val=parseInt($(this).val());
		if(val<=0 || isNaN(val)){
			$(this).val("1");
			$(this).trigger('keyup');
		}
	});
	$('input.cant').live('keyup', function(){
		val=parseInt($(this).val());
		if(val>0 && !isNaN(val)){
		$.ajax(
			{
				url: '/index.php',
				type: 'POST',
				data: 'itemID='+$(this).attr('itemID')+'&cant='+$(this).val()+'&action=basket_items_recalc',
				complete: updateBasketContent
			});
		}
	});
	$('.kupit_box_bot').hover(function (){
			$(this).addClass('kupit_box_bot_hover');
		}, function (){
			$(this).removeClass('kupit_box_bot_hover');
		}
	);
	$('#show_block').click(function (){
		$('.trans').css('display', 'none');
		var h = $('#gogo').height() - 25;
		$('#gogo_wraper').css({'height':h+'px'});
		show_form('w_subscribe');
		//$('#gogo_wraper').addClass('pipker');
	});
	
	$('.pod').hover(
		function()
		{
			$(this).addClass('hvrd');
		},
		function()
		{
			$(this).removeClass('hvrd');
		}
	);
	
	if(typeof $.datepicker == 'object'){
		$.datepicker.setDefaults($.datepicker.regional['ru']);
		$(".datepicker").datepicker({
			changeMonth: true,
			changeYear: true,
			maxDate: "-6M",
			minDate: "-16Y",
			yearRange:"c-16:c+16",
			dateFormat: "dd.mm.yy"
		});
	};
	
	$('.selector_bg select').change(
		function()
		{
			$('.sel_bl').hide();
			$('#'+$(this).val()).show();
		}
	);

	$('.plans td .offer').mouseenter(
		function(e)
		{
			$('.pop_pic').css({'left':(e.pageX+10)+'px', 'top':(e.pageY+10)+'px'});
			$('.pop_pic div').html($(this).prev('img').clone());
			//if($('.pop_pic').css('display')=='none') 
			$('.pop_pic').stop(true,true).fadeIn(300); //;fadeTo(0,0).css('visibility','visible').fadeTo(300,1)
		});
	$('.plans td a.offer').mouseleave(	
		function()
		{
			//if($('.pop_pic').css('display')=='block') 
			$('.pop_pic').fadeOut(300);//;
		}
	);
	
});

$(window).resize(function(){
	resize();
});

$(window).scroll(function(){
	if( ($(document).scrollTop() > 100) && $(document).scrollTop() < ($(document).height() - $('.form_table').height() - 110))
	{
		$('.form_table').css({'top' : ($(document).scrollTop() + 100)+'px'});
	}
	if (window.ie6 && $('#fade_frame')[0])
	{
		$('#fade_frame').css({'top':$(document).scrollTop()+'px', 'height':$(window).height()+'px'});
		$('.fade').css({'top':$(document).scrollTop()+'px', 'height':$(window).height()+'px'});
	}
});
