// 

var Agroma = {
	dealer_on : true,
	service_on : true,
	region : "dolnośląskie,kujawsko-pomorskie,lubuskie,łódzkie,lubelskie,mazowieckie,małopolskie,opolskie,podlaskie,podkarpackie,pomorskie,śląskie,świętokrzyskie,warmińsko-mazurskie,wielkopolskie,zachodniopomorskie".split(","),
	regions_on : false,
        mapArray: [15,10,13,8,2,14,1,5,0,3,4,7,11,6,12,9],
	
	Region_Map : function() {
        var region = this.region;
        var Unpolish = this.Unpolish;
        var Swap = this.Swap;
        var mapArray = this.mapArray;
		$("#imgmap area").each(function(counter){
			$(this).attr("href", "/regiony/pokaz/" +Unpolish(region[Swap(counter, mapArray)]));
			$(this).bind("mouseover", function(){
				$("#my_img").css("backgroundPosition", "-"+(134*(counter+1))+"px 0px");
			}).bind("mouseout", function(){
				$("#my_img").css("backgroundPosition", "0px 0px");
			});
		});
	},
	
	Bind_Search : function() {
		$("#regions").html("");
		for(var i = 0; i < this.region.length; i++) 
			$("<li rel="+(i+1)+">"+this.region[i]+"</li>").appendTo("#regions");
		
	},

        Unpolish : function(s){
            var x="żźćńąśłęóŻŹĆŃĄŚŁĘÓ";
            var y="zzcnasleoZXCNASLEO";
            for(var i=0;i<x.length;i++)
              s=s.replace(x.charAt(i), y.charAt(i));
            return s;
        },

        Swap : function(i, mapArray){
            return mapArray[i];
        },
        Form_Action : function(e)
        {
            var options = (Agroma.service_on && Agroma.dealer_on) ?  "" : (Agroma.dealer_on ? "dealerzy" : (Agroma.service_on ? "serwisy" : ""));
            var selected_region = $("#regionselect div").text();
            var location = "/regiony/pokaz/"+(selected_region=="Województwo" ? "pl" : Agroma.Unpolish(selected_region))+"/"+options;
            document.location = location;
            e.preventDefault();
        }
};

function hide_handler(e) {
	$("#regionselect").trigger('click');
}
	
function combo_handler(e) {
		if($("#regions").css("display")=="none")
		{
			$("#regions").show(200);
			$(document).bind('click', hide_handler);	
		} 
		else
		{
			$("#regions").hide(100);
			$(document).unbind('click', hide_handler);
		}
		e.stopPropagation();
}

function update_combo(e) {
	$("#regionselect div").html($(e.target).text());
}

function mail(s) {
    if(!s)
        return false;

    document.location.href = 'mailto:' + s + '@agroma.pl';
}

