﻿/* General application wide javascript scripts */

function subMenu(subMenuName, displayStatus)
{
    obj = window.document.getElementById(subMenuName);
    if (obj != null)
    {
        obj.style.display = displayStatus;
    }
}

function isUserAgent(distinctUserAgentNameSubstring)
{
    return (navigator.userAgent.toLowerCase().indexOf(distinctUserAgentNameSubstring.toLowerCase()) != -1);
}

function doRelayedPostBack(postbackButtonTriggerId) {
    var target = window.document.getElementById(postbackButtonTriggerId);
    
    if (target == null)
        return;
    
    if (target.onclick != null)
        target.click();
    else if (target.href != null)
        window.location = target.href;
}

function pressReturnKeyEventHandler(eventObj, targetToClickObjectName) {
    
    var keyCode = 0;
    if (window.event)    // IE
        keyCode = eventObj.keyCode;
    else if (eventObj.which)    // Netscape/Firefox/Opera
        keyCode = eventObj.which;
    
    if (keyCode == 13) {
        
        if (!eventObj)
            eventObj = window.event;
        eventObj.cancelBubble = true;
        if (eventObj.stopPropagation)
            eventObj.stopPropagation();

        eventObj.returnValue = false;

        var target = window.document.getElementById(targetToClickObjectName);
        target.focus();
        if (target.onclick != null) {
            target.click();
        }
        else if (target.href != null) {
            window.location = target.href;
        }

        //eventObj.cancelBubble = true;

        return false;
    }
   
        
    
    return true;
}

// direction: 1 - left, (-1) - right
// step: "short" - 3/4 links' window container size, "long" - 1/4 links' window size
function scrollLinks(direction, stepSize, currentOffsetElementName, linksElementName, endOfLinksElementName)
{
	var currentOffsetElement = window.document.getElementById(currentOffsetElementName);
	var linksElement = window.document.getElementById(linksElementName);
	var endOfLinksElement = window.document.getElementById(endOfLinksElementName);
	
	if (isUserAgent("MSIE"))
	{
		if (linksElement.scrollWidth <= linksElement.style.pixelWidth)
			return;
	}
	else
	{
		if (endOfLinksElement.offsetLeft <= linksElement.offsetWidth)
			return;
	}
	
	var offset = 0;
	switch (stepSize)
	{
		case "long":
			if (isUserAgent("MSIE"))
			{
				offset = linksElement.scrollWidth;
			}
			else
			{
				offset = endOfLinksElement.offsetLeft;
			}
			offset = offset * 1 / 4;
			break;
		case "short":
			if (isUserAgent("MSIE"))
			{
				offset = linksElement.style.pixelWidth;
			}
			else
			{
				offset = linksElement.offsetWidth;
			}
			offset = offset * 3 / 4;
			break;
        case "shorter":
            if (isUserAgent("MSIE"))
            {
                offset = linksElement.style.pixelWidth;
            }
            else
            {
                offset = linksElement.offsetWidth;
            }
            offset = offset * 1 / 5.21;
    break;
		default: 	// "shortest"
			if (isUserAgent("MSIE"))
			{
				offset = linksElement.style.pixelWidth;
			}
			else
			{
				offset = linksElement.offsetWidth;
			}
			offset = offset * 1 / 8;
			break;
	}
	
	var newOffsetValue = parseInt(currentOffsetElement.value) + direction * offset;
	var furthestOffset = newOffsetValue;
	if (isUserAgent("MSIE"))
	{
		furthestOffset =-(linksElement.scrollWidth - linksElement.style.pixelWidth);
	}
	else
	{
		furthestOffset = -(endOfLinksElement.offsetLeft - linksElement.offsetWidth);
	}

	if (0 < newOffsetValue)	// max scroll to the right
	{
		newOffsetValue = 0;
	}
	else if (newOffsetValue < furthestOffset)	// max scroll to the left
	{
		newOffsetValue = furthestOffset;
	}
	currentOffsetElement.value = newOffsetValue;
	linksElement.style.left = newOffsetValue + "px";
}

