/* 

	Function to prevent customer's from leaving the page without making an order

*/

var __customerValidClick = 0;

function bindCustomerCare() {
	
	if ($('.checkout-block').length) {
	
		$('.customer-care').each(function() {
							
			$(this).mousedown(function(){__customerValidClick = 1});
			
		});
		
		try {
	
			$(window).bind('beforeunload', function() {
				
				if (!__customerValidClick) {
				
					return 'ОТКАЗАТЬСЯ ОТ ОФОРМЛЕНИЯ ЗАКАЗА И ВЕРНУТЬСЯ К НЕМУ ПОЗДНЕЕ?';
					
				}
				else {
					
					return ;
					
				}
				
			});
			
		}
		catch(e) {}
		
	}
	
}

var currentCitySuggestData = '-1';
		
function bindColorboxClick() {
		
	$("a[rel='colorbox']").colorbox();				

	$("#click").click(function(){
		
		$('#click').css({"background-color":"#f00", "color":"#fff", "cursor":"inherit"}).text("Open this window again and this message will still be here.");

		return false;
		
	});
	
}	
	
function citySuggest(val) {
	
	if (typeof(val) != 'undefined') {
		
		if (val == currentCitySuggestData) {return ;}
		
		currentCitySuggestData = val;
		
		$.ajax({
		
			type: 'post',
			url: '/web/forms/geo/suggest',
			data: 'city=' + val,
			dataType: 'html',
			success: function(html) {
				
				$('#citysuggest').show();
				
				$('#citysuggest').html(html);
				
			}
		
		});
		
	}
	
}

function bindSearchSuggest() {		
	
	$('.search').each(function() {				
		
		$(this).keyup(function() {
			
			var v = this.value;
			
			if (v.length >= 3 && $(this).attr('last') != v) {
				
				$.ajax({
				
					type: 'post',
					url: '/web/forms/search/suggest',
					dataType: 'html',
					data: $('#search input'),
					success: function (html) {
					
						$('#suggest .response').html(html);
						
						$('#suggest').fadeIn(500);
						
						$('.menu > .last').hoverIntent({
			
							timeout: 100,
						
							over: function () {
								
								$('#suggest').fadeIn(500);
								
							},
							
							out: function () {
								
								$('#suggest').fadeOut(500);
								
							}
							
						});
						
						if ($('#suggest > .response > div').height() > 300) {
							
							$('#suggest > .response > div').addClass('overflow-y-scroll height-300');
							
							$('#suggest > .response').css('padding','15px 10px 15px 20px');
							
						}
					
					}
					
				});	
				
			}
			else {
				
				$('#suggest').fadeOut(500);
				
				$('.menu > .last').hoverIntent({
			
					timeout: 0,
				
					over: function () {},
					
					out: function () {}
					
				});
				
			}
			
			$(this).attr('last',v);
			
		});
		
	});
	
}

function bindSorryClick() {
	
	$('.sorry').each(function() {
		
		$(this).click(function() {
					
			$.ajax({
				
				type: 'get',
				url: '/web/forms/home/sorry',
				dataType: 'html',
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'400px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'156px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});	
			
			return false;								
		
		});
		
	});							
	
}

function bindDiscountClick() {
	
	$('.discount-programm').each(function() {
		
		$(this).click(function() {
					
			$.ajax({
				
				type: 'get',
				url: '/web/forms/home/discountprogramm',
				dataType: 'html',
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'500px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'156px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});									
		
		});
		
	});							
	
}

function bindSubscribeNewsClick() {
	
	$('.subscribe-news').each(function() {
		
		$(this).click(function() {
					
			$.ajax({
				
				type: 'post',
				url: '/web/forms/feedback/subscribenews',
				dataType: 'html',
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'400px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'156px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});									
		
		});
		
	});							
	
}

function bindSubscribeArticlesClick() {
	
	$('.subscribe-articles').each(function() {
		
		$(this).click(function() {
					
			$.ajax({
				
				type: 'post',
				url: '/web/forms/feedback/subscribearticles',
				dataType: 'html',
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'400px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'156px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});									
		
		});
		
	});							
	
}

function bindWorktimeClick() {
	
	$('.worktime').each(function() {
		
		$(this).click(function() {
					
			$.ajax({
				
				type: 'get',
				url: '/web/forms/home/worktime',
				dataType: 'html',
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'470px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'156px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});									
		
		});
		
	});							
	
}

function bindRegionClick() {
	
	$('.cityvalue').each(function() {
		
		$(this).click(function() {
					
			$.ajax({
				
				type: 'get',
				url: '/web/forms/geo/regionselect',
				dataType: 'html',					
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'500px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'106px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});									
		
		});
		
	});							
	
}

function bindCallbackClick() {
	
	$('.callbackme').each(function() {								
		
		$(this).click(function() {
		
			$.ajax({
				
				type: 'post',
				url: '/web/forms/feedback/callback',
				dataType: 'html',					
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'500px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'106px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});
			
		});
		
	});							
	
}

