  /*Global*/
var map;
var openBaner = false;
var menu_autoclose = true

var latitude = 49.7832273;
var longitude = 22.76947893;
var mapaNazwa = 'Zen-on Sp. z o.o.';
var latitude2 = 50.028135;
var longitude2 = 22.018229;


var aPkt = new Array();
var aPktIdent = new Array();


aPktIdent = new Array(0,1);

var gmarkers = new Array();
var map;
var isMapMade = false;
var pwybor = false;

function o(id) {
    return document.getElementById(id);
}

// maps
function array_search(arr,val){
    for (var i=0; i<arr.length; i++)
        if (arr[i] == val)
            return i;
    return false;
}



function showGmap(id) {
    if(!isMapMade) {

        $("#dialogGmap").dialog({
            modal: true,
            width:520,
            height:390,
            bgiframe: true,
            resize: function(event, ui) {
                if (map) map.checkResize();
            }
        });
        isMapMade = true;
        $("#dialogGmap").dialog('open');
        makeMap(id);

    } else {

        $("#dialogGmap").dialog('open');
        var center = new GLatLng(aPkt[id][3], aPkt[id][4]);
        map.setCenter(center, 15);
        myclick(id);
    }
}

function createMarker(point, tytul, number) {
    var marker = new GMarker(point);
    var i = gmarkers.length;
    gmarkers.push(marker);

    var iwAnchor = marker.getIcon().infoWindowAnchor;
    var iconAnchor = marker.getIcon().iconAnchor;
    var offset = new GSize(iwAnchor.x-iconAnchor.x,iwAnchor.y-iconAnchor.y);
    var dymek_html = '<b style="color: #000000;">'+tytul+'</b><br/><span style="color: #000000;">';
    if (number == 1) {
      dymek_html += addr1;
    } else if (number == 2) {
      dymek_html += addr2;
    } else {
      dymek_html += addr3;
    }
    dymek_html += '</span>';
    map.openInfoWindowHtml(marker.getLatLng(), dymek_html, {
        pixelOffset:offset
    });


    return marker;
}

function myclick(clickid) {
    GEvent.trigger(gmarkers[array_search(aPktIdent, clickid)],"click");
}

function makeMap(latitude, longitude, title, div, number) {

    var point = new GLatLng(latitude,longitude);

    map = new GMap2(document.getElementById(div));
    var marker = createMarker(point, title, number);

    var center = new GLatLng(latitude,longitude);
    map.setCenter(center, 16);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

    var mm = new GMarkerManager(map);

    mm.addMarkers(gmarkers,0,16);
    mm.refresh();



}



(window.attachEvent)?attachEvent('onload', init) : addEventListener('load', init, false);

function init() {
    /*try {
      $(window).resize(lokalizajca);
    } catch (e){};*/
    try { 
        Shadowbox.init({
            counterType:"skip"
        });
    } catch (e){};
}

function validateSearchForm(seo) {
    var iTypOferty = o('typ_oferty').value;
    var iObiekt = o('rodzaj').value;
    var iPowierzchnia = o('powierzchnia').value;
    var sMiasto = o('miasta').value;
    var iPage = 1;
    if (iTypOferty == '' && iObiekt == '' && iPowierzchnia == '' && sMiasto == '') {
        return;
    } else {
        xajax_searchForm(iTypOferty, iObiekt, iPowierzchnia, sMiasto, iPage, seo);
    }
}

function searchPager(){

}

function stylowanie() {
    $('.sSel').sSelect();
    $('input:radio').checkbox();
}

function menu(autoclose) {
    //init
    var speed = 300;
    $('#main_menu > div').show();
    $('#main_menu > div').find('div').slideUp(1);
    setTimeout(function(){
        $('#main_menu > div.opened').find('div').slideDown(speed);
    },speed*2);
    //actions
    $('#main_menu > div > a').mouseup(function() {
        var t = $(this).parent().get(0)
        if (!($(t).find('div').html() == null)) {
            if($(t).hasClass('opened')) {
                $(t).find('div').slideUp(speed,function() {
                    setTimeout(function(){
                        $(t).removeClass('opened');
                    },speed);

                });
            } else {
                if(autoclose) {
                    $('#main_menu > div').not(t).find('div').slideUp(speed,function() {
                        setTimeout(function(){
                            $('#main_menu > div').not(t).removeClass('opened');
                        },speed);
                    });
                }
                $(t).addClass('opened');
                $(t).find('div').slideDown(speed)
            }
        }
    })
}


function initBaner() {
    $("#baner").dialog({
        bgiframe: true,
        autoOpen: true,
        width: 620,
        modal: true,
        resizable: false,
        buttons: {
            OK: function() {
                $(this).dialog('close');
            }
        }
    });
}

