window.onload = startPage;

function startPage() {
    pridatTargetBlank('new_window');
    nastavitAkceProGalerie('onclickChangeDetail');
    // putSuggest('search_input');
    // nastavitMenu('menu');
};

function pridatTargetBlank (elmClassName) {
    var elms = document.getElementsByTagName('a');
    for (var i = 0; i < elms.length; i++) {
        if (elms[i].className == elmClassName) elms[i].target = '_blank';
    }
};

function nastavitAkceProGalerie (elmClassName) {
    var elms = document.getElementsByTagName('a');
    for (var i = 0; i < elms.length; i++) {
        if (elms[i].className == elmClassName) {
            elms[i].onmousedown = zmenDetailGalerie;
            elms[i].href = "javascript: void(0)";
        }
    }
};

function zmenDetailGalerie (e) {
    e = (e) ? e : window.event;
    var nosic;
    if (e.target) nosic = e.target;
    else if (e.srcElement) nosic = e.srcElement;
    if (nosic.nodeType == 3) nosic = nosic.parentNode;
    if (nosic.tagName == 'IMG') nosic = nosic.parentNode;
    var castiId = nosic.id.split('_');
    var detailElm = document.getElementById('img_detail_'+castiId[1]);
    var newDetailHTML = '<a href="'+eval('images_'+castiId[1]+'["imgUrls"]')+castiId[2]+'/full.jpg" target="_blank" class="thickbox">\n';
    var imageSizeString = eval('images_'+castiId[1]+'["'+castiId[2]+'_size"]');
    var imageSize = new Array();
    imageSize = imageSizeString.split('_');
    
    newDetailHTML = newDetailHTML + '<img src="'+eval('images_'+castiId[1]+'["imgUrls"]')+castiId[2]+'/'+eval('images_'+castiId[1]+'["fileNms"]')+'" alt="'+eval('images_'+castiId[1]+'["'+castiId[2]+'"]')+'" width="'+imageSize[0]+'" height="'+imageSize[1]+'" />\n';
    newDetailHTML = newDetailHTML + '</a>\n';
    
    detailElm.innerHTML = newDetailHTML; 
    return true;
};

function nastavitMenu (parentId) {
    var liElms = document.getElementsByTagName('li');
    for (var i = 0; i < liElms.length; i++) {
        if (((liElms[i].parentElement) && (liElms[i].parentElement.id == parentId)) || ((liElms[i].parentNode) && (liElms[i].parentNode.id == parentId))) {
            liElms[i].onmouseover = zobrazPodmenu;
            liElms[i].onmouseout = skryjPodmenu;
        }
    }
};

function zobrazPodmenu(e) {
    e = (e) ? e : window.event;
    var nosic;
    if (e.target) nosic = e.target;
    else if (e.srcElement) nosic = e.srcElement;
    if (nosic.tagName == 'A') nosic = nosic.parentNode;
    
    var ulElms = document.getElementsByTagName('ul');
    for (var i = 0; i < ulElms.length; i++) {
        if (((ulElms[i].parentElement) && (ulElms[i].parentElement == nosic)) || ((ulElms[i].parentNode) && (ulElms[i].parentNode == nosic))) {
            ulElms[i].style.filter = 'alpha(opacity=10)';
            ulElms[i].style.opacity = '.1';
            ulElms[i].style.display = 'block';
            ulElms[i].onmouseover = zobrazMenu;
            ulElms[i].onmouseout = skryjMenu;
            setTimeout('fadeToMaximum(\''+ulElms[i].id+'\',0,5,25)',25); // id of element, init percents, percent by one round
        }
    }
};

function fadeToMaximum(elmId,initPerc,percByRound,timeByRound) {
    var targetOpacity = 100;
    if (initPerc < targetOpacity) {
        var elm = document.getElementById(elmId);
        var newPercnets = ((initPerc + percByRound) < targetOpacity) ? (initPerc + percByRound) : targetOpacity;
        elm.style.filter = 'alpha(opacity='+newPercnets+')';
        elm.style.opacity = newPercnets / 100;
        setTimeout('fadeToMaximum(\''+elmId+'\','+newPercnets+','+percByRound+','+timeByRound+')',timeByRound);    
    }
};

function skryjPodmenu(e) {
    e = (e) ? e : window.event;
    var nosic;
    if (e.target) nosic = e.target;
    else if (e.srcElement) nosic = e.srcElement;
    if (nosic.tagName == 'A') nosic = (nosic.parentNode) ? nosic.parentNode : nosic.parentElement;
    
    var ulElms = document.getElementsByTagName('ul');
    for (var i = 0; i < ulElms.length; i++) {
        if (((ulElms[i].parentElement) && (ulElms[i].parentElement == nosic)) || ((ulElms[i].parentNode) && (ulElms[i].parentNode == nosic))) {
            ulElms[i].style.display = 'none';
        }
    }
};

function zobrazMenu(e) {
    e = (e) ? e : window.event;
    var nosic;
    if (e.target) nosic = e.target;
    else if (e.srcElement) nosic = e.srcElement;
    if (nosic.tagName == 'LI') nosic = (nosic.parentNode) ? nosic.parentNode : nosic.parentElement;
    if (nosic.tagName == 'A') nosic = (nosic.parentNode) ? nosic.parentNode.parentNode : nosic.parentElement.parentElement;
    
    nosic.style.display = 'block';
};

function skryjMenu(e) {
    e = (e) ? e : window.event;
    var nosic;
    if (e.target) nosic = e.target;
    else if (e.srcElement) nosic = e.srcElement;
    if (nosic.tagName == 'LI') nosic = (nosic.parentNode) ? nosic.parentNode : nosic.parentElement;
    if (nosic.tagName == 'A') nosic = (nosic.parentNode) ? nosic.parentNode.parentNode : nosic.parentElement.parentElement;
    
    nosic.style.display = 'none';
};

function changeComDis (newStyleDisp,targetClass) {
    var trElms = document.getElementsByTagName('tr');
    for (var i = 0; i < trElms.length; i++) {
        if (trElms[i].className == targetClass) {
            trElms[i].style.display = newStyleDisp;
        }
    }
};

function skryj(elmId,className,elmType) {
    if (!className) {
        document.getElementById(elmId).style.display = 'none';
    } else {
        var elms = document.getElementsByTagName(elmType);
        for (var i = 0; i < elms.length; i++) {
            if (elms[i].className == className) elms[i].style.display = 'none';
        }
    }
    return false;
}

function zobraz(elmId,className,elmType) {
    if (!className) {
        document.getElementById(elmId).style.display = '';
    } else {
        var elms = document.getElementsByTagName(elmType);
        for (var i = 0; i < elms.length; i++) {
            if (elms[i].className == className) elms[i].style.display = '';
        }
    }
    return false;
}

var correctHref = 0;

function checkPhone () {
    if (getCheckedValue('shipment_type') == '15') {
        alert(window.noPhoneMess);
        return false;
        if (document.getElementById('orderer_phone').value.length < 9) {
            alert(window.noPhoneMess);
            linkElm.href = 'javascript:void(0)';
            return false;
        } else {
            return true;
        }
    }
}

function checkRuleSubmit (ruleElmId,linkElmId) {
    var ruleElm = document.getElementById(ruleElmId);
    var linkElm = document.getElementById(linkElmId);
    if (window.correctHref == 0) window.correctHref = linkElm.href;
    if (ruleElm && linkElm) {
        if (ruleElm.checked) {
            linkElm.href = window.correctHref;
            return true;
        } else {
            alert(window.noRulesMess);
            linkElm.href = 'javascript:void(0)';
            return false;
        }
    }
}

function addBBcode (type, targetId) {
    var targetElm = document.getElementById(targetId);
    var html = targetElm.value;
    switch(type)
    {
    case 'b':
      html = html + '[b][/b]';
      break;
    case 'i':
      html = html + '[i][/i]';
      break;
    case 'u':
      html = html + '[u][/u]';
      break;
    case 'img':
      html = html + '[img][/img]';
      break;
    case 'url':
      html = html + '[url=""][/url]';
      break;
    }
    targetElm.value = html;
}

function validateShipmentSelection (newValue) {
    // allowed[newValue] -> array of allowed values for payment
    var paymentInput = document.shipment.payment_type;
    var actualPaymentVal = getCheckedValue(paymentInput);
    if (!in_array(actualPaymentVal,window.allowed[newValue])) {
        setCheckedValue(paymentInput,window.allowed[newValue][0]);
    }
    var allInputs = document.getElementsByTagName('input');
    for (var i = 0; i < allInputs.length; i++) {
        for (var y = 0; y < 25; y++) {
            if (allInputs[i].id == 'payment_type_'+y) {
                if (in_array(y,window.allowed[newValue])) {
                    allInputs[i].disabled = false;
                } else {
                    allInputs[i].disabled = true;
                }                
            }
        }
    }
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function in_array (needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
 
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}

