// we need to preload images:
function doPreloadImages() {
    var preload = new Array();
    $(".hover").each(function() {
        s = $(this).attr("src").replace(/\.(.+)$/i, "_on.$1");
        preload.push(s)
    });
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
}

// we need to create hover where possible:
function doApplyHovers() {
    $(".hover").each(function() {
        if ($(this).attr("src").match(/_on\.(.+)$/i)) {
            $(this).removeClass("hover");
        }
    });
    $(".hover").hover(function() {
        s = $(this).attr("src").replace(/\.(.+)$/i, "_on.$1");
        $(this).attr("src", s);
    }, function() {
        s = $(this).attr("src").replace(/_on\.(.+)$/i, ".$1");
        $(this).attr("src", s);
    });
}

//to show popup windows:
function openPopup(url, width, height) {
    var wx = (screen.availWidth - width) / 2;
    var wy = (screen.availHeight - height) / 2;
    window.open(
        url, // + '&popup=1&print=1',
        'popup',
        'width=' + width + ', height=' + height +
        ', status=no, title=no, scrollbars=no, resizable=no, left=' +
        wx + ', top=' + wy
    );
}

function toCurrency(value) {
    var value2 = new Number(value);
    return value2.toFixed(2);
}

$(window).bind("load", function() {
    $("td.left_menu ul li").bind("mouseover", function() {
        if (!$(this).hasClass("left_menu_current")) {
            $(this).css("background-color", "#c0cd35");
        }
    });
    $("td.left_menu ul li").bind("mouseout", function() {
        if (!$(this).hasClass("left_menu_current")) {
        $(this).css("background-color", "#a0a0a0");
        }
    });
});

function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  var theForm = $('#' + form);
  $(':input', theForm).each(function() {
  var type = this.type;
  var tag = this.tagName.toLowerCase(); // normalize case
  // it's ok to reset the value attr of text inputs,
  // password inputs, and textareas
  if (type == 'text' || type == 'password' || tag == 'textarea')
    this.value = "";
    // checkboxes and radios need to have their checked state cleared
    // but should *not* have their 'value' changed
  else if (type == 'checkbox' || type == 'radio')
    this.checked = false;
    // select elements need to have their 'selectedIndex' property set to -1
    // (this works for both single and multiple select elements)
  else if (tag == 'select')
    this.selectedIndex = 0;
  });
};


function submitForm(form) {
  $('#' + form).trigger("submit");
};

function checkoutForm(form) {
  $("#sc_checkout").attr('value', '1');
  submitForm(form);
};


function appParseFormToParams(form) {
    var k = '';

    $(':input', form).each(function() {
        var type = this.type;
        var tag  = this.tagName.toLowerCase(); // normalize case
        if (type == 'hidden' || type == 'text' || type == 'password' || tag == 'textarea') {
            if (this.value) {
                k += '&' + this.name + '=' + encodeURIComponent(this.value);
            }
        } else if (type == 'checkbox' || type == 'radio') {
            if (this.checked) {
                k += '&' + this.name + '=' + encodeURIComponent(this.value);
            }
        } else if (tag == 'select') {
            if (this.options[this.selectedIndex].value) {
                k += '&' + this.name + '=' + encodeURIComponent(this.options[this.selectedIndex].value);
            }
        }
    });
    return k;
}

function doUpdateShoppingCart(formID, statusAreaID) {
    var form = $("#" + formID).get(0);

    var statusArea = $("span#" + statusAreaID);
    var params = appParseFormToParams(form);

    $.ajax({
        type: "POST",
        data: params,
        error: function(xml){
            statusArea.addClass('message_error').html("Network error, please try again later.");
        },
        success: function(xml){
            statusArea.addClass('message_ok').html("Added.");
        }
    });
}

function appShowFaqAnswer(i) {
    $("div.faq_answer").hide();
    if (i >= 0) {
        $("div.faq_answer:eq(" + i + ")").show();
    }
}

// init functions on page ready:
$(document).ready(function() {
    doApplyHovers();
});

var windowLoadHandler = function() {
    $.ajaxSetup({
        url:  'index.php'
    });
//    jsHover();
//    $('a[rel*=facebox]').facebox();
//    doPreloadImages()
}

$(window).bind("load", windowLoadHandler);