// direction: 0 - first window, 1 - next window, (-1) - previous window
function showFilters(direction, windowSizeElementName, filterHitsCountElementName, currentWindowElementName, filtersContainerElementName, filterHitsInfoElementName)
{
    var windowSizeElement = window.document.getElementById(windowSizeElementName);
    var filterHitsCountElement = window.document.getElementById(filterHitsCountElementName);
    var currentWindowElement = window.document.getElementById(currentWindowElementName);

    // determine virtual windows parameters
    if (direction == 0)
    {
        currentWindowElement.value = "0";
    }
    else
    {
        var totalWindows = parseInt(filterHitsCountElement.value) / parseInt(windowSizeElement.value);
        if (Math.floor(totalWindows) < totalWindows)     // partially filled window
        {
            totalWindows = Math.floor(totalWindows) + 1;
        }
        
        // index of window to display
        var currentWindowIndex = parseInt(currentWindowElement.value) + direction;
        
        // looping mechanism
        if (currentWindowIndex < 0)
        {
            currentWindowIndex = totalWindows - 1;
        }
        else if (currentWindowIndex > totalWindows - 1)
        {
            currentWindowIndex = 0;
        }
        
        currentWindowElement.value = currentWindowIndex;
    }

    // convert from virtual windows to item indeces
    var firstFilterIndexToShow = parseInt(currentWindowElement.value) * parseInt(windowSizeElement.value) ;
    var lastFilterIndexToShow = Math.min((parseInt(currentWindowElement.value) + 1) * parseInt(windowSizeElement.value) - 1, parseInt(filterHitsCountElement.value) - 1);
    
   
    // do the actual showing
    var filterHitsInfoElement = window.document.getElementById(filterHitsInfoElementName);
    filterHitsInfoElement.innerHTML = (firstFilterIndexToShow + 1) + "-" + (lastFilterIndexToShow + 1) + "/" + filterHitsCountElement.value;

    var filtersContainerElement = window.document.getElementById(filtersContainerElementName);
    var filterElements = new Array();
    
    var i = 0;
    var j = 0;
    while (i < filtersContainerElement.childNodes.length)
    {
        if (filtersContainerElement.childNodes[i].nodeName.toLowerCase() == "li" && filtersContainerElement.childNodes[i].className == "")
        {
            filterElements[j] = filtersContainerElement.childNodes[i];
            j++;
        }
        i++;
    }
    
    for (i = 0; i < filterElements.length; i++)
    {
        if (firstFilterIndexToShow <= i && i <= lastFilterIndexToShow)
        {
            filterElements[i].style.display = "";
        }
        else
        {
            filterElements[i].style.display = "none";
        }
    }
}

function setDisplayStyleFor(arrayOfElementNames, displayStyle)
{
    for (i = 0; i < arrayOfElementNames.length; i++)
    {
        var element = window.document.getElementById(arrayOfElementNames[i]);
        if (element != null)
        {
            element.style.display = displayStyle;
        }
    }
}

// registers checkBoxElement state into globalToolBoxItemsBagElementName
function toolBoxMarkState(checkBoxElement, bagItemId, globalToolBoxItemsBagElementName)
{
    var globalToolBoxItemsBagElement = window.document.getElementById(globalToolBoxItemsBagElementName);
    
    globalToolBoxItemsBagElement.value = globalToolBoxItemsBagElement.value.replace(bagItemId, "");
    
    if (checkBoxElement.checked)
    {
        globalToolBoxItemsBagElement.value += bagItemId;
    }
}

