function o(id){return document.getElementById(id);}

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){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};


function przyciemnienie() {
	var wymiary = getPageSize();
	o('przyciemnienie').style.height =  wymiary[1] + "px";
	//$('#przyciemnienie').fadeIn();
	o('przyciemnienie').style.display = "block";
}

function closeWindow(div) {
	okienko_wybrane = "none";
	o("przyciemnienie").style.display = "none";
	o(div).style.display = "none";
}


var okienko_wybrane = "none";
function showWindow(div) {
	okienko_wybrane = div;
	pozycja(div);
	o(div).style.display = "block";
	przyciemnienie();
}

function pozycja(id) {
var div = o(id);
var wymiary = getPageSize();
  div.style.marginLeft = parseInt((wymiary[2] / 2) - ($('#'+id).width()/2)) + "px";
  div.style.marginTop =  parseInt(wymiary[3] / 2) - ($('#'+id).height()/2) + "px";
}

function redirect(url){
    window.location.href = url;
}


function showSubmenu(iId) {
    o('submenu'+iId).style.marginTop = "61px";
    o('menu_item'+iId).className = "selected";
    o('submenu'+iId).style.display = "block";
    
    //if (o('submenu'+iId).style.display != "block")
        //$("#submenu"+iId).slideDown("slow");
    var uMargin;
    var iWidth = o('menu_item'+iId).offsetWidth;
    var iSubmenuWidth = o('submenu'+iId).offsetWidth;
    //var iItemMargin = o('menu_item'+iId).offsetLeft;

    //var obj = o('menu_item'+iId).getBoundingClientRect();
    //var iItemMargin = obj.left;
    var iItemMargin = $('#menu_item'+iId).offset().left;

    //var obj2 = o('wrapper').getBoundingClientRect();

//alert($('#wrapper').offset().left + '-' + obj2.left);
    var obj2Left = $('#wrapper').offset().left;

    uMargin = iItemMargin - (iSubmenuWidth-iWidth)/2;

    
    if ((uMargin + iSubmenuWidth) >= (obj2Left + o('wrapper').offsetWidth)) {
        o('submenu'+iId).style.marginLeft = (obj2Left + o('wrapper').offsetWidth) - iSubmenuWidth + "px";
    } else {
        o('submenu'+iId).style.marginLeft = uMargin + "px";
    }
    
    //console.log(iItemMargin + '-' + iSubmenuWidth + '-' + iWidth);
    
    o('submenu'+iId).style.position = "absolute";
}

function hideSubmenu(iId, iSelected) {
    o('submenu'+iId).style.display = "none";
    //$("#submenu"+iId).slideUp("slow");
    if (iSelected == 0) {
        o('menu_item'+iId).className = "";
    }
}

var sPoprzednieRozwiniete;

function rozwinMenu(sId) {
    if (o('menu_product_item'+sId).style.display == "none") {
        $("#menu_product_item"+sId).slideDown("slow");
        o('menu_product_link'+sId).className = "selected";
    } else {
        o('menu_product_link'+sId).className = "";
        $("#menu_product_item"+sId).slideUp("slow");
    }
    

    if (sPoprzednieRozwiniete != sId) {
        try {o('menu_product_link'+sPoprzednieRozwiniete).className = "";
        $("#menu_product_item"+sPoprzednieRozwiniete).slideUp("slow");} catch (e) {};
        sPoprzednieRozwiniete = sId;
    }
    
}

function Flash(){
    var params = {
        wmode: "transparent",
        menu: "false",
        allowscriptaccess: "always",
        quality: "high",
        salign: "tl"
    };
    var flashvars = {
        xml_url: 'client/xml.xml',
        delay: '4000'
    };


    var attributes = '';

    swfobject.embedSWF("client/main.swf", "main_flash", "950", "457", "8.0.0", "client/expressinstall.swf", flashvars, params, attributes);

}

function check_imie(n)
{
	var imie = o('imie');


	if(n==0)
	{
		if(imie.value=='Your name')
			imie.value ='';

        }

        if(n==1)
        {
            if(imie.value=='')
            imie.value ='Your name';
        }
}

function sendKontakt () {
    var sImie = o('imie').value;
    var sEmail = o('email').value;
    var sWiadomosc = o('tresc').value;
    var iSendToMe = o('checkbox').checked;

    var bErr = false;

    if (sImie == '' || sImie == 'Your name' || sImie == 'Write your name') {
        bErr = true;
        o('imie').style.fontWeight = "bold";
        o('imie').style.color = "#da0e00";
        o('imie').value = "Write your name";
    } else {
        o('imie').style.fontWeight = "normal";
        o('imie').style.color = "#fff";
    }

    if (sEmail == '' || sEmail == 'Your e-mail' || sEmail == 'Enter Your e-mail') {
        bErr = true;
        o('email').value = "Write your e-mail";
        o('email').style.fontWeight = "bold";
        o('email').style.color = "#da0e00";
    } else {
        if (sEmail != '' && !validateEmail(sEmail)) {
            bErr = true;
            o('email').value = "Incorrect e-mail";
            o('email').style.fontWeight = "bold";
            o('email').style.color = "#da0e00";
        } else {
            o('email').style.fontWeight = "normal";
            o('email').style.color = "#fff";
        }
    }



    if (sWiadomosc == '' || sWiadomosc == 'Write your message' || sWiadomosc == 'Your message') {
        bErr = true;
        o('tresc').value = "Write your message";
        o('tresc').style.fontWeight = "bold";
        o('tresc').style.color = "#da0e00";
    } else {
        o('tresc').style.fontWeight = "normal";
        o('tresc').style.color = "#fff";
    }

    if (!bErr)
        xajax_sendEmail(sImie, sEmail, sWiadomosc, iSendToMe ? '1' : '0');
}


function check_email(n)
{
	var email = o('email');

	if(n==0)
	{
		if(email.value=='Your e-mail')
			email.value ='';
	}
	if(n==1)
	{
		if(email.value=='')
			email.value ='Your e-mail';
	}
}

function check_tresc(n)
{
	var imie = o('tresc');


	if(n==0)
	{
		if(imie.value=='Your message')
			imie.value ='';

        }

        if(n==1)
        {
            if(imie.value=='')
            imie.value ='Your message';
        }
}

function cleanField(sId) {
    switch (sId) {
        case 'imie':
            if(o('imie').value == "Write your name")
                o('imie').value = '';
                o('imie').style.color = "#fff";
                o('imie').style.fontWeight = "normal";
            break;

        case 'email':
            if (o('email').value == "Write your e-mail" || o('email').value == "Incorrect e-mail")
                o('email').value = '';
                o('email').style.color = "#fff";
                o('email').style.fontWeight = "normal";
            break;

        case 'tresc':
            if (o('tresc').value == "Write your message" || o('tresc').value == "Your message" )
                o('tresc').value = '';
                o('tresc').style.color = "#fff";
                o('tresc').style.fontWeight = "normal";
            break;
    }
}


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 stylowanie() {
  $('#checkbox').checkbox();
 
}

function checkImage() {
    var iWidth = o('img_im').width;
    if (iWidth > 250) {
        $('.image').css('position', 'absolute');
//        var iDiff = iWidth - 250;
//        var iHeight = o('img_im').height;
        
//        o('img_im').style.width = "250px";
//        var iHeight2 = o('img_im').height;
//        var iDiff2 = iHeight - iHeight2;
//        o('img_im').style.marginLeft = -(iDiff/2) + "px";
//        o('img_im').style.marginTop = -(iDiff2/2) + "px";
    }
}