var lPrefix = getCookie('lang');
var lClickToEdit = (lPrefix == 'pl') ? 'Kliknij aby edytować' : 'Click to edit';
var lRotatingPhoto = (lPrefix == 'pl') ? 'Obracam zdjęcie...' : 'Rotating photo...';
var lAddingToGroup = (lPrefix == 'pl') ? 'Dodaję do grupy...' : 'Adding to group...';
var lAddingToAlbum = (lPrefix == 'pl') ? 'Dodaję do albumu...' : 'Adding to album...';
var lRemovingFromGroup = (lPrefix == 'pl') ? 'Usuwam z grupy...' : 'Removing from group...';
var lRemovingFromAlbum = (lPrefix == 'pl') ? 'Usuwam z albumu...' : 'Removing from album...';
var lAddingPrints = (lPrefix == 'pl') ? 'Dodaję odbitki do koszyka...' :  'Adding prints to cart...';
var lAddingToNewAlbum = (lPrefix == 'pl') ? 'Dodaję do nowego albumu...' : 'Adding to new album...';

var sHref;
var isIE = (navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("win") != -1);
var isIE7 = (navigator.userAgent.toLowerCase().indexOf("msie 7.0") != -1) && (navigator.userAgent.toLowerCase().indexOf("win") != -1);

var iconExt = (isIE && !isIE7) ? 'gif' : 'png';

var hlTimer = new Array();
var hTime = 400;
var doneState = false;

function isIE()
{
    var useragent = navigator.userAgent;
    var pos = useragent.indexOf('MSIE');
    if (pos > -1) {
        return true;
    } else {
        return false;
    }
}

function htmlspecialchars(string)
{
    var str = "";
    for (i = 0; i < string.length; i++)
    {
        if (string.charAt(i) == "<") str += "&lt;";
        else if (string.charAt(i) == ">") str += "&gt;";
        else if (string.charAt(i) == "&") str += "&amp;";
        else if (string.charAt(i) == '"') str += "&quot;";
        else str += string.charAt(i);
    }
    return str;
}

function copyDiv(div1, div2)
{
    _ge(div2).innerHTML = _ge(div1).innerHTML;
}

var _pi = function(val)
{
    return parseInt(val);
};

var _ge = function(id)
{
    return document.getElementById(id);
};

function cLink(msg, link)
{
    if (confirm(msg)) {
        location.href = link;
    }
}

function newImage(arg) {
    if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
    }
}

function changeDiv(the_div)
{
    var the_style = getStyleObject(the_div);
    if (the_style != false) {
        if (the_style.display == 'block') {
            the_style.display = 'none';
        } else {
            the_style.display = 'block';
        }
    }
}

function divVis(div, type)
{
    var the_style = getStyleObject(div);
    if (type == 'off') {
        the_style.display = 'none';
    } else {
        the_style.display = 'block';
    }
}

function switchDiv(first, second)
{
    var style1 = getStyleObject(first);
    var style2 = getStyleObject(second);
    style1.display = 'none';
    style2.display = 'block';
}

function getStyleObject(objectId)
{
    if ((document.getElementById) && (_ge(objectId))) {
        return _ge(objectId).style;
    } else {
        if ((document.all) && (document.all(objectId))) {
            return document.all(objectId).style;
        } else {
            return false;
        }
    }
}

function hidestatus()
{
    window.status = ''
    return true
}

if (document.layers) {
    document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT | Event.CLICK)
}

document.onmouseover = hidestatus
document.onmouseout  = hidestatus
document.onclick     = hidestatus

function jump(targ,selObj,restore)
{
    eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
    if (restore) {
        selObj.selectedIndex=0;
    }
}

function qif_submit(msg1, msg2)
{
    var f = document.quickinviteform;
    var nemails = 0;
    var ivemails = 0;
    for (var i = 3; i < 6; i++) {
        if (f[i].value != '') {
            nemails++;
            if (!isValidEmail(f[i].value)) {
                ivemails++
            } else {
                f.emails.value+=';'+f[i].value
            }
        }
    }

    if (nemails < 1) {
        alert(msg1);
        return false;
    } else if (ivemails>0) {
        alert(msg2);
        return false;
    }
}

function isValidEmail(e)
{
    var pat=/^[a-z0-9\-\.%_&+]+@(([a-z0-9]|[a-z0-9]+[a-z0-9-]*[a-z0-9]+)\.)+[a-z0-9][a-z0-9]+$/i;
    return(pat.test(e));
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

var hL = function()
{
    if (hlTimer && hlTimer[this.id]) {
        clearTimeout(hlTimer[this.id]);
    }
    this.title = lClickToEdit;
    this.style.backgroundColor = '#def4ff';
}


var hLD = function()
{
    if (hlTimer && hlTimer[this.id]) {
        clearTimeout(hlTimer[this.id]);
    }

    hlTimer[this.id] = setTimeout("autoHideLD('" + this.id + "')", hTime);
}

var hLThis = function(div)
{
    if (hlTimer && hlTimer[div]) {
        clearTimeout(hlTimer[div]);
    }

    _ge(div).title = lClickToEdit;
    _ge(div).style.backgroundColor = '#def4ff';
}

var hLDThis = function(div)
{
    if (hlTimer && hlTimer[div]) {
        clearTimeout(hlTimer[div]);
    }

    hlTimer[div] = setTimeout("autoHideLD('" + div + "')", 700);
}

function autoHideLD(div) {
    _ge(div).title                 = '';
    _ge(div).style.backgroundColor = '';
}

var sI = function (div, imgSrc) {
    div.src = '/images/' + getCookie('lang') + imgSrc + '.gif';
}

function hM(div)
{
    _ge(div).style.display = 'none';
}

function sM(div)
{
    _ge(div).style.display = 'block';
}

function cClass(div, name)
{
    _ge(div).className = name;
}

function flipThis(field)
{
    _ge(field).checked = !_ge(field).checked;
}

function checkThis(field)
{
    _ge(field).checked = '1';
}

function flip(value)
{
    if (ids.length < 100000) {
        for (var i = 0; i < ids.length; i++){
            if (ids[i]) {
                _ge('s_'+ids[i]).checked = value;
            }
        }
    } else {
        _ge('s_'+ids.length).checked = value;
    }

    if (_ge('deselect').style.display == 'none') {
        _ge('deselect').style.display = 'block';
        _ge('select').style.display = 'none';
    } else {
        _ge('deselect').style.display = 'none';
        _ge('select').style.display = 'block';
    }
}

String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g,"");
};

String.prototype.nl2br = function()
{
    return this.split("\n").join("<br/>\n");
};

var _dec = function()
{
    var el = arguments[0];
    if (!el) {
        return;
    }
    arguments[0] = el;
    var functionName = 'dec_' + el.className;
    if (window[functionName]) {
        return window[functionName](arguments);
    }
};

var _get_event_src = function(e)
{
    if (e) {
        return e.target;
    }
    if(window.event){
        return window.event.srcElement;
    }
    return null;
};

var _disable_select = function()
{
    document.onselectstart = new Function('return false');
};

var _enable_select = function(){
    document.onselectstart = new Function('return true');
};

var limit = function(objDiv, max)
{
    if(objDiv.value.length > max){
        objDiv.value = objDiv.value.substring(0, max);
    }
};

