/**
 * header, footer, search results left panel, shopping cart right panel
 */
var cartIsActive;

jQuery(document).ready(function($){	

	$.initSelectMenus();

	/*--Event handler on Change for Language select list & Set cookie for Language select list--*/
	$("select#lang").change(function(){
		var urlVars = getUrlVars();

		var loc = AppendParamToURL(urlVars, 'lang', $("select#lang").val());

		//calculate location using #panorama param
		var anchorVars = getAnchorVars();
		if (anchorVars["panorama"]) {
			loc = AppendParamToURL(urlVars, 'panoramaId', anchorVars["panorama"]);
		}

		document.location.href = loc;
		setCookieSafe($,'cookie_lang', $("select#lang").val(), {expires: 30});
	});

	/*--Making handler for "map View" onChange--*/
	$("select#mapView").change(function(){
		setCookieSafe($, 'cookie_mapView', $("select#mapView").val(), { expires: 30 });
		mapViewCheck($,'','','');
	});

	$("#search_results_but,.openSearchPanelBtn").click(function() {
		if (search_results_enable) {
			$.search_results_push();
			setCookieSafe($, 'cookie_mapView', 'map', { expires: 30 });
		} else  {
			$.search_results_pop();
			setCookieSafe($, 'cookie_mapView', 'list_map', { expires: 30 });
		}
		google.maps.event.trigger(map, "resize");
	});

	$("#toolbar_cart_link").click(function() {
        if (!shopping_cart_enable) {
            $.shopping_cart_pop();
            $(".but_cart").click();
            cartIsActive = true;
        } else {
            $.shopping_cart_push();
        }
    });
    $("#shopping_cart_but").click(function() {
        $.shopping_cart_push();
    });
});

(function ($){
	$.initSelectMenus = function() {
		$('select#mapView').selectmenu('destroy');
		$('select#lang').selectmenu('destroy');

		$.urlVars = getUrlVars();

	    /*--Get data from Cookie for map View--*/
        var mapView=getCookieSafe($, 'cookie_mapView');
        setSelectedViewMode($, mapView);

	   /*--Get data from Cookie for Language--*/
		var lang = getCookieSafe($, 'cookie_lang');
		$.setSelectedLanguage(lang);

		if ($.urlVars["lang"] != undefined) {
			if ($.urlVars["lang"][0].toLowerCase() != lang) {
				$.setSelectedLanguage($.urlVars["lang"][0]);
				setCookieSafe($,'cookie_lang', $("select#lang").val(), {expires: 30});
			}
		}

		/*--Calculating of max length text for mapView options --*/
		var maxLength=0;
		$("#mapView [value]").each(function(){
			if(maxLength<this.text.length){maxLength=this.text.length;}
		});

		/*--Create Select list for map View--*/
		$('select#mapView').selectmenu({
			style : 'dropdown',
			width: maxLength*11.5,
			menuWidth: maxLength*11.5,
			maxHeight: 300,
			icons: [
				{find: '.list'},
				{find: '.list_map'},
				{find: '.map'}
			]
		});

		/*--Create Select list for Language--*/
		$('select#lang').selectmenu({
			style : 'dropdown',
			width: 110,
			maxHeight: 300,
			icons: [
				{find: '.en'},
				{find: '.fr'},
				{find: '.it'},
				{find: '.de'},
				{find: '.es'},
				{find: '.pt'},
				{find: '.ja'},
				{find: '.ru'},
				{find: '.ua'}
			]
		});
	};
    $.setSelectedLanguage = function(lang) {
        if (lang == undefined
                || $.inArray(lang,['en','de','fr','it','es','pt','ja','ru','ua']) == -1) {
            lang = (envDomainRegion == 'mx') ? 'es' : 'en';
        }
        $("#lang [value='"+lang+"']").attr("selected", true);
    };

	search_results_enable = false;
    shopping_cart_enable = false;
    shopping_cart_width = 300;

    $.search_results_pop = function()
    {
        $("#search_results").removeClass("hide") ;
        $(".openSearchPanelBtn").addClass("hide");
        search_results_enable=true;
		$.initSelectMenus();
    };
    $.search_results_push = function()
    {
        $("#search_results").addClass("hide");
        $(".openSearchPanelBtn").removeClass("hide");
        search_results_enable=false;
		$.initSelectMenus();
    };
	$.shopping_cart_pop = function()
	{
		$("#shopping_cart").removeClass('hide');
		shopping_cart_enable = true;
	};
	$.shopping_cart_push = function()
	{
		$("#shopping_cart").addClass('hide');
		shopping_cart_enable = false;
	};
})(jQuery);

function setSelectedViewMode(jQuery, mapView) {
    if (mapView == null) {
        mapView = 'list_map';
    }

    var index = jQuery("#mapView option").index(jQuery("#mapView [value='" + mapView + "']"));

    if (!jQuery("select#mapView").selectmenu) {
        jQuery = spareJQueryInstance;
    }
    if (jQuery("select#mapView").selectmenu) {
        jQuery("select#mapView").selectmenu("value",index);
    }    
    jQuery("#mapView").val(mapView);
}

function clearItemTypeCookie(jQuery) {
    setCookieSafe(jQuery, 'item_type', null, {});
}

function displayDetailsBlock(jQuery) {
    if (jQuery("select#mapView").val() != "list") {
        jQuery("#detailBlock").addClass("hide");
        jQuery(".gotoStreetPanoramaLink").removeClass("hide");
        jQuery(".fitMapBoundsBtn").removeClass("hide");
    } else {
        jQuery("#detailBlock").removeClass("hide");
        jQuery(".gotoStreetPanoramaLink").addClass("hide");
        jQuery(".fitMapBoundsBtn").addClass("hide");
    }
}

function toggleSearchInShopPanelButton(jQuery, shopId) {
    if (isEmptyString(shopId)) {
        jQuery(".inShopPanelBtn").addClass("hide");
    } else {
        jQuery(".inShopPanelBtn").removeClass("hide");
    }
}

function toggleSearchWithingVisibleMap(show) {
    if (show) {
        jQuery("#searchInsideMap").removeClass("hide");
        jQuery(".insideMapText").removeClass("hide");
    } else {
        jQuery("#searchInsideMap").addClass("hide");
        jQuery(".insideMapText").addClass("hide");
        jQuery("#searchInsideMap").removeAttr("checked");
    }
}

function mapViewCheck(jQuery,zoom,centerLat,centerLng) {

    if (jQuery("#placeForMap").is(":hidden")) {
        jQuery("#placeForMap").show();
    }

    if (jQuery("#placeForMap").outerWidth() == 0) {
        jQuery("#placeForMap").animate({width: '100%' }, 400, 'linear')
    }

    if (jQuery("select#mapView").val() == "map") {
        if (search_results_enable) {
            jQuery.search_results_push();
        }
		initResultOnMap(zoom,centerLat,centerLng);
		toggleSearchWithingVisibleMap(true);
    }

    if (jQuery("select#mapView").val() == "list_map") {
        if (!search_results_enable) {
            jQuery.search_results_pop();
        }
		initResultOnMap(zoom,centerLat,centerLng);
		toggleSearchWithingVisibleMap(true);
    }

    if (jQuery("select#mapView").val() == "list") {
        if (!search_results_enable) {
            jQuery.search_results_pop();
        }
		initResultOnMap(zoom,centerLat,centerLng);
		jQuery("#placeForMap").fadeOut();
        toggleSearchWithingVisibleMap(false);
    }

    displayDetailsBlock(jQuery);
}
