/* NOTE: this file is used only for portal functionalities */

/**
 * external_links()
 * ---------------
 * This functions iteraters through links that are external (include the rel="external" tag)
 * and inserts atribute target="_blank" to make them open in new window.
 * Real reason? To keep our XHTML valid.
 *
 * @author     Miha Hribar <miha@hribar.info
 */
function external_links() {
    $("a[rel=external]").attr({ target: "_blank" });
}

jQuery.fn.loading = function () {
    var div = jQuery(this);
    div.attr({ innerHTML: '<div class="loading"></div>' });
    return(this);
}

jQuery.fn.sortOptions = function(ascending)
{
    var a = typeof(ascending) == "undefined" ? true : !!ascending;
    this.each(
        function()
        {
            if(this.nodeName.toLowerCase() != "select") return;
            // get options
            var o = this.options;
            // get number of options
            var oL = o.length;
            // create an array for sorting
            var sA = [];
            // loop through options, adding to sort array
            for(var i = 0; i<oL; i++)
            {
                sA[i] = {
                    v: o[i].value,
                    t: o[i].text
                }
            }
            // sort items in array
            sA.sort(
                function(o1, o2)
                {
                    // option text is made lowercase for case insensitive sorting
                    o1t = o1.t.toLowerCase(), o2t = o2.t.toLowerCase();
                    // if options are the same, no sorting is needed
                    if(o1t == o2t) return 0;
                    if(a)
                    {
                        return o1t < o2t ? -1 : 1;
                    }
                    else
                    {
                        return o1t > o2t ? -1 : 1;
                    }
                }
            );
            // change the options to match the sort array
            for(var i = 0; i<oL; i++)
            {
                o[i].text = sA[i].t;
                o[i].value = sA[i].v;
            }
        }
    );
    return this;
};

function popUp(URL, width, height) {
    day = new Date();
    id = day.getTime();
    leftVal = (screen.width - width)/2;
    topVal = (screen.height - height)/2;
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+ width +",height="+ height +", left="+ leftVal +", top="+ topVal +"');");
}

function inputFocus(id) {
    $('#'+id).focus();
}

function confirmLocation(string, url) {
        if (confirm(string)) {
                window.location.href = url;
                return true;
        }
        else {
            return false;
        }
}

function submitForm(f) {
    if (!document.getElementById) {
        return;
    }

    var fo = document.getElementById(f);
    fo.submit();
}

function banner(cSRC, cURL, nType, cTarget, width, height) {
    if (cTarget != '') {
        urlTarget = cTarget;
    }
    if (nType == 2) {
        document.getElementById('flash_banner').innerHTML = '<a href="'+ cURL +'" onclick="this.target=\'' + urlTarget + '\'"><img src="'+ cSRC +'" alt="" style="border:none;" /></a>';
    } else {
        var fo = new FlashObject(cSRC + '?clickthru=' + cURL, "banner", width, height, "6", "#ffffff");
        fo.write("flash_banner");
    }
}

// shopping process; on click we disable all set of buttons
function btnNav() {
    var buttons = Array('prev', 'clear', 'next')
    for(var i=0; i<buttons.length; i++) {
        document.getElementById(buttons[i]).disabled = true;
    }
}

function nav() {
    $('#main_nav li').hover(function(){
        // over
        $(this).addClass('hover');
    }, function(){
        // out
        $(this).removeClass('hover');
    });
}

function is_numeric(val) {
     if (isNaN(parseFloat(val))) {
          return false;
     }
     return true
}

function addValue(cId,cValue)
{
    document.getElementById(cId).value = cValue;
}

/* collapse/expand clicked additional service form */
function slideToggleAdditionalServices()
{
    $('#additional_services h3').click(
        function(){
                        $(this).parent().children(':not(h3)')
                                .slideToggle("fast");
                        $(this).parent()
                                .toggleClass("collapsed");
        }
    );
}

/* Fix hover states for IE6 */
function language_switch()
{
    $('#language_switch li:first-child').css('display', 'block');
    $('#language_switch').hover(
        function(){
            $(this).find('li').css('display', 'block');
        },
        function(){
            $(this).find('li:not(:first-child)').css('display', 'none');
        }
    );
}

/**
 * showHideImages
 *
 * @description Show hide images with a checkbox
 */
function showHideFieldset(fieldset_name) {
	if($("dd fieldset." + fieldset_name).hasClass('hide-me')) {
		$("dd fieldset." + fieldset_name).removeClass('hide-me');
	} else {
		$("dd fieldset." + fieldset_name).addClass('hide-me');
	}
}

/**
 * hideEmptyCells
 *
 * @description Looks for empty dt cells and dd cells with hidden fieldset and completely hide them
 */
function hideEmptyCells() {
	
	// Finds all dd cells with class "hide-me" on fieldset/input and completely hide them along with theirs dt pair
	$("dl.zend_form dd .hide-me").each(function() {
		if (!$(this).hasClass('hide-toggle')){
			$(this).parent().addClass('hide-me');
			$("#" + $(this).parent().attr('id').replace("-element", "-label")).addClass('hide-me');
		}
	});
	
	// Finds all empty dt cells and add class "hide-me"
	$("dl.zend_form dt").each(function() {
		if ($.trim($(this).text()) == ""){$(this).addClass('hide-me');}
	});
}

/* on document ready do the following */
$(document).ready(function(){

    language_switch();
    external_links();
    slideToggleAdditionalServices();
    var path = document.location.pathname;
    var pos = path.indexOf("/");
    pos = path.indexOf("/", pos + 1);
    pos = path.indexOf("/", pos + 1);
    path = path.substring(0,pos+1)/*+"search.html";*/;

    /* add autocomplete to quicksearch *//* add autocomplete to quicksearch */
    $("#search_string").autocomplete(path, { minChars:2, maxItemsToShow:7 });
    nav();
    /* close all additional service forms on load */
        if (!$('#additional_services form').hasClass('opened')) {
            $('#additional_services form').addClass('collapsed');
            $('#additional_services form').children(':not(h3)').css('display', 'none');
        }
	
	hideEmptyCells();

});

