

$.fn.reposition = function(){
    var o = $(this).children('.pp-navigation')
	 var w = $(this).width()+78;

    if (o.width()<w) o.width(w)
    if(!o.hasClass('pp-navigation-long'))
        o.css({left:($(this).position().left-2)+'px'})
    return $(this);
}





/* -------------------------------------------------- *
 * Project scripts
 * -------------------------------------------------- *
 * Author: HELLO WORLD
 * URL: http://helloworld.by/
 * Copyright: 2011 HELLO WORLD
** -------------------------------------------------- */

function openPopup(id,l,t){
	var	lb = $('.navigation__ul:first');
	var b = $('.blacker');
	b.height(Math.max($(document.body).height(),$('#minWidth').height())).stop(true,false).fadeTo(200,0.2);
	var obj = $('#'+id);
	obj.width(l).parents('.rounded:first').fadeIn().css({top:t+'px',left:(lb.offset().left-5+(lb.width()-64-l)/2)+'px',width:(l+74)+'px'}).find('.float_right:first').click(function(){b.fadeOut();$(this).parents('.over_black:first').fadeOut();return false;});
    obj.find('input:first').focus()
	if($.donkey)$.rounded_over_black_fix();
}

$.fn.mouse_connect = function(){

	var obj = $(this).css({'position':'absolute','z-index':101001,overflow:'hidden'});
	var w = obj.width();

	$(document).mousemove(function(e){
		var more = (e.pageX+15+w-$(document.body).width());
		if (more>0) obj.width(w-more); else obj.width(w)
		
		obj.css({
			left:e.pageX+15,
			top:e.pageY+20
		})
	})
	
	return obj;

}

$.fn.loader = function(){
	var donkey = ($.browser.msie && $.browser.version<8);
	$(this).html('<span></span><span></span><span></span>').find('span').css({
		'margin-right':'3px',
		display:'block',
		'float':'left',
		height:'9px',
		width:'9px',
		background:'url(/images/global/'+(donkey?'d':'b')+'.png) no-repeat left'
	})
	var n = 0;
	$(this).find('span').each(function(){
		$(this).fadeTo(1,(3-n)/3);
		
		if (n==2) $(this).css({'margin-right':0});
		++n;
	})
	
	var $loader = $(this);
	var step = 0;
	var interval = setInterval(function(){
		step=(step+1)%3;
		var n = 0;
		$loader.find('span').each(function(){
			var f = (3-n);for(var i=0;i<step;i++) f=(f%3)+1;
			$(this).fadeTo(200,f/3);++n;
		})
		
	},200);
	$(this).data('loader',interval);
}
$.fn.stoploader = function(){
	clearInterval($(this).data('loader'));
}

$.width_normalize = function(){} 
$.width_update1 = function(){}
$.width_update2 = function(){}
$.width_update3 = function(){}
$.reinitaccesories = function(){}
$.absolute_right = function(){}
$.dogPositionFunction = function(){}
$.rounded_over_black_fix = function(){
	$('.rounded.over_black:visible').each(function(){
		var a = $(this).stop(true,false).animate({left:(($(document.body).width()-$(this).width())/2)},200,'linear');
        if (!$.donkey) a.fadeTo(0,1)
	})
}

