var layout = {};

(function($) {

$(document).ready(function() 
{
    var mapbox = {
        visible: false,
        click_on_panel: false
    };       
    
    $('body').click(function()
    {
        if(!mapbox.click_on_panel)
        {
            $('#mapbox').css('display', 'none');
            $('#mapbox_show').removeClass('active');
        }                   
        mapbox.click_on_panel = false;
    })
    
    $('#mapbox').click(function() { mapbox.click_on_panel = true; })
    
    $('#mapbox_show').click(function()
    {
       if ($('#mapbox').css('display')=='none') {
			$(this).toggleClass('active');
			//$('#mapbox').toggle();
			//var display = $('#mapbox').css('display');
        
        	$('#mapbox').animate({ opacity: "show" }, "slow");

        } else {
 			$(this).toggleClass('active');
			$('#mapbox').toggle();
			var display = $('#mapbox').css('display');
        }
        
        mapbox.click_on_panel = true; 
   });
	
	
                 
    $('#prn').click(function()
    {
    	//var el = document.getElementById('printmap');
    	//el.href='/css/prnintmap.css';
   		
    	window.print();
    	//el.href='/css/style.css';
    	
    	return false;
    }); 
                
    $('#close').click(function()
    {
            $('#mapbox').css('display', 'none');
            $('#mapbox_show').removeClass('active');
        return false; 
    });             
	

/*
   $('#site_map_show').click(function()
   {
       $(this).toggleClass('active');
       $('#site_map').toggle();
       $('#mapbox_show').toggle();
       site_map_panel.click_on_panel = true;
       $('#mapbox').css('display', 'none');
   });

    if (jQuery.browser.opera && jQuery.browser.version >= 9.8)
    {
        $('#search_submit').css('height', '31px');
    }
*/    

    // preload
    var imageObj = new Image();
    var images = new Array();
    images[0] = '/i/mapbox_top.png';
    images[1] = '/i/mapbox_bottom.png';
    images[2] = '/css/prnintmap.css';
    
    for(var i = 0; i < images.length; i++) 
    {
        imageObj.src = images[i];
    }
	
	
	/**
	 * Functions to save, read and erase cookies
	 */

	function createCookie(name, value, days) {
		var date, expires;
		if (days) {
			date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = "; expires=" + date.toGMTString();
		}
		else {
			expires = "";
		}
		document.cookie = name + "=" + value + expires + "; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		var i, c;
		for (i = 0; i < ca.length; i += 1) {
			c = ca[i];
			while (c.charAt(0) === ' ') {
				c = c.substring(1, c.length);
			}
			if (c.indexOf(nameEQ) === 0) {
				return c.substring(nameEQ.length, c.length);
			}
		}
		return null;
	}

	function eraseCookie(name) {
		createCookie(name, "", -1);
	}


	/**
	 * ????????? ?????? ???????? <code>elem</code> ??????.
	 * ???? <code>elem</code> ?? ???????? ?????????, ?? ???????????? <code>this</code>
	 * 
	 * @param {Element} elem ??????? ? ??????? ????? ???????? ??????
	 * @param {String} [prefix] ??????? ??? ???? ??????? ????????
	 */
	function wrapInCorners(elem, prefix) {
		elem = (elem && elem.nodeType) ? elem : this;
		var p = (typeof prefix === 'string') ? prefix + '-' : '';

		$(elem)
			.append('<ins class="' + p + 'cn ' + p + 'tl" /><ins class="' + p + 'cn ' + p + 'tr" />')
			.append('<ins class="' + p + 'cn ' + p + 'bl" /><ins class="' + p + 'cn ' + p + 'br" />');
	}

	// ?????? ?????? ??? ?????????
	$('.frame').each(wrapInCorners);

	// ?????? ??????????? ?????? ??? ??????? ? ?????
	$('.products_cart > li, .products_list > li, dl.tabs .button').each(function () {
		wrapInCorners(this, 'crt');
	});

	$('dl.tabs > dd').each(function () {
		wrapInCorners(this, 'tab');
	});
	
	//??????? ?????????? ??????
	layout.wrapInCorners = wrapInCorners;


	/**
	 * ?????????? ?? ??????? <code>elem</code> ??????? ??? ???????????? ??????.
	 * ???? <code>elem</code> ?? ???????? ?????????, ?? ???????????? <code>this</code>
	 * 
	 * @param {Element} elem ??????? ?? ??????? ????? ???????? ???????
	 * @param {String} [prefix] ??????? ??? ???? ??????? ??????
	 */
	function makeHover(elem, prefix) {
		elem = (elem && elem.nodeType) ? elem : this;
		var p = (typeof prefix === 'string') ? prefix + '_' : '';

		$(elem).bind('mouseover focus', function () {
			$(this).addClass(p + 'hover');
		});

		$(elem).bind('mouseout blur', function () {
			$(this).removeClass(p + 'hover');
			$(this).removeClass(p + 'down');
		});
	}

	/**
	 * ?????????? ?? ??????? <code>elem</code> ??????? ??? ???????????? ???????.
	 * ???? <code>elem</code> ?? ???????? ?????????, ?? ???????????? <code>this</code>
	 * 
	 * @param {Element} elem ??????? ?? ??????? ????? ???????? ???????
	 * @param {String} [prefix] ??????? ??? ???? ??????? ???????
	 */
	function makeDown(elem, prefix) {
		elem = (elem && elem.nodeType) ? elem : this;
		var p = (typeof prefix === 'string') ? prefix + '_' : '';

		$(elem).mousedown(function () {
			$(this).addClass(p + 'down');
		});
		$(elem).keydown(function (event) {
			if (event.keyCode === 13) {
				$(this).addClass(p + 'down');
			}
		});

		$(elem).mouseup(function () {
			$(this).removeClass(p + 'down');
		});
		$(elem).keyup(function (event) {
			if (event.keyCode === 13) {
				$(this).removeClass(p + 'down');
			}
		});
	}

	// ?????????? ?????? ? ??????? ?? ??????
	$('.frame_button_1, .frame_button_2').each(function () {
		makeHover(this, 'frame_button');
		makeDown(this, 'frame_button');
	});


	/**
	 * ????????? ????????? input type="search" ??? ? ??????
	 * 
	 * @param {Element} elem ???? ?????
	 * @param {String} [class_empty] ????? ??? ??????? ???? ?????
	 */
	function makePlaceholder(elem, class_empty) {
		class_empty = (typeof class_empty === 'string') ? class_empty : 'empty';

		$(elem).focus(function () {
			if (this.value === $(this).attr('placeholder')) {
				this.value = '';
			}
			$(this).removeClass(class_empty);
		});

		$(elem).blur(function () {
			if (!this.value.length) {
				this.value = $(this).attr('placeholder');
				$(this).addClass(class_empty);
			}
		});

		if (!elem.value.length) {
			$(elem).blur();
		}
	}

	// ? ?????? placeholder ? ??? ????????
	if (!$.browser.safari) {
		$('input[placeholder]').each(function () {
			makePlaceholder(this);
		});
	}


	/**
	 * ?????? ????????????
	 */
	var makeUserBalance = function () {

		var balance = $('#user_balance');

		if (readCookie('balance') === 'disabled') {
			balance.addClass('disabled');
		}

		balance.find('.switcher').click(function () {
			if (balance.hasClass('disabled')) {
				balance.removeClass('disabled');
				eraseCookie('balance');
			}
			else {
				balance.addClass('disabled');
				createCookie('balance', 'disabled', 365);
			}
		});

	}();


	/**
	 * ???????? ? ??????
	 */
	var makeFooterProducts = function () {

		var footer = $('#footer.products_tape');
		var items = footer.find('.switch li');
		var selected_item = items.filter('.selected');
		var images = footer.find('.images');

		items.each(function () {
			var that = $(this);
			$(this).find('a').click(function () {

				if (selected_item.hasClass('framed')) {

					selected_item.removeClass('framed selected');
					selected_item.children('span').removeClass('frame frame1 r10');

					that.addClass('framed selected');
					that.children('span').addClass('frame frame1 r10');

					that.append(selected_item.children('ins'));
					that.children('span').append(selected_item.children('span').children('ins'));

				}
				else {

					selected_item.removeClass('selected');
					that.addClass('selected');
					that.append(selected_item.children('ins'));

				}

				selected_item = that;

				images.load('/footer-products/', {type: $(that).attr('product-type')});

				return false;
			});
		});

	}();
	
    
});

})($);