function initMapa() {

    makeMap(latitude,longitude, 'Zen-on Sp. z o.o.', 'gmap', 1);
	makeMap(latitude2,longitude2, 'Zen-on Sp. z o.o.', 'gmap2', 2);
	makeMap(latitude3,longitude3, 'Zen-on Sp. z o.o.', 'gmap3', 3);

    $("#mapa").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 810,
        height: 530,
        modal: true,
        resizable: false
    });

	$("#mapa2").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 810,
        height: 530,
        modal: true,
        resizable: false
    });
    
    $("#mapa3").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 810,
        height: 530,
        modal: true,
        resizable: false
    });
	
    $(".mapa_small").click(function(){
        $('#mapa').dialog('open');
    });
	
	$(".mapa_small2").click(function(){
        $('#mapa2').dialog('open');
    });
}

function sendKontakt () {
    var sImie = o('imie').value;
    var sEmail = o('email').value;
    var sTemat = o('temat').value;
    var sWiadomosc = o('tresc').value;

    var bErr = false;

    if (sImie == '' || sImie == 'Musisz wpisać imię') {
        bErr = true;
        o('imie').style.fontWeight = "bold";
        o('imie').style.color = "#da0e00";
        o('imie').value = "Musisz wpisać imię";
    } else {
        o('imie').style.fontWeight = "normal";
        o('imie').style.color = "#383838";
    }

    if (sEmail == '' || sEmail == 'Musisz wpisać adres e-mail') {
        bErr = true;
        o('email').value = "Musisz wpisać adres e-mail";
        o('email').style.fontWeight = "bold";
        o('email').style.color = "#da0e00";
    } else {
        if (sEmail != '' && !validateEmail(sEmail)) {
            bErr = true;
            o('email').value = "Niepoprawny adres e-mail";
            o('email').style.fontWeight = "bold";
            o('email').style.color = "#da0e00";
        } else {
            o('email').style.fontWeight = "normal";
            o('email').style.color = "#383838";
        }
    }



    if (sWiadomosc == '' || sWiadomosc == 'Musisz wpisać treść wiadomości') {
        bErr = true;
        o('tresc').value = "Musisz wpisać treść wiadomości";
        o('tresc').style.fontWeight = "bold";
        o('tresc').style.color = "#da0e00";
    } else {
        o('tresc').style.fontWeight = "normal";
        o('tresc').style.color = "#383838";
    }

    if (!bErr)
        xajax_sendEmail(sImie, sEmail, sTemat, sWiadomosc);
}

function validateEmail(email){
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(email.match(emailRegEx)){
        return true;
    }else{
        return false;
    }
}

function dodajDoNewslettera() {
    var sEmail = o('email').value;
    var iTyp = o('newsletter').value;

    var bErr = false;

    if (sEmail == '') {
        bErr = true;
        o('newsletterErr').innerHTML = "Musisz wpisać adres e-mail";
        o('newsletterErr').style.display = "block";
    } else {
        if (!validateEmail(sEmail)) {
            bErr = true;
            o('newsletterErr').innerHTML = "Nieprawidłowy adres e-mail";
            o('newsletterErr').style.display = "block";
        } else {
            bErr = false;
            o('newsletterErr').innerHTML = "";
            o('newsletterErr').style.display = "none";
        }
    }

    if (!bErr) {
        xajax_dodajDoNewslettera(sEmail, iTyp);
    }
}

function changeFieldValue(sId, uValue) {
    o(sId).value = uValue;
}

function galeria() {
    var element = ".oferta_zdjecie > a > img";
    var przedrostek = "thumb_";

    var podglad = "#img_big";
    var przedrostek_podgladu = "flash_";

    var kolor_zaznaczenia = "#7092B7";
    var szybkosc_fade = "fast";


    $(element).mouseup(function() {
        var src = $(this).attr("src");
        var t = this;
        var Bsrc = src.replace(przedrostek,przedrostek_podgladu);
        if(Bsrc != $(podglad).attr("src")) {
            $(element).css("background","transparent");
            $(this).css("background",kolor_zaznaczenia);
            $(podglad).fadeOut(szybkosc_fade, function(){
                $(podglad).attr("src",Bsrc);
                $(podglad).load(function() {
                    $(podglad).fadeIn(szybkosc_fade);
                });
            });
        }
    });
}