$(function(){
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/Android/i))) {
		$('#minWidth').css({'min-width':'1159px'});
		$('.nv-shd').css({'margin-left':'-480px'})
		$('body').addClass('body-touchdevice');
	}
	if($('.headlines-txt').length) {
		$('.headlines-txt').bind({
			mouseenter:function(){
				$(this).parents('.headlines__li').addClass('active-headline');
			},
			mouseleave:function(){
				$(this).parents('.headlines__li').removeClass('active-headline');
			}
		})
	};
	
	windowres();
	
	if($('.navigation__ul').length) {
		mainnav();
	};
	
	
	
	
	
	var hide_drop_timer = false;
	
	$('.othercats-drop').each(function(){
		
		var obj = $(this).parent();
		var func = function(){			
			if (obj.data('hide_drop_timer')) {
				clearTimeout(obj.data('hide_drop_timer'));
				obj.data('hide_drop_timer', false);
			}
			obj.addClass('active-drop');
			return false;
		}
		
		$(this).bind($(this).parent().hasClass('clicker')?'click':'mouseover', func);
		$(this).next().bind('mouseover',func);
	})
	
	
	$('.othercats__li').bind('mouseleave click', function(){
		if ($(this).data('hide_drop_timer')) return false;
		var obj = $(this);
		$(this).data('hide_drop_timer', setTimeout(function(){obj.data('hide_drop_timer',false);obj.removeClass('active-drop');},300));
	}).data('hide_drop_timer',false)
	
	$('.b-search .zoom').click(function(){
        $(this).parents('.b-search:first').toggleClass('active-ppsearch')
    });
	
	$('.b-search-extend .f-cb').change(function(){
		$('.search__ip input').focus();
	});
	
	if ($('.navi').length) {		
	
		window.n = 0;
		
		$('.navi').each(function(){
			$(this).find('a').each(function(ind){
				$(this).attr('num',ind);
			});
		});
		$('.navi a').click(function(){
			if (window.animationFlag) {
				return false;
			}
			if ($(this).hasClass('selected')) {
				return false;
			}
			window.n = parseInt($(this).attr('num'));
			homeSwitch();
			return false;
		});
		$('.navi a,.ar-r,.prev-page,.ar-l,.next-page').mousedown(function(){
			window.homeSwitches = window.clearInterval(window.homeSwitches);
		});
		$('.ar-l,a.prev-page').click(function(){
			if (window.animationFlag) {
				return false;
			}
			if (window.n == 0) {
				window.n = $('.navi:visible a').length - 1;
			} else {
				window.n--;
			}
			homeSwitch();
			return false;
		});
		$('.ar-r,a.next-page').click(function(){
			if (window.animationFlag) {
				return false;
			}
			if (window.n == $('.navi:visible a').length - 1) {
				window.n = 0;
			} else {
				window.n++;
			}
			homeSwitch();
			return false;
		});
		

		window.homeSwitches = window.setInterval("homeNextSwitch();",5500);
	}
		
	
	
	
	
});

function windowres() {

	var $t = $(window),
		$d = $(document);
		$body = $('body'),
		$inner = $('.inner'),
		mainpage = false,
		$under = $('#under_all2,#under_all');
		if($('#minWidth').hasClass('main-page')) {
			mainpage = true;
		}
	
	$t.bind('resize', function(){

		w = $t.width();
		if(w < 1152) {
			setW = 960;
		}
		else if((w >= 1152) && (w < 1350)) {
			setW = w - 192;
		}
		else {
			setW = 1350 - 192;
		}
		
		$inner.css('width',setW + 'px');
		$(function(){$under.height($('#minWidth').height()-345);});
		
		$.width_update1();
		$.width_update2();
		$.width_update3();
		$.width_normalize();
		$.reinitaccesories();
		$.absolute_right();
		$.rounded_over_black_fix();
		$.dogPositionFunction();
		
		
		if(mainpage) {
			$('.b-navigation, .mainvisual-slider,.nv-shd').css('margin-left', Math.round(setW / -2) + 'px');
			$('.headlines__li').css('width', Math.round((setW - 42) / 3) + 'px');
		}
        if(mainpage || $.detail) {
			$('.b-mainvisual .ar-l').css('margin-left', (Math.round(setW / -2)-10) + 'px');
			$('.b-mainvisual .ar-r').css('margin-left', Math.round(setW / 2 - 18) + 'px');
		}
	});
	$t.resize();
};

function mainnav() {
	var lnav = $('.navigation__ul'), ltimer, ctimer, aNav = false, aNav__idx, catdrop = 0;
	
	$('.pp-navigation',lnav).parent().addClass('sfNav');
	
	var lnav__li = $('.navigation__li,.navigation-cart',lnav);
	
	if($('.nav-hr',lnav).length) {
		aNav = true;
		aNav__idx= $('.nav-hr',lnav);
	}
	
	$('.navigation__a',lnav).bind('mouseenter',function(){
		if((!$(this).parent().hasClass('nav-sbnav-active')) && ($(this).parent().hasClass('sfNav'))) {
			openNav(this);
		}
	});
	
	lnav__li.bind({
		mouseover:function(){
			clearTimeout(ltimer);
			if (!$(this).hasClass('nav-active') && !$(this).hasClass('sfNav')) {
				$('.sfNav').removeClass('nav-hr nav-sbnav-active');
				$(this).addClass('nav-hover');
			}
		},
		mouseleave:function(){
			hideNav();
			$(this).removeClass('nav-hover');
		}
	});
	
	function openNav(obj) {
		clearTimeout(ltimer);
		var nlnk = $(obj).parent();
		$('.navigation__li',lnav).removeClass('nav-sbnav-active nav-hr');
		nlnk.addClass('nav-hr')
		if(nlnk.hasClass('sfNav')) {
			nlnk.addClass('nav-sbnav-active').reposition().parents('.b-navigation:first').addClass('super-z');
		}
	};
	function hideNav() {
		ltimer = setTimeout(function(){
			lnav__li.removeClass('nav-sbnav-active nav-hr').parents('.b-navigation:first').removeClass('super-z');
			if(aNav) {
				aNav__idx.addClass('nav-hr')
				if(aNav__idx.hasClass('sfNav')) aNav__idx.addClass('nav-sbnav-active')
			}
		},700);
	};
	
	if(!navigator.userAgent.match(/MSIE 6.0/i)) {	
	
		$('.catdrop',lnav).bind({
			mouseenter:function(){
				clearTimeout(ctimer);
				openCatNav(this);
			},
			mouseleave:function(){
				hideCatNav(this);
			}
		});
		
		function openCatNav(obj) {
			$('.catdrop',lnav).removeClass('active-catdrop');
			var $t = $(obj);
			catdrop = $('.catdrop',lnav).index($t);
			$t.addClass('active-catdrop');
			var $hrf = $t.attr('rel');
			$('.catnav-lvl',lnav).hide();
			$('#'+$hrf).show();
			$('.pp-catnav',lnav).addClass('pp-catnav-2lvl');
			
			var o = $t.parents('.pp-navigation-i:first').css({width:'auto'});
            var w = 20;
            o.children(':visible').each(function(){w+=$(this).width();});
            o.width(w);
		};
		
		function hideCatNav(obj) {
			ctimer = setTimeout(function(){
				var $t = $(obj);
				var $hrf = $t.attr('rel');
				$t.removeClass('active-catdrop');
				$('#'+$hrf).hide();
				$('.pp-catnav',lnav).removeClass('pp-catnav-2lvl');
				
				$t.parents('.pp-navigation-i:first').css({width:'auto'})
			},300);
		};
		
		$('.catnav-lvl',lnav).bind({
			mouseover:function(){
				clearTimeout(ctimer);
				$(this).show();
				$('.catdrop',lnav).eq(catdrop).addClass('active-catdrop');
				$('.pp-catnav',lnav).addClass('pp-catnav-2lvl');
			},
			mouseleave:function(){
				hideNav();
			}
		});

	}
		
};

function homeSwitch() {
	$('.navi:visible a.selected').removeClass('selected');
	$('.navi:visible').find('a:eq(' + window.n + ')').addClass('selected');
	if ($('.b-mainvisual').hasClass('mainvisual-newitems')) {
		var o = $('.slider-newitems-i table:visible');
		if (o.length>0)window.animationFlag = 1;
		o.fadeOut(100,function(){
			$('.slider-newitems-i table:eq(' + window.n + ')').fadeIn(600,function(){
				window.animationFlag = 0;
			});
		});
	} else {
		$('#main-announce .a:visible').addClass('to-be-hidden').css('z-index',5);
		var obj = $('#main-announce .a:eq(' + window.n + ')');
		var ua2=$('#under_all2').css('background',obj.attr('rel'));
		window.animationFlag = 1;
		var ua1=$('#under_all').fadeOut(500,function(){
			ua1.css('background',obj.attr('rel')).fadeIn(1);
			window.animationFlag = 0;
		})
		if (obj.hasClass('white-back')) {
			$('.mainvisual-slider, .b-mainvisual').addClass('black-links');
			$('.md-header').removeClass('inver-black-links');
		} else {
			$('.mainvisual-slider, .b-mainvisual').removeClass('black-links');
			$('.md-header').addClass('inver-black-links');
		}
		$('#main-announce .a:eq(' + window.n + ')').css('z-index',10).fadeIn(500,function(){
			$('.to-be-hidden').removeClass('to-be-hidden').hide();
			window.animationFlag = 0;
		});
		$('.slider-newitems-i table:eq(' + window.n + ')').fadeIn(600);
	}
}

function homeNextSwitch() {
	var o = $('#main-announce>.a:visible');
    if (o.next().length==0 || (typeof($.images)=='object' && $.images[o.index()+1]))
	    $('.ar-r').click();
}
	
function homeReset() {
	if (window.animationFlag) {
		window.setTimeout("homeReset()",500);
		return false;
	}
	var ma=$('#main-announce');
	ma.children('*').hide();
	ma.children(':first').show();
	$('.slider-newitems-i table:visible').hide();
	$('.slider-newitems-i table:eq(0)').show();
	$('.mainannounce__ul').css({marginLeft: 0});
	$('.navi a.selected').removeClass('selected');
	$('.navi').each(function(){
		$(this).find('a:eq(0)').addClass('selected');
	});
	window.homeSwitches = window.clearInterval(window.homeSwitches);
	window.homeSwitches = window.setInterval("homeNextSwitch();",3000);
}

$.fn.addSpaces = function(){
	$(this).val($(this).val().replace(' ',''));

	if ($.trim($(this).val()).indexOf(' ') != -1) {
		nStr = $(this).val().replace(' ','') + '';
	} else {
		nStr = $(this).val() + '';
	}
	if (isNaN(parseInt(nStr))) {
		nStr = $(this).attr('longdesc') != "" ? $(this).attr('longdesc') : 1000;
	} else {
		nStr = parseInt(nStr);
		$(this).attr('longdesc',nStr + '');
	}
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	$(this).val(x1 + x2);
	$(this).val($.trim(x1 + x2));
}

$.fn.addSpacesDiv = function(){
	nStr = $(this).text() + '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	$(this).text(x1 + x2);
}

function catalogListReload() {
	
	// /*???????????? ?????? ??????? ? ???????*/
	
}

$(document).bind('keydown',function(e){ if (e.keyCode==27){ $('#lightbox .h.x,.popup .float_right').click(); return false;}  })

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((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);
}

function set5minCookie(name,value){

    var d1 = new Date();
    d1.setTime(d1.getTime() + (5*60*1000));

    setCookie(name,value,d1);

}
function set1000dayCookie(name,value){

    var d1 = new Date();
    d1.setTime(d1.getTime() + (1000*24*60*60*1000));

    setCookie(name,value,d1);

}

$(function(){
    var x = $('.btp-wrap').children('li:not(.call-me)').mouseover(function(){
        var obj = $('#over-top-phones');
        var ph  = $(this).parents('.b-topphones:first');
        obj.css({
            width:ph.width()-95+($.browser.opera?60:0)
        }).show().unbind('mouseleave').unbind('mouseleave').mouseleave(function(){
                $(this).hide();
            })
    })

    var ph = getCookie('phone');
    ph = ph==null? 2: parseInt(ph);
    if (ph>0)
        setTimeout(function(){
            ph--;
            set1000dayCookie('phone',ph);
            x.trigger('mouseover');
            var y = $('#over-top-phones');
            y.mouseover(function(){clearTimeout(z);})
            var z = setTimeout(function(){y.trigger('mouseleave');},1500);
        },300)
})