function toolBoxMarkAllCandidatesState(action, globalToolBoxItemsBagElementName, globalToolBoxCandidateItemsBagElementName, elementToShowName, elementToHideName)
{
    // globalToolBoxItemsBagElementName and globalToolBoxCandidateItemsBagElementName required
    if (globalToolBoxItemsBagElementName == null || globalToolBoxCandidateItemsBagElementName == null)
    {
        return;
    }

    // if no candidates in hidden field then no action
    var globalToolBoxCandidateItemsBagElement = window.document.getElementById(globalToolBoxCandidateItemsBagElementName);
    if (globalToolBoxCandidateItemsBagElement.Value == "")
    {
        return;
    }

    var elementToShow = window.document.getElementById(elementToShowName);
    var elementToHide = window.document.getElementById(elementToHideName);

    var checkboxes = globalToolBoxCandidateItemsBagElement.value.replace(new RegExp("::", "g"), ",").replace(new RegExp(":", "g"), "").split(",");

    if (action == "check")
    {
        for (i = 0; i < checkboxes.length; i++)
        {
            var cbIdPair = checkboxes[i].split("/");    // [0] - checkbox element id, [1] - corresponding publication id
            var checkbox = window.document.getElementById(cbIdPair[0]);
            checkbox.checked = true;
            toolBoxMarkState(checkbox, ":" + cbIdPair[1] + ":", globalToolBoxItemsBagElementName);
        }
    }
    else if (action == "uncheck")
    {
        for (i = 0; i < checkboxes.length; i++)
        {
            var cbIdPair = checkboxes[i].split("/");    // [0] - checkbox element id, [1] - corresponding publication id
            var checkbox = window.document.getElementById(cbIdPair[0]);
            checkbox.checked = false;
            toolBoxMarkState(checkbox, ":" + cbIdPair[1] + ":", globalToolBoxItemsBagElementName);
        }
    }

    // toggle buttons visibility
    elementToShow.style.display = "";
    elementToHide.style.display = "none";
}

function biziSearch(inputTextElementName) {
    var url = "http://www.bizi.si/bannersearch.aspx?tis=" + document.getElementById(inputTextElementName).value;
    var newWindow = window.open(url, 'bizi');
    newWindow.focus();
}

/* needed for fckeditor*/
function FCKUpdateLinkedField(id)
{
    try
    {
        if (typeof (FCKeditorAPI) == "object")
        {
            FCKeditorAPI.GetInstance(id).UpdateLinkedField();
        }
    }
    catch (err)
    {
    }
}

/* ActivitiesListingLightBox */
function selectActivities(letterIdsArray, selectedLetter) {
    for (i = 0; i < letterIdsArray.length; i++) {
        var letterElement = window.document.getElementById(letterIdsArray[i]);
        var activitiesElement = window.document.getElementById(letterIdsArray[i] + "_activities");

        letterElement.className = (letterIdsArray[i] == selectedLetter ? "sel" : "");
        activitiesElement.style.display = (letterIdsArray[i] == selectedLetter ? "block" : "none");
    }
}

function pickActivity(destinationTextFieldId, text, id) {
    if (destinationTextFieldId != "") {
        window.document.getElementById(destinationTextFieldId).value = text;
    }
    subMenu(id, 'none');
}


/*********
news strip
*********/
var displayNews = 0;
var cycleNewsTimer = null;

function ShowText(buttonIndex)
{
	var textArray = new Array();
	
	for (i = 0; i < 10; i++)
	{
	    var id = "ctl00_TopLeftTop_NewsStrip1_RepeaterNews_ctl0" + i + "_text";
	    var obj = document.getElementById(id);
	    if (obj)
	        textArray.push(obj);
	    else
	        break;
	}

	for (var i = 0; i < textArray.length; i++)
	{
		textArray[i].style.display = "none";
    }

    textArray[buttonIndex].style.display = "block";
    
    if (cycleNewsTimer != null) {
        clearTimeout(cycleNewsTimer);
        displayNews = buttonIndex;
        cycleNewsTimer = setTimeout("CycleNews();", 30000);
    }
}