function loadNaglowek(t, s) {
    var flashvars = {
        title: t,
        subtitle: s
    };
    var params = {
        wmode: "transparent",
        menu: "false",
        allowscriptaccess: "always",
        quality: "high",
        salign: "tl"
    };
    var attributes = {};
    swfobject.embedSWF("/images/naglowek.swf", "naglowek", "600", "50", "8.0.0", "/images/expressinstall.swf", flashvars, params, attributes);

}
/*
function produktFlashOFF(){
 
    $('#produkt_flash').css({
        'display' : 'none'
    });
    global_idProd = null;
    przyciemnienieOFF();
}

function przyciemnienie() {
    var wymiary = getPageSize();
    
    $('#przyciemnienie').css({
        'height' : wymiary[1] + "px"
    });
     
    $('#przyciemnienie').css({
        'display' : 'block'
    });
    

}

function przyciemnienieOFF(){
    $('#przyciemnienie').css({
        'display' : 'none'
    });
    global_idInsp = null;
}

function setSelected(n){
    global_selected = n;
}
function lokalizajca(){
    var wymiary = getPageSize();

    try{
        $('#produkt_flash').attr('width',wymiary[2]);
        $('#produkt_flash').attr('height',wymiary[3]);
        //$('#produkt_flash').width(wymiary[2]);
       // $('#produkt_flash').height(wymiary[3]);
    } catch(e) {}

    //if(global_idInsp != null)inspiracjeFlash(global_selected);
    //if(global_idProd != null) produktFlash(null , global_selected);

}
var global_idInsp = null;
var global_selected = null;


//kolektor linków
var xmlString = '';
function collectLinks(){
    if(xmlString != '') xmlString = '';
    xmlString += '<images>';
    $('img.FlashBox').each(function(){
        var params = $(this).attr('alt').split('|');
        //if(params.length != 3) alert('podaj parametry atrybutu ALT w konwencji: "sciezka|tytul|opis", lub np: "sciezka||"')
        xmlString += '<image>';
        xmlString += '<source>';
        xmlString += params[0];
        xmlString += '</source>';
        xmlString += '<title>';
        xmlString += params[1];
        xmlString += '</title>';
        xmlString += '<description>';
        xmlString += params[2];
        xmlString += '</description>';
        xmlString += '</image>';
    });
    xmlString += '</images>';
}
//kolektor linków*/
function inspiracjeFlash()
{

    
    //collectLinks();
    
     var idInsp;
 
    if(arguments[0] != undefined) {
        global_idInsp = arguments[0];
        idInsp = arguments[0];
   }
    else idInsp = global_idInsp;
    global_selected = idInsp;

    FlashBox.init('',arguments[0],'insp');

    /*
    $('#container_flash').css({'display' : 'block'});
    przyciemnienie();
    var wymiary = getPageSize();
    var params = {
        wmode: "transparent",
        menu: "false",
        allowscriptaccess: "always",
        quality: "high",
        salign: "tl"
    };
    var flashvars = {

        xml_src: xmlString,
        //xml_url: '/seo/xmlflash/inspiracje/',
        selected: idInsp
    };
    var attributes = '';
    swfobject.embedSWF("/images/FlashBox.swf", "produkt_flash", wymiary[2], wymiary[3], "9.0.0", "/images/expressinstall.swf", flashvars, params, attributes);
*/
}


function produktFlash()
{
    var idProd;
    if(arguments[0] != undefined) {
        global_idProd = arguments[0];
        idProd = arguments[0];
    }
    else idProd = global_idProd;


    FlashBox.show('/seo/xmlflash/galeria/'+idProd);

    /*
    przyciemnienie();
    var wymiary = getPageSize();
    
    var params = {
        wmode: "transparent",
        menu: "false",
        allowscriptaccess: "always",
        quality: "high",
        salign: "tl"
    };
    var flashvars = {
    
        xml_url: '/seo/xmlflash/galeria/'+idProd,
        selected: arguments[1]
        };
  

    var attributes = '';
  
    swfobject.embedSWF("/images/FlashBox.swf", "produkt_flash", wymiary[2], wymiary[3], "9.0.0", "/images/expressinstall.swf", flashvars, params, attributes);
*/
}
var global_idProd = null;
function loadFlash(FirmaUrl, OfertaUrl, InspiracjeUrl, KontaktUrl, Xml, n, select) {
    var flashvars = {
        url_1: FirmaUrl,
        url_2: OfertaUrl,
        url_3: InspiracjeUrl,
        url_4: KontaktUrl,
        xml_url: Xml,
        selected: select,
        tryb: n
    };
    var params = {
        wmode: "transparent",
        menu: "false",
        allowscriptaccess: "always",
        quality: "high",
        salign: "tl"
    };
    var attributes = {};
    if (n == 0){

        swfobject.embedSWF("/images/main.swf", "flash_main", "1000", "480", "8.0.0", "/images/expressinstall.swf", flashvars, params, attributes);
    }
    if (n == 1){
        swfobject.embedSWF("/images/main.swf", "flash_main", "1000", "260", "8.0.0", "/images/expressinstall.swf", flashvars, params, attributes);
    }
 

}
function loadNaglowek2(t, n, u) {
    var flashvars = {
        title: t,
        url: u
    };
    var params = {
        wmode: "transparent",
        menu: "false",
        allowscriptaccess: "always",
        quality: "high",
        salign: "tl"
    };
    var attributes = {};
    swfobject.embedSWF("/images/naglowek2.swf", "naglowek"+n, "200", "25", "8.0.0", "/images/expressinstall.swf", flashvars, params, attributes);

}

function cleanField(sId) {
    switch (sId) {
        case 'imie':
            if(o('imie').value == "Musisz wpisać imię")
                o('imie').value = '';
            break;

        case 'email':
            if (o('email').value == "Musisz wpisać adres e-mail" || o('email').value == "Niepoprawny adres e-mail")
                o('email').value = '';
            break;

        case 'tresc':
            if (o('tresc').value == "Musisz wpisać treść wiadomości")
                o('tresc').value = '';
            break;
    }
}

function pokazMape() {
    $('#mapa').dialog('open');
}

function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }      // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
}

function pokazMape2() {
    $('#mapa2').dialog('open');
}

function pokazMape3() {
    $('#mapa3').dialog('open');
}
