$(document).ready(function(){
	
	$(document).keyup(function(e) {
		if (e.keyCode == 27) {
			$.unblockUI();
		} 
	});

	$('#searchGoogle a').click(function(){
		 $("#searchGoogle").submit();
	});

	$('#goCalc').click(function(){
		 $("#calcForm").submit();
	});
	
	$(function(){ // костыли для багов IE (из-за тени)
		if (($.browser.msie) && ($.browser.version <= '7.0')) {
			alert('К сожалению, у Вас очень старый браузер IE ' + $.browser.version + '!' + '\n' + 'Сайт может отображаться некорректно!');
			$('#vendor').hide();
			$('#vendorSlider').hide();		
			$('#kab').css('right', '-7px');
			$('.more').css('left', '-11px');
			$('.slide').css('margin-left', '10px');
			$('.vSlide').css('margin-left', '8px');
			/* input IE bag */
			$('input, textarea, div .bh2, #bcity, #liveCam, select, .titlel1').css({'position' : 'relative', 'left' : '7px'});
		}
		else{
			if(($.browser.msie) && ($.browser.version == '8.0')){
				$('#kab').css('right', '-7px');
				$('.more').css('left', '-11px');
				$('.slide').css('margin-left', '10px');
				$('.vSlide').css('margin-left', '8px');
				/* input IE bag */
				$('input, textarea, div .bh2, #bcity, #liveCam, select, .contl2').css({'position' : 'relative', 'left' : '7px'});
			}
			$('#vendorSlider').slides({
				preload: true,
				generateNextPrev: false,
				generatePagination: false
			});			
		}
	});

	$(function(){
		$('#slides').slides({
			preload: true,
			preloadImage: '/img/load.gif',
			generatePagination: false,
			play: 8000,
			pause: 3000,
			hoverPause: true,
			animationStart: function(current){
				$('.caption').animate({
					bottom:-35
				},100);
			},
			animationComplete: function(current){
				$('.caption').animate({
					bottom:0
				},300);
			},
			slidesLoaded: function() {
				$('.caption').animate({
					bottom:0
				},300);
			}
		});
	});

	function ModalWindow(_url_){

		var _w_ = $(window).width();
		var _l_ = Math.floor(((_w_ - 500) / 2 ) / _w_ * 100);
		_css_ = { top: '50px', width: '510px', left: _l_ + '%' };

		$.blockUI({ 
            message: $('#modal'),
			css: _css_
        }); 
		$.ajax({ 
			url: '/modal' + _url_,
			cache: false,
			beforeSend: function(){
				$(".modalContent").html('<center><img class="process" src="/img/load.gif"></center>');
			},
			success: function(answ){
				$(".process").hide();
				$(".modalContent").append(answ);
			}
		});
	}

	$('.order').click(function() {
		var _id_ = $(this).attr('id');
        ModalWindow('/order.php?id=' + _id_);
	}); 

	$('.pInfo').click(function() {
		var _id_ = $(this).attr('id');
        ModalWindow('/priceInfo.php?id=' + _id_);
	}); 

	$('.softInfo').click(function() {
		var _id_ = $(this).attr('id');
        ModalWindow('/softInfo.php?id=' + _id_);
	});

	$('.ipPort').click(function() { 
        ModalWindow('/checkPort.php');
	}); 

	$('#subscribe, #ss').click(function() { 
		ModalWindow('/subscribe.php');
	});

	$('.login').click(function() { 
		ModalWindow('/login.php');
	});
	
	$('.test').click(function() { 
		ModalWindow('/test.php');
	});

	$('.join').click(function() {
		ModalWindow('/rule.php');
	}); 

	$('.remind').click(function() {
		ModalWindow('/remind.php');
	}); 

	$('.close').click(function() { 
        $.unblockUI();
	});

	$('.nTitle').hover(
		function () {
			$(this).parents(".news").css("background", "lightyellow");
		},  
		function () { 
			$(this).parents(".news").css('background', 'transparent'); 
	});
	
	$("#kabLink").click(function()	{
		$('#kab').hide('300'); 
	});

	$('#enter').click(function() {
		var isHide = $('#kab').css("display");
		if (isHide == 'block'){
			$('#kab').hide('300');
		}else{
			$('#search').attr('value', '');
			$('#kab').show('300');
		}
	});

	$('#left div.ml').click(function()	{
		var getId = $(this).attr("id");
		$('#left div.sm').slideUp();
		var listId = $('#smenu-' +getId).css('display');
		if (listId == 'block'){
			$('#smenu-' +getId).slideUp();
			$(this).css({'background':'url(/img/mgreen.png) no-repeat', 'background-position': 'top right'});
		}else{
			$('#smenu-' +getId).slideDown();
			$(this).css({'background':'url(/img/mgreena.png) no-repeat', 'background-position': 'top right'});
		}		
	});

	$(".vSlide img").mouseover(
		function () {
			var imgSrc = $(this).attr("src");
			var imgSrcA = imgSrc.replace('.jpg','-a.jpg');
			$(this).attr('src', imgSrcA).show(); 
	}); 
	$(".vSlide img").mouseout(
		function () { 
			var imgSrc = $(this).attr("src");
			var imgSrcA = imgSrc.replace('-a.jpg','.jpg');
			$(this).attr('src', imgSrcA).show(); 
	});
	
	$("#subscribe").hover(
		function () {
			$('#si').attr('src', '/img/subsb.png').show('300'); 
		},  
		function () { 
			$('#si').attr({src: "/img/subs.png"}); 
	});
		
	$("td.m").hover(
		function () {
			$(this).css("background", "url(/img/mbga.png) repeat-x" );  
		},  
		function () { 
			$(this).css("background", "url(/img/mbg.png) repeat-x" );
	});

	$('input#search, input#se').focus(function() {
		$(this).attr('value', '');
	});
	
	if($.cookie("actCountry")) {
		var phone = $.cookie("actPhone");
		var country = $.cookie("actCountry")
		$('#number, #hnumber').html(phone);
		$('#rUp, #rFoot').html(country);
	}

	$('.reg').click(function()	{
		var aReg = $(this).html();
		var activ = $(this).parent().attr('id');
		if (activ == 'r1')	{
			var aPhone = ['8 800', '200 1666'];
		}
		else if (activ == 'r2')	{
			var aPhone = ['0 800', '50 12 30'];
		}
		else if (activ == 'r3')	{
			var aPhone = ['8 800', '200 1666'];
		}
		else if (activ == 'r4')	{
			var aPhone = ['8 820', '007 11 666'];
		}
		else if (activ == 'r5')	{
			var aPhone = ['7 861', '217 0036'];
		}
		else if (activ == 'r6')	{
			var aPhone = ['7 861', '217 0036'];
		}
		else if (activ == 'r7')	{
			var aPhone = ['0 800', '6 14 65']; 
		}
		$.cookie("actCountry", null);
		$.cookie("actCountry", aReg, { path: "/" }); 
		

		$('#selectRegion').hide('300');

		var phone = '<div class="fn1">' + aPhone[0] + '</div><div class="fn2">' + aPhone[1] + '</div>';
		$.cookie("actPhone", null);
		$.cookie("actPhone", phone, { path: "/" });

		$('#number, #hnumber').html(phone);
		$('#rUp, #rFoot').html(aReg);
	});

	$('.region').click(function() { 
		var isHide = $('#selectRegion').css("display");
		if (isHide == 'block'){
			$('#selectRegion').slideUp();;
		}else{
			$('#selectRegion').slideDown();
		}
	}); 

	
});

jQuery.cookie = function (key, value, options) {
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);
        if (value === null || value === undefined) {
            options.expires = -1;
        }
        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        value = String(value);
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', 
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