function CycleNews() {
    var textArray = new Array();

    for (i = 0; i < 10; i++) {
        var id = "ctl00_TopLeftTop_NewsStrip1_RepeaterNews_ctl0" + i + "_text";
        var obj = document.getElementById(id);
        if (obj)
            textArray.push(obj);
        else
            break;
    }

    if (textArray.length == 0)
        return;

    for (var i = 0; i < textArray.length; i++) {
        textArray[i].style.display = (i == displayNews) ? "block" : "none";
    }

    displayNews = (displayNews >= textArray.length - 1) ? 0 : displayNews + 1;
    cycleNewsTimer = setTimeout("CycleNews();", 30000);
}
// news stip - end


/******************
news strip advanced
******************/
var displayNewsAdvanced = 0;
var cycleNewsAdvancedTimer = null;

function ShowNewsTextAdvanced(buttonIndex) {
    var items = new Array();

    for (i = 0; i < 10; i++) {
        var item = {
            text: document.getElementById("ctl00_TopLeftTop_NewsStripAdvanced1_RepeaterNews_ctl0" + i + "_divNewsText"),
            link: document.getElementById("ctl00_TopLeftTop_NewsStripAdvanced1_RepeaterNews_ctl0" + i + "_aNewsStaticLink"),
            button: document.getElementById("ctl00_TopLeftTop_NewsStripAdvanced1_ulButtons_" + i)
        };
        if (item.text != null) {   // all three item sub-types must coexist (text, image, button)
            items.push(item);
        } else {
            break;
        }
    }

    // hide & deselect
    for (i = 0; i < items.length; i++) {
        items[i].text.style.display = "none";
        items[i].link.style.display = "none";
        items[i].button.className = "";
    }

    // show & select
    items[buttonIndex].text.style.display = "block";
    items[buttonIndex].link.style.display = "block";
    items[buttonIndex].button.className = "sel";

    if (cycleNewsAdvancedTimer != null) {
        clearTimeout(cycleNewsAdvancedTimer);
        displayNewsAdvanced = buttonIndex;
        cycleNewsAdvancedTimer = setTimeout("CycleNewsAdvanced();", 30000);
    }
}

function CycleNewsAdvanced() {
    var items = new Array();

    for (i = 0; i < 10; i++) {
        var item = {
            text: document.getElementById("ctl00_TopLeftTop_NewsStripAdvanced1_RepeaterNews_ctl0" + i + "_divNewsText"),
            link: document.getElementById("ctl00_TopLeftTop_NewsStripAdvanced1_RepeaterNews_ctl0" + i + "_aNewsStaticLink"),
            button: document.getElementById("ctl00_TopLeftTop_NewsStripAdvanced1_ulButtons_" + i)
        };
        if (item.text != null) {   // all three item sub-types must coexist (text, image, button)
            items.push(item);
        } else {
            break;
        }
    }

    if (items.length == 0)
        return;

    // show / hide & select / deselect
    for (i = 0; i < items.length; i++) {
        if (i == displayNewsAdvanced) {
            items[i].text.style.display = "block";
            items[i].link.style.display = "block";
            items[i].button.className = "sel";
        } else {
            items[i].text.style.display = "none";
            items[i].link.style.display = "none";
            items[i].button.className = "";
        }
    }

    displayNewsAdvanced = (displayNewsAdvanced >= items.length - 1) ? 0 : displayNewsAdvanced + 1;
    cycleNewsAdvancedTimer = setTimeout("CycleNewsAdvanced();", 30000);
}
// news strip advanced - end


function openBanners() {
    window.open("AllBanners.aspx"/*"http://adserver.iprom.net/adserver/tisAllBanners.pl"*/, "Seznam_bannerjev", 'scrollbars=yes,toolbar=no,location=no,status=on,width=764,height=600');
}

function windowBackOrClose() {
    if (window.name == "mapprint") {
        window.close();
    } else {
        window.history.back();
    }
}