$(document).ready(function(){
	$("#dealer").bind("click", function() {
		Agroma.dealer_on?$("#dealer span").css("background", "none"):$("#dealer span").css("background", "url('/images/checbox_marker.gif') left 4px no-repeat");
		Agroma.dealer_on=!Agroma.dealer_on;
	});
	$("#service").bind("click", function(){
		Agroma.service_on?$("#service span").css("background", "none"):$("#service span").css("background", "url('/images/checbox_marker.gif') left 4px no-repeat");
		Agroma.service_on=!Agroma.service_on;
	});
	
	Agroma.Region_Map();
	Agroma.Bind_Search();
	
	$("#search_btn").bind('click', Agroma.Form_Action);
	$("#regionselect").bind('click', combo_handler);
	$("#regions li").bind('click', update_combo);
        $('a[@rel*=lightbox]').lightBox();
        $('a[@rel*=blank]').each(
        function(){$(this).attr("target", "_blank")});
    $("#westwood_menu .wdrop").bind('mouseover', function(e) {
        $("#westwood_submenu").css("display", "block");
        $("#westwood_menu .wdrop").css("background", "url('/images/westwood/gradient_both.gif') 0 -30px repeat-x");
        var event = e.target;
        $("#westwood_submenu").css("left", $("#westwood_menu .wdrop").position().left + ($.browser.msie?10:0));
    });

    $("#westwood_menu .wdrop").bind('mouseout', function() {
        $("#westwood_submenu").css("display", "none");
        $("#westwood_menu .wdrop").css("background", "url('/images/westwood/gradient_both.gif') 0 0 repeat-x");
    });


    $("#westwood_submenu").bind('mouseover', function() {
        $("#westwood_submenu").css("display", "block");
        $("#westwood_menu .wdrop").css("background", "url('/images/westwood/gradient_both.gif') 0 -30px repeat-x");
    });

    $("#westwood_submenu").bind('mouseout', function() {
        $("#westwood_submenu").css("display", "none");
        $("#westwood_menu .wdrop").css("background", "url('/images/westwood/gradient_both.gif') 0 0 repeat-x");
    });

    $("#kos_menu .wdrop").bind('mouseover', function(e) {
        $("#kos_submenu").css("display", "block");
        $("#kos_menu .wdrop").css("background", "url('/images/westwood/gradient_both.gif') 0 -30px repeat-x");
        var event = e.target;
        $("#kos_submenu").css("left", $("#kos_menu .wdrop").position().left + ($.browser.msie?10:0));
    });

    $("#kos_menu .wdrop").bind('mouseout', function() {
        $("#kos_submenu").css("display", "none");
        $("#kos_menu .wdrop").css("background", "url('/images/westwood/gradient_both.gif') 0 0 repeat-x");
    });


    $("#kos_submenu").bind('mouseover', function() {
        $("#kos_submenu").css("display", "block");
        $("#kos_menu .wdrop").css("background", "url('/images/westwood/gradient_both.gif') 0 -30px repeat-x");
    });

    $("#kos_submenu").bind('mouseout', function() {
        $("#kos_submenu").css("display", "none");
        $("#kos_menu .wdrop").css("background", "url('/images/westwood/gradient_both.gif') 0 0 repeat-x");
    });

    $("#submenu2").bind('mouseover', function(e) {
        $("#agroma_submenu2").css("display", "block");
        $("#submenu2").css("background", "url('/images/westwood/gradient_both.gif') 0 -30px repeat-x");
        var event = e.target;
        $("#agroma_submenu2").css("left", $("#submenu2").position().left + ($.browser.msie?10:0));
    });

    $("#submenu2").bind('mouseout', function() {
        $("#agroma_submenu2").css("display", "none");
        $("#submenu2").css("background", "url('/images/westwood/gradient_both.gif') 0 0 repeat-x");
    });


    $("#agroma_submenu2").bind('mouseover', function() {
        $("#agroma_submenu2").css("display", "block");
        $("#submenu2").css("background", "url('/images/westwood/gradient_both.gif') 0 -30px repeat-x");
    });

    $("#agroma_submenu2").bind('mouseout', function() {
        $("#agroma_submenu2").css("display", "none");
        $("#submenu2").css("background", "url('/images/westwood/gradient_both.gif') 0 0 repeat-x");
    });

    $("#submenu3").bind('mouseover', function(e) {
        $("#agroma_submenu3").css("display", "block");
        $("#submenu3").css("background", "url('/images/westwood/gradient_both.gif') 0 -30px repeat-x");
        var event = e.target;
        $("#agroma_submenu3").css("left", $("#submenu3").position().left + ($.browser.msie?10:0));
    });

    $("#submenu3").bind('mouseout', function() {
        $("#agroma_submenu3").css("display", "none");
        $("#submenu3").css("background", "url('/images/westwood/gradient_both.gif') 0 0 repeat-x");
    });


    $("#agroma_submenu3").bind('mouseover', function() {
        $("#agroma_submenu3").css("display", "block");
        $("#submenu3").css("background", "url('/images/westwood/gradient_both.gif') 0 -30px repeat-x");
    });

    $("#agroma_submenu3").bind('mouseout', function() {
        $("#agroma_submenu3").css("display", "none");
        $("#submenu3").css("background", "url('/images/westwood/gradient_both.gif') 0 0 repeat-x");
    });

    $('a[rel^=email]').each(function(){
        var rel = $(this).attr("rel");
        var email = rel.substring(6, rel.length-1)+"@agroma.pl";
        $(this).attr("href", "mailto:"+email);
        if($(this).text()=="")
            $(this).text(email);
    });

});