// ----- G L O B A L ------

// function higlight(colNo, colCnt, color)
// function insertEntity()
// function parentInsertEntity(pInputIdent, value)
// function wopen(path, size)

//var pathPrefix = "http://ecards.designplus.local/";
var pathPrefix = "http://www.ecards.cz/";
var befCnt = -1;

function _higlightCols(cnt, colCnt, color) {
	for (f = 0; f < colCnt; f++)
		document.getElementById("col" + f + "_" + cnt + "no").style.backgroundColor = color;
}

var switch_beforeLink = "";
var switch_beforeSrc = "";


function scX() {
    return 0;
}

function scY() {
	return document.documentElement.scrollTop;
}

function switchSrc(imgIdent, switchTo, resX, resY, switchImgId) {
    var imgObj = document.getElementById(imgIdent);

    imgObj.width = resX;
    imgObj.height = resY;
    imgObj.src = switchTo;

    imgRef = document.getElementById(switchImgId);
    if (imgRef != null) {
        var newBefLink = imgRef.parentNode.href;
        var newBefSrc = imgRef.src;

        imgRef.parentNode.href = switch_beforeLink + "'" + switchImgId + "')";
        imgRef.src = switch_beforeSrc;

        var pos = newBefLink.length - 1;
        var sepNum = 0;
        while (pos >= 0 && sepNum != 2) {
            if (newBefLink.substr(pos, 1) == "'") sepNum++;
            if (sepNum != 2) pos += -1;
        }

        switch_beforeLink = newBefLink.substr(0, pos);
        switch_beforeSrc = newBefSrc;
    }
}

var closeTimeout = null;

function closeFinish(baseObjIdent) {
    document.getElementById(baseObjIdent).style.display = "none";
    closeTimeout = null;
}

function closeClear() {
    if (closeTimeout != null) {
        clearTimeout(closeTimeout);
        closeTimeout = null;
    }
}

function closeTry(trgEvent, baseObjIdent, closeFunc) {
    var trgObj = document.getElementById(baseObjIdent);
    if (trgObj != null && trgObj.style.display == "block") {

        var selfEl = (trgEvent.srcElement == null) ? trgEvent.target : trgEvent.srcElement;
        var target = (trgEvent.toElement == null) ? trgEvent.relatedTarget : trgEvent.toElement;

        while (target != null && target != trgObj)
            target = target.parentNode;

        if (target != trgObj) {
            if (closeFunc == null) {
                closeClear();
                closeTimeout = setTimeout("closeFinish('" + baseObjIdent + "')", 1000);

                return true; // zavre se
            } else {
                closeFunc();
                return true; // zavre se
            }
        } else closeClear();
    }
    return false; // nezavre
}

function higlightCols(cnt, colCnt, color) {
	if (befCnt != -1) _higlightCols(befCnt, colCnt, "white");
	_higlightCols(cnt, colCnt, color);
	befCnt = cnt;
}

function isNumericBracket(iStr) {
    var brk = false;
    var num = "";
    var parts = 0;
    var inBracket = false;

    for (var f = 0; f < iStr.length; f++) {
        if (inBracket) {
            switch (iStr.substr(f, 1)) {
                case ">" :
                    inBracket = false;
                    break;
            }
        } else {
            switch (iStr.substr(f, 1)) {
                case "0" :
                case "1" :
                case "2" :
                case "3" :
                case "4" :
                case "5" :
                case "6" :
                case "7" :
                case "8" :
                case "9" :
                    num += iStr.substr(f, 1);
                    break;
    
                case "(" :
                case ")" :
                case " " :
                    parts += 1;
                    break;
    
                case "<" :
                    inBracket = true;
                    break;
    
                default :
                    brk = true;
                    break;
            }
        } // if
    } // switch

    return brk || parts < 3 ? 0 : (parseInt(num));
}

function myRound(iVal) {
    return Math.round(iVal * 10) / 10;
}

function pr(iVal) {
    var flr = Math.floor(iVal);
    var rnd = Math.round(iVal * 10) / 10;
    var tmp = rnd + (flr != rnd ? "0" : ",00") + " Kč";
    return tmp.replace(".", ",");
}

function urlLeaf(iUrl) {
    var pos = iUrl.length - 1;
    var brk = false;
    var fin = "";
    var aktChar = "";
    
    while (pos >= 0 && aktChar != "/") {
        aktChar = iUrl.substr(pos, 1);
        if (aktChar != "/") pos += -1;
        else pos++;
    }
    
    return iUrl.substr(pos, iUrl.length - pos);
}

function fixStr(iStr, limit) {
    if (iStr.length < limit) {
        /*
        var f;
        var stringsRem = new Array("<", ">", "&", "=");
        var st = new String(iStr);

        for (f = 0; f < stringsRem.length; f++)
            st = st.replace(stringsRem[f], "");

        st = st.replace("\"", "'");

        return st;*/

        var aktChar = "";
        var fin = "";

        for (f = 0; f < iStr.length; f++) {
            aktChar = iStr.substr(f, 1);

            switch (aktChar) {
                case "<" :
                case ">" :
                case "&" :
                case "=" :
                    break;

                case "\"" :
                    fin += "'";
                    break;

                default :
                    fin += aktChar;
                    break;
            }
        }
        return fin;

    } else return iStr;
}

function showPhoto(photoID, addParams) {
    document.location.href = "photo.browse.php?p=" + photoID + addParams;
}

function showAlbum(albumID) {
    document.location.href = "photo.php?setalbum=" + albumID;
}

function openSlideshow() {
    window.open("photo.slideshow.php", "ecards_slideshow", "width=700,height=750,resizable=0,scrollbars=0,menubar=0");
}

function openSlideshowFrom(iPhotoID) {
    window.open("photo.slideshow.php?pid=" + iPhotoID, "ecards_slideshow", "width=700,height=750,resizable=0,scrollbars=0,menubar=0");
}

function printsMenu() {
    var mObj = document.getElementById("prints_menu");
    var oObj = document.getElementById("prints_opener");

    mObj.style.display = "block";
    mObj.style.left = elementPositionX(oObj) + "px";
    mObj.style.top  = (elementPositionY(oObj) + 20) + "px";
}

function fotolabInfo(trgEvent, iLevel) {
    var mObj = document.getElementById("fotolab_info");
    switch (iLevel) {
        case 0 : mObj.innerHTML = "Tisk fotografie bude pravděpodobně nekvalitní, fotografie má nízné rozlišení."; break;
        case 1 : mObj.innerHTML = "Tisk fotografie bude <span class='errorText'>velmi nekvalitní</span>, nemá dostatečné rozlišení. Nedoporučujeme fotografii tisknout."; break;
    }
    mObj.style.display = "block";
    if (trgEvent.clientX && trgEvent.clientY) {
        mObj.style.left = (trgEvent.clientX - 10) + "px";
        mObj.style.top = (trgEvent.clientY - 10 + scY()) + "px";
    } else {
        mObj.style.left = (elementPositionX(trgEvent) - 200 + trgEvent.offsetWidth - 8) + "px";
        mObj.style.top = elementPositionY(trgEvent) + "px";
    }
}

function needLoginErrorSpec(iLoc, iRegLink) {
    if (msg != null) {
        msg.show("<span class='darkBlueText' style='font-size : 14px;font-weight : bold'>Nejdříve se prosím přihlaste.</span>");
        msg.setLoginForm(iLoc, iRegLink);
    }
}

function needLoginError() {
    if (msg != null) {
        msg.show("<span class='darkBlueText' style='font-size : 14px;font-weight : bold'>Nejdříve se prosím přihlaste.</span>");
        msg.setLoginForm(null, null);
        //msg.show("<span class=\"errorText\">Pro využití této funkce musíte být přihlášen.</span><br/>Přejít na <a href=\"javascript:msg.setLoginForm()\">přihlášení</a>");
    }
}

function msgError(errMsg) {
    if (msg != null) {
        msg.show("<span class=\"errorText\">" + errMsg + "</span>");
        msg.setButton("Pokračovat");
    }
}

function addAccountFav(accountID, isLogged) {
    if (isLogged) {
        if (exec == null) alert("Chyba!");
        else exec.make("fav_account(" + accountID + ")", null);
    } else {
        needLoginError();
    }
}

/*
function insertEntity() {
	// vlozi kontakt z adresare (showcard.php)
	selObj = document.getElementById("dirVals");
	if (selObj.options.length > 0) {
		txtObj = document.getElementById("sendFor");
		
		txtObj.value = txtObj.value + selObj.options[selObj.selectedIndex].value + "\r\n";
	}
}*/

function updateSky() {
    var cont = document.getElementById("skycraperCont");
    var left = document.body.offsetLeft + document.body.offsetWidth + 20;
    var top  = scY() + 190;

    cont.style.left    = left + "px";
    cont.style.top     = top + "px";
    cont.style.display = "block";
}

function openMap() {
	wopen("info.php?pg=kde_nas_najdete", "mapwin");
}

function _playSound(soundName) {
	container = document.getElementById("soundContainer");

	if (soundName != "") {
		//soundName = "http://www.ecards.cz/music/" + soundName;
		//var put = "";
		//if (navigator.appName.toLowerCase() == "microsoft internet explorer") put = '<bgsound src="' + soundName + '" loop="infinite"></bgsound>';
		//else put = '<embed src="' + soundName + '" id="sound_el" hidden="true" border="0" width="5" height="5" autostart="true" loop="true" hidden="true" mastersound></embed>';
		//put = '<bgsound src="' + soundName + '" loop="infinite"></bgsound>';
		//container.innerHTML = "&nbsp;" + put;

		document.getElementById("soundFrame").src = "/playsound.php?soundName=" + soundName;

	} else {
        //container.innerHTML = "";
        
        document.getElementById("soundFrame").src = "/playsound.php";
    }
}

function playSound() {
	selector  = document.getElementById("soundSelector");
	_playSound(selector.options[selector.selectedIndex].value);
}

function elementPositionX(targetEl) {
	var value = 0;
	while (targetEl != null) {
		value += targetEl.offsetLeft;
		targetEl = targetEl.offsetParent;
	}
	return value;
}

function elementPositionY(targetEl) {
	var value = 0;
	while (targetEl != null) {
		value += targetEl.offsetTop;
		targetEl = targetEl.offsetParent;
	}
	return value;
}

function elementPosition(targetEl) {
	return new Array(elementPositionX(targetEl), elementPositionY(targetEl));
}

function swapNodes(container, nodeA, nodeB) {
	// zjisteni pozic
	if (container.childNodes.length > 0) {
		var posA = 0;
		var posB = 0;
		var founded = false;
		
		while (posA < container.childNodes.length && !founded) {
			if (container.childNodes[posA] == nodeA) founded = true;
			else posA++;
		}

		if (founded) {
			founded = false;
			while (posB < container.childNodes.length && !founded) {
				if (container.childNodes[posB] == nodeB) founded = true;
				else posB++;
			}
			
			if (founded) {
				var childs = new Array();
				var minPos = 0;
				var maxPos = 0;
				var refFirst = null;
				var refLast  = null;
				var f = 0;
				
				if (posA < posB) {
					minPos = posA;
					maxPos = posB
					
					refFirst = nodeB;
					refLast  = nodeA;
				} else {
					minPos = posB;
					maxPos = posA;
					
					refFirst = nodeA;
					refLast  = nodeB;
				}
				var len	= container.childNodes.length;
				
				for (f = container.childNodes.length - 1; f >= minPos; f += -1) {
					if (container.childNodes[f] != refFirst && container.childNodes[f] != refLast)
						childs[childs.length] = container.childNodes[f];
					container.removeChild(container.childNodes[f]);
				}
				
				container.appendChild(refFirst);
				f = childs.length - 1;
				while (f >= 0) {
					if (container.childNodes.length == maxPos) container.appendChild(refLast);
					else {
						container.appendChild(childs[f]);
						f += -1;
					}
				}
				
				if (container.childNodes.length == maxPos) container.appendChild(refLast);					
			} // if
		}
	} // if
}

function cutPeriod(iVal) {
	return ((iVal.substr(iVal.length - 2, 2) == "px") ? iVal.substr(0, iVal.length - 2) : iVal) * 1;
}

function orig_imgLoad(picID) {
	var objLoadCont = document.getElementById("orig_loadContainer" + picID + "no");
	var objImg      = document.getElementById("orig_image" + picID + "no");

	objLoadCont.style.display = "none";
	objImg.style.display = "block";
}

function orig_show(picID) {
	var objCont    = document.getElementById("orig_container" + picID + "no");
	var objSubCont = document.getElementById("orig_subContainer" + picID + "no");
	var objImg     = document.getElementById("orig_image" + picID + "no");
	
	var path = document.getElementById("orig_fullpath" + picID + "no").value;
	var resX = document.getElementById("orig_resX" + picID + "no").value * 1;
	var resY = document.getElementById("orig_resY" + picID + "no").value * 1;
	
	var maxRes = 580;
	if (resX > maxRes || resY > maxRes) {
		if (resX > resY) {
			resY = Math.round(resY * (maxRes / resX));
			resX = maxRes;
		} else if (resY > resX) {
			resX = Math.round(resX * (maxRes / resY));
			resY = maxRes;
		}
	}
	
	if (objCont.style.display == "none") {
		objSubCont.style.width = resX + "px";
		objSubCont.style.height = resY + "px";
		objCont.style.display = "block";
		
		objImg.src = path;
		//objImg.width = resX;
		//objImg.height = resY;
	} else objCont.style.display = "none";
}

function parentInsertEntities(pInputIdent) {
    var pEl = window.opener.document.getElementById(pInputIdent);

    if (pEl != null) {
        var index = 0;
        var aEl = null;
        var toPut = "";

        do {
            aEl = document.getElementById("check" + index + "no");

            if (aEl != null) {
                if (aEl.checked)
                    toPut += (toPut == "" && pEl.value == "" ? "" : ", ") + aEl.value;
                index++;
            }
        } while (aEl != null);

        pEl.value = pEl.value + toPut;
        window.close();
        return true;
    }
    return false;
}

function parentInsertEntity(pInputIdent, toPut) {
	var pEl = window.opener.document.getElementById(pInputIdent);
	if (pEl != null) {
		if (pEl.value != "") pEl.value = pEl.value + ", ";
		pEl.value = pEl.value + toPut;
		window.close();
		return true;
	} else return false;
}

function wopen(path, size) {
    var w = 0;
    var h = 0;
	putSize = ""
	switch (size) {
		case "tiny" :
			putSize = "resizable=0";
			w = 250;
			h = 300;
			break;

		case "small" :
			putSize = "resizable=0";
			w = 400;
			h = 370;
			break;

		case "medium" :
			putSize = "resizable=0";
			w = 500;
			h = 300;
			break;

		case "big" :
			putSize = "resizable=0";
			w = 500;
			h = 500;
			break;
			
		case "long" :
		    putSize = "resizable=0";
		    w = 630;
		    h = 385;
		    break;

		case "biglong" :
			putSize = "resizable=0";
			w = 457;
			h = 595;
			break;

		case "invite_wind" :
			putSize = "resizable=0";
			w = 450;
			h = 650;
			break;

		case "huge" :
			putSize = "resizable=0,scrollbars=1";
			w = 810;
			h = 600;
			break;
			
		case "mapwin" :
			putSize = "resizable=0,scrollbars=0";
			w = 700;
			h = 600;
			break;			

		case "huge_resizable" :
			putSize = "resizable=1,scrollbars=1";
			w = 800;
			h = 600;
		    break;

        case "huge_noscroll" :
			putSize = "resizable=0,scrollbars=0";
			w = 800;
			h = 600;
			break;

		case "resizable" :
			putSize = "resizable=1,scrollbars=auto";
			w = 500;
			h = 300;
			break;
	}

    var left = (screen.width - w) / 2;
    var top  = (screen.height - h) / 2;

	window.open(path, "ecards_window_" + size, putSize + ",menubar=no,width=" + w + ",height=" + h + ",left=" + left + ",top=" + top);
}


// ------- D E T E C T --------

var agt = navigator.userAgent.toLowerCase();

var is_nav   = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_gecko = (agt.indexOf('gecko') != -1);
var is_ie    = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_opera = (agt.indexOf("opera") != -1);

var is_other = (!is_nav && !is_gecko && !is_ie && !is_opera);

var have_js = (!is_other);


// -------- C A R D S W I T C H ---------

function list_swap(noA, noB) {
	objBef = document.getElementById("cardPage" + noA + "no");
	objNew = document.getElementById("cardPage" + noB + "no");
	objBef.style.display = "none";
	objNew.style.display = "block";
}

function list_pageLast(aktual) {
	tmpLast = aktual - 1;
	if (tmpLast >= 0) list_swap(aktual, tmpLast);
}

function list_pageNext(aktual) {
	tmpNext = aktual + 1;
	objNext = document.getElementById("cardPage" + tmpNext + "no");
	if (objNext !== null) list_swap(aktual, tmpNext);
	else list_swap(aktual, 0);
}


// ---------- T O P M E N U -----------

var lastMenuItem = "";
var onceOut = false;

function topMenu_open(iId) {
	var base    = document.getElementById(iId);
	var childs = document.getElementById(iId + "_childs");

	//base.style.backgroundColor = "white";
	//base.style.height = "25px";
	childs.style.display = "block";
	childs.style.zIndex = "100000";

    childs.style.left = elementPositionX(base) + "px";
    childs.style.top = (elementPositionY(base) + parseInt(base.style.height) + 5) + "px";
}

function topMenu_close(iId) {
	var base   = document.getElementById(iId);
	var childs = document.getElementById(iId + "_childs");

    //base.style.backgroundColor = "";
	childs.style.display = "none";
}

function closeTopMenu() {
	if (lastMenuItem != "") {
		topMenu_close(lastMenuItem);
		lastMenuItem = "";
    }
}

function openTopMenu(index) {
    if ("tmenu" + index + "no" != lastMenuItem) {
        closeTopMenu();
    	var newItem = "tmenu" + index + "no";

    	topMenu_open(newItem);
    	lastMenuItem = newItem;
    	onceOut = false;
	}
}

function topMenuOut(index, dEvent) {
    if (lastMenuItem != "") {
        closeTry(dEvent, "tmenu" + index + "no_childs", closeTopMenu);
    }
}

// ----- A B S T R A C T ------

function photoCollection() {}
function photoDialogCollection() {}
function inputCollection() {}

