var intMenu;

function initAjax(){
    new Ajax.Request('index.php?class=viewSiteInit&type=view', {
        method: 'get',
        parameters: {},
        evalScripts: true,
        onComplete: function(transport){
            response = transport.responseText;
            var myJSONObject = eval('(' + transport.responseText + ')');
            $('b1').innerHTML = myJSONObject.menu;
            $('b2').innerHTML = myJSONObject.content;
            
            if ($("cowparade_swf")) {
                var so = new SWFObject("site/graphics/cowparade.swf", "zapisz", "130px", "110px", "8", "#fff");
                so.addParam("wmode", "transparent");
                so.write("cowparade_swf");
            }
        }
    });
}

function ajaxControl(ajaxReq, ajaxContent, intMenuId, isSub){
	
    if (ajaxReq.search(/start/) == 0) {
        var arr = ajaxReq.split(',');
        var lang = arr[1];
        lang = lang.replace(/\.html/gi, "");
    }
    else {
        var lang = '';
    }
    
    if (intMenuId) {
        intMenu = intMenuId;
        var arrElements = $$('#b1 .first_li');
        arrElements.each(function(item){
            if (item.id != 'li_' + intMenuId) 
                $(item).style.background = 'url( site/graphics/bgLi.gif ) 0 3px no-repeat';
        });
        var arrElements = $$('#b1 .subUl_first');
        arrElements.each(function(item){
            if (item.id != 'li_' + intMenuId) 
                $(item).style.background = 'url( site/graphics/bgLi01.gif ) 0 9px no-repeat';
        });
        
        /*if ( isSub == true ){
         $('li_'+intMenu).style.background='url( site/graphics/bgLi01.gif ) 0 9px no-repeat';
         }else{
         $('li_'+intMenu).style.background='url( site/graphics/bgLi.gif ) 0 3px no-repeat';
         }*/
    }
    new Ajax.Request(ajaxReq, {
        method: 'get',
        evalScripts: true,
        parameters: {
            lang: lang
        },
        onComplete: function(transport){
            response = transport.responseText;
            $(ajaxContent).innerHTML = response;
            Lightview.updateViews();
            
            if ($("cowparade_swf")) {
                var so = new SWFObject("site/graphics/cowparade.swf", "zapisz", "130px", "110px", "8", "#fff");
                so.addParam("wmode", "transparent");
                so.write("cowparade_swf");
            }
        }
    });
    //przeladowanie menu jesli wybrano wersje jezykowa
    if (ajaxReq.search(/start/) == 0) {
        new Ajax.Request('index.php?class=viewSiteMenu&type=view', {
            method: 'get',
            evalScripts: true,
            parameters: {
                lang: lang
            },
            onComplete: function(transport){
                response = transport.responseText;
                $('b1').innerHTML = response;
            }
        });
    }
}

function sendForm(ajaxReq, ajaxContent){

    KODre = /^\d\d-\d\d\d$/;
    EMAILre = /^([a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9](\.[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9\,])+\s*)+$/;
    TELEFONre = /^\+\d+\.\d+$/;
    
    var blnSend = 'true';
    var blnSend1 = validate($('strName'));
    var blnSend2 = validate($('strTelefon'));
    var blnSend3 = validate($('strContent'));
    
    if (blnSend1 == 'false' || blnSend2 == 'false' || blnSend3 == 'false') {
        blnSend = 'false';
    }
    
    if (EMAILre.test($F('strEmail')) != true) {
        $($('strEmail')).setStyle({
            'border': '1px solid red',
            'background': '#ffe9ba'
        });
        blnSend = 'false';
    }
    else {
        $($('strEmail')).setStyle({
            'border': '1px solid black',
            'background': '#fff'
        });
    }
    new Ajax.Request(ajaxReq, {
        method: 'post',
        evalScripts: true,
        parameters: {
            blnSave: $F('blnSave'),
            strName: $F('strName'),
            strTelefon: $F('strTelefon'),
            strEmail: $F('strEmail'),
            strContent: $F('strContent')
        },
        onComplete: function(transport){
            response = transport.responseText;
            if (blnSend == 'true') {
                alert('Dziękujemy Twoja wiadomość została prawidłowo wysłana.')
                $(ajaxContent).innerHTML = response;
            }
            //blnSend == 'false' ? $('errorMsg').innerHTML = response : 
        }
    });
}


function validate(obj){

    if (trim(obj.value) == '') {
        $(obj).setStyle({
            'border': '1px solid red',
            'background': '#ffe9ba'
        });
        return false;
    }
    else {
        $(obj).setStyle({
            'border': '1px solid black',
            'background': '#fff'
        });
        return true;
    }
}

function trim(str){
    while (str.substring(0, 1) == ' ') {
        str = str.substring(1, str.length);
    }
    while (str.substring(str.length - 1, str.length) == ' ') {
        str = str.substring(0, str.length - 1);
    }
    return str;
}

function changeLi(id, state){
    if (state == true) {
        if (id == 'li_1') {
            $(id).style.background = 'url( site/graphics/bgLi1.gif ) 0 10px no-repeat';
        }
        else {
            $(id).style.background = 'url( site/graphics/bgLi1.gif ) 0 3px no-repeat';
        }
    }
    else {
        if (id == 'li_1') {
            $(id).style.background = 'url( site/graphics/bgLi.gif ) 0 10px no-repeat';
        }
        else {
            $(id).style.background = 'url( site/graphics/bgLi.gif ) 0 3px no-repeat';
        }
    }
}

function changeSubLi(id, state){
    if (state == true) {
        $(id).style.background = 'url( site/graphics/bgLi01a.gif ) 0 7px no-repeat';
    }
    else {
        $(id).style.background = 'url( site/graphics/bgLi01.gif ) 0 9px no-repeat';
    }
}

function isset(varname){
    return (typeof(window[varname]) != 'undefined');
}

function changeImg(path01, path02, path03, mainImg, obj){
    var src = $('maingImg').src;
    $('maingImg').src = path01 + obj.id;
    $('mainImgA').href = 'javascript:Lightview.show({href:"' + path02 + obj.id + '"})';
    var a = src.split("/");
    $(obj).src = path03 + a[a.length - 1];
    obj.id = a[a.length - 1];
    Lightview.updateViews();
}


function archive(link){
    Lightview.show({
        href: link,
        rel: 'ajax',
        options: {
			height:600,
			width:1000
        }
    });
}