function bindFeedbackClick() {
	
	$('.feedback').each(function() {							
				
		$(this).click(function() {
					
			$.ajax({
				
				type: 'post',
				url: '/web/forms/feedback/feedback',
				dataType: 'html',					
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'500px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'106px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});
			
		});
		
	});							
	
}

function bindClaimClick() {
	
	$('.claim').each(function() {							
				
		$(this).click(function() {
					
			$.ajax({
				
				type: 'post',
				url: '/web/forms/feedback/claim',
				dataType: 'html',					
				success: function (html) {
				
					$.blockUI({
					
						message : html,
						
						css : {
							
							border: 'none',
							backgroundColor:'transparent',
							width:'500px',							
							margin:'0 0 0 -230px',
							left:'50%',
							top:'50px',
							cursor: 'default'					
						
						},
						
						overlayCSS : {
						
							background: 'url(\'/i/lightbox/bg.png\')',
							cursor: 'default'
						
						},
						
						applyPlatformOpacityRules : false,
						
						timeout : 120000
						
					});												
				
				}
				
			});
			
		});
		
	});							
	
}

function bindCartToggle() {
	
	$('.basket').hover(function(event) {return ;}, function(event) {$('.cart-content').hide();});

	$('.show-cart-content').mouseover(function(){

		$('.cart-content').show();

	});		
	
}

function bindAddToCartClick() {
	
	$('#add_to_cart').replaceWith('<a onclick="" id="add_to_cart" class="button">' + $('#add_to_cart').html() + '</a>');
			
	$('#add_to_cart').click(function () {
		
		$.ajax({				
		
			type: 'post',			
			url: '/web/forms/cart/add',			
			dataType: 'html',
			data: $('#product :input'),
			
			success: function (html) {
				
				$('#cart_top').html(html);
				
				$('.cart-content').show();
				
				$('html, body').animate({scrollTop:0}, 'slow');
				
				setTimeout(function() {
           									
           			$('.cart-content').hide();           																				
										
				}, 7000 );
				
			},	
			
			complete: function () {
			
				var image = $('#image').offset();				
	
				$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: 10px; left: 10px;" />');
	
				params = {
				
					top : '-395px',
					left : '500px',
					opacity : 0.0,
					width : $('#cart_top').width(),  
					heigth : $('#cart_top').height()
				
				};		
	
				$('#temp').animate(params, 'slow', false, function () {
					
					$('#temp').remove();
					
				});
						
			}	
					
		});	
				
	});
	
}

(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

$(document).ready(function() {
	
	try {
		
		$('#seospacer').height($('#seo').height() + 15);
		
	}
	catch(e) {}
	
	// BIND MENU
	
	$('.menu2wrapper').each(function() {
		
		$(this).parent().eq(0).hoverIntent({
			
			timeout: 100,
			
			over: function () {
				
				var current = $('.menu2wrapper:eq(0)', this);
				current.fadeIn(500);
				
			},
			
			out: function () {
				
				var current = $('.menu2wrapper:eq(0)', this);
				current.fadeOut(500);
				
			}
			
		});
		
	});
	
	// BIND LEFT MENU 
	
	try {
		
		$('.menu-left-inner').each(function() {
			
			$(this).parent().eq(0).hoverIntent({
			
				timeout: 100,
				
				over: function () {										
					
					var current = $('.menu-left-inner:eq(0)', this);
					
					var parent = current.parent().addClass('current');
					
					current.fadeIn(500);										
					
				},
				
				out: function () {
					
					var current = $('.menu-left-inner:eq(0)', this);
					
					var parent = current.parent().removeClass('current');
					
					current.fadeOut(500);
					
				}
				
			});
			
		});
		
	}
	catch(e) {}
	
	// BIND CART TOGGLE
	
	try {
		
		bindCartToggle();
		
	}
	catch(e) {}
	
	// BIND CART CLICKS
	
	try {
		
		bindAddToCartClick();
		
	}
	catch(e) {}
		
	// BIND POPUPS
	
	try {
		
		bindSorryClick();
		
	}
	catch(e) {}		
	
	try {
	
		bindWorktimeClick();
		
	}
	catch(e) {}
	
	try {
		
		bindDiscountClick();
		
	}
	catch(e) {}
	
	try {
		
		bindSearchSuggest();
		
	}
	catch(e) {}
	
	try {
	
		bindRegionClick();
		
	}
	catch(e) {}
	
	try {
	
		bindCallbackClick();
		
	}
	catch(e) {}
	
	try {
	
		bindFeedbackClick();
		
	}
	catch(e) {}
	
	try {
	
		bindClaimClick();
		
	}
	catch(e) {}
	
	try {
	
		bindSubscribeNewsClick();
		
	}
	catch(e) {}
	
	try {
	
		bindSubscribeArticlesClick();
		
	}
	catch(e) {}
	
	try {
		
		bindColorboxClick();
		
	}	
	catch(e) {}
	
	try {
		
		bindCustomerCare();
		
	}	
	catch(e) {}		
	
});