var conf = function ()
{
    if (confirm(arguments[0])) {
        functionName = arguments[1];
        if (window[functionName]) {
            if (arguments[8]) {
                disableButtons();
                positionProgress(arguments[8]);
            }
            return window[functionName](arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
        }
    }
}

function decline(num, form1, form2, form3, returnNum)
{
    //zwraca ostatni znak w liczbie
    liczba = (num % 10);

    //zwraca dwa ostatnie znaki w liczbie
    val = new String(num);
    lastTwo = val.substr(val.length - 2, 2);

    //Wybďż˝r liczby
    if (num == 1) {
        decl = 1;
    } else if (num == 11 || num == 12 || num == 13 || num == 14 || num == 15 || num == 16 || num == 17 || num == 18 || num == 19) {
        decl = 3;
    } else if (lastTwo == 11 || lastTwo == 12 || lastTwo == 13 || lastTwo == 14) {
        decl = 3;
    } else {
        switch (liczba) {
            case 2:
            decl = 2;
            break;
            case 3:
            decl = 2;
            break;
            case 4:
            decl = 2;
            break;
            default:
            decl = 3;
        }
    }

    switch (decl) {
        case 1:
        return ((returnNum) ? num + ' ' : '') + form1;
        break;
        case 2:
        return ((returnNum) ? num + ' ' : '') + form2;
        break;
        case 3:
        return ((returnNum) ? num + ' ' : '') + form3;
        break;
    }
}

function progressBar(text, type)
{
    return '<p class="pbr">' + text + '<br/><img src="/images/pb' + type + '.gif" class="pbr"/></p>';
}

function progress(divId, op, type, lPrefix)
{
    if (op == 'add') {
        msg = (type == 'g') ? lAddingToGroup : lAddingToAlbum;
    } else if (op == 'new') {
        msg = lAddingToNewAlbum;
    } else if (op == 'print') {
        msg = lAddingPrints;
    } else if (op == 'rot') {
        msg = lRotatingPhoto;
    } else {
        msg = (type == 'g') ? lRemovingFromGroup : lRemovingFromAlbum;
    }
    opsDiv = _ge(divId);
    opsDiv.innerHTML = progressBar(msg, 'b');
}

function positionProgress(text, dis) {
    if (dis) {
        disableButtons();
    }

    if (typeof( window.innerWidth ) == 'number') {
        //Non-IE
        var ww  = window.innerWidth;
        var wh  = window.innerHeight;
        var bgX = window.pageXOffset;
        var bgY = window.pageYOffset;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        var ww = document.documentElement.clientWidth;
        var wh = document.documentElement.clientHeight;
        var bgX = document.documentElement.scrollLeft;
        var bgY = document.documentElement.scrollTop;
    } else {
        //IE 4 compatible
        var ww  = document.body.clientWidth;
        var wh  = document.body.clientHeight;
        var bgX = document.body.scrollLeft;
        var bgY = document.body.scrollTop;
    }

    progDiv = document.getElementById('prog');

    progDiv.style.left   = (bgX + ((ww - 240) / 2)) + 'px';
    progDiv.style.top    = (bgY + (wh / 2)) + 'px';

    if (window.captureEvents) {
        window.captureEvents(Event.scroll);
    }

    savedText = text;
    window.onscroll = scrollProgress;
    progDiv.innerHTML = progressBar(text, 'b');
    progDiv.style.display = 'block';
}

function scrollProgress(doIt) {
    if (doneState == false) {
        return;
    }
    doneState = false;
    clearTimeout(scrollTimer);
    scrollTimer = setTimeout('scrollProgress(true)', 10);
    if (doIt || 1) {
        doneState = true;
        positionProgress(savedText);
    }
}

function cancelProgress()
{
    savedText = '';
    _ge('prog').style.display = 'none';
    clearTimeout(scrollTimer);
    window.onscroll = null;
}

function addOnload(newFunction) {
    if(typeof window.addEventListener != 'undefined') {
        //dla przeglądarek: gecko, safari, konqueror i standardardowych
        window.addEventListener('load', newFunction, false);
    } else if(typeof document.addEventListener != 'undefined') {
        //dla przeglądarki opera 7
        document.addEventListener('load', newFunction, false);
    } else if(typeof window.attachEvent != 'undefined') {
        //dla przeglądarki win/ie
        window.attachEvent('onload', newFunction);
    }  else {
        //dla przeglądarek mac/ie5 i wszystkich starszych
        if(typeof window.onload == 'function') {
            //przechowuje aktualne ustawienia onload
            var existing = onload;

            //i dodaje nową funkcję
            window.onload = function()
            {
                //wywołanie aktualnych funkcji
                existing();

                //wywołanie dodanej funkcji
                newFunction();
            };
        } else {
            window.onload = newFunction;
        }
    }
}

//Lepsza wersja funkcji
//Parametry: nazwa klasy, rodzaj elementu, container
function getElementsByClassName(strClass, strTag, objContElm) {
    strTag      = strTag || "*";
    objContElm  = objContElm || document;
    var objColl = objContElm.getElementsByTagName(strTag);

    if (!objColl.length &&  strTag == "*" &&  objContElm.all) {
        objColl = objContElm.all;
    }

    var arr      = new Array();
    var delim    = strClass.indexOf('|') != -1  ? '|' : ' ';
    var arrClass = strClass.split(delim);
    for (var i = 0, j = objColl.length; i < j; i++) {
        var arrObjClass = objColl[i].className.split(' ');

        if (delim == ' ' && arrClass.length > arrObjClass.length) {
            continue;
        }

        var c = 0;

        comparisonLoop:
        for (var k = 0, l = arrObjClass.length; k < l; k++) {
            for (var m = 0, n = arrClass.length; m < n; m++) {
                if (arrClass[m] == arrObjClass[k]) {
                    c++;
                }

                if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
                    arr.push(objColl[i]);
                    break comparisonLoop;
                }
            }
        }
    }
    return arr;
}

function posMenu(div, ref, x, y)
{
    var ref     = document.getElementById(ref);
    var menu    = document.getElementById(div);
    var offsetX = 0;
    var offsetY = 0;

    do {
        offsetX += ref.offsetLeft;
        offsetY += ref.offsetTop;
        objParent = ref.offsetParent.tagName;
        ref = ref.offsetParent;
    } while(objParent != 'BODY' && objParent != 'HTML');

    menu.style.left = (offsetX + x) + 'px';
    menu.style.top  = (offsetY + y) + 'px';
}

var initHL = function (className, element)
{
    var elements = getElementsByClassName(className, element);
    for (var no = 0; no < elements.length; no++) {
        elements[no].onmouseover = hL;
        elements[no].onmouseout  = hLD;
    }
}

var initLang = function()
{
    lang            = (getCookie('lang') == 'en') ? 'pl' : 'en';
    jQuery('#lang_' + lang).click(function() {document.location='/?lang=' + lang;}).css('cursor', 'pointer');
}

var initBoxLinks = function()
{
    jQuery('div.box div.p').hover(
    function() {
        jQuery(this).addClass('hl');
    },
    function() {
        jQuery(this).removeClass('hl');
    }
    );
}

var initSubmits = function()
{
    jQuery('form input[@class=sub]').hover(
    function() {
        jQuery(this).addClass('sub1');
    },
    function() {
        jQuery(this).removeClass('sub1');
    }
    );
}

//Inicjalizacja onClick zmiany języka
addOnload(initLang);
addOnload(initBoxLinks);
addOnload(initSubmits);