function shouldPublicationPrint(publicationObject, should) {
    while (publicationObject.parentNode != null && publicationObject.className.indexOf("publication") == -1) {
        publicationObject = publicationObject.parentNode;
    }

    if (isUserAgent("MSIE")) {
        publicationObject.style.filter = "alpha(opacity=" + ((should) ? "100" : "40") + ")";
    } else {
        publicationObject.style.opacity = (should) ? "1" : "0.4";
    }

    publicationObject.className = (should) ? "publication" : "publication noprint";
}


/************************************
search results big items' commercials
************************************/
var currentCommercial;
var commercialArray;
var lastCommercialImgId;
var timerShow;
var timerHide;
var currTimeoutPID;
var delay;

function initCommercials() {
    currentCommercial = 0;
    commercialArray = null;
    lastCommercialImgId = "";
    timerShow = null;
    timerHide = null;
    currTimeoutPID = 0;
    delay = 100;
}

function showCommercials(bigItemId, oglasInteriorId, oglasImgId, logoNamesArray, timeoutPID) {
    
    if (lastCommercialImgId != oglasImgId) {
        lastCommercialImgId = oglasImgId;
        currentCommercial = 0;
        commercialArray = logoNamesArray;
    }

    if (timeoutPID == currTimeoutPID) {
        clearTimeout(timerHide);
    } else {
        currTimeoutPID = timeoutPID;
    }
    
    timerShow = setTimeout('showDelayCommercials(\'' + bigItemId + '\', \'' + oglasInteriorId + '\', \'' + oglasImgId + '\');', delay);
}

function hideCommercials(bigItemId, oglasInteriorId) {
    clearTimeout(timerShow);
    timerHide = setTimeout('hideDelayCommercials(\'' + bigItemId + '\', \'' + oglasInteriorId + '\');', delay + 100);
}

function showDelayCommercials(bigItemId, oglasInteriorId, oglasImgId) {
    document.getElementById(bigItemId).className = "resultBig resItem withAdd";
    document.getElementById(oglasInteriorId).style.display = "block";
    document.getElementById(oglasImgId).src = "logo/" + commercialArray[currentCommercial] + ".gif";
}

function hideDelayCommercials(bigItemId, oglasInteriorId) {
    document.getElementById(bigItemId).className = "resultBig resItem";
    document.getElementById(oglasInteriorId).style.display = "none";
}

function showNextCommercial() {
    currentCommercial++;
    if (currentCommercial >= commercialArray.length)
        currentCommercial = commercialArray.length - 1;
    document.getElementById(lastCommercialImgId).src = "logo/" + commercialArray[currentCommercial] + ".gif";
}

function showPreviousCommercial() {
    currentCommercial--;
    if (currentCommercial < 0)
        currentCommercial = 0;
    document.getElementById(lastCommercialImgId).src = "logo/" + commercialArray[currentCommercial] + ".gif";
}
// search results big items' commercials - end


/***********************************
Company Details "LeftColumn" aligner
***********************************/
function alignCompanyDetailsLeftColumn(leftColumnId) {
    var height = 0;
    var titleHolder = window.document.getElementById("ctl00_Middle_CompanyInfoMenu1_LabelTitle");
    
    if (isUserAgent("firefox")) {
        height = titleHolder.parentNode.parentNode.parentNode.clientHeight;
    } else {
        height = titleHolder.parentNode.parentNode.parentNode.offsetHeight
    }

    var leftColumn = window.document.getElementById(leftColumnId);
    leftColumn.style.top = (-height + 5) + "px";
    leftColumn.style.marginBottom = (-height + 5) + "px";
}
// Company Details "LeftColumn" aligner - end

//execute the click of the button with provided id
function virtualSubmit(clientButtonID) 
{
    var btn = document.getElementById(clientButtonID); 
    btn.click(); 
}
