/*************************************
General
*************************************/

//show/hide sidebar menu elements /* TODO - jquery*/
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

function hideShow(element)
{
	var el = getNextSibling(element);
	
	//hide <ul> block, change background o <a> block	
	switch(el.style.display)
	{
		case "none":
		{
			el.style.display = "block";
			element.style.backgroundImage = 'url("images/bg_menulink_active.png")';
			break;
		}
		case "block":
		{
			el.style.display = "none";
			element.style.backgroundImage = 'url("images/bg_menulink_inactive.png")';
			break;
		}
		default:
		{
			el.style.display = "block";
			element.style.backgroundImage = 'url("images/bg_menulink_active.png")';
		}
	}
}

function sidebarMenuHandler()
{
	/*$("#sidebarMenu li a").click(function(event) {
		//var sibling = $($(this)" ~ ul");
		alert(sibling);
	});*/
}

//fix for the Firefox DOM (ignore whitespace)
function getNextSibling(startBrother){
  endBrother = startBrother.nextSibling;

  while(endBrother.nodeType != 1)
  {
    endBrother = endBrother.nextSibling;
  }
  
  return endBrother;
}

/* text handling in forms */

var searchDef = "[ Search for products ]";
var newsletterDef = "[ Your email ]";

//set default form text
function formTextHandler()
{
	//set default text for inputs
	$(".txtSearch").val(searchDef);
	$(".txtNewsletter").val(newsletterDef);
	
	//remove the text when clicked
	$(".txtSearch, .txtNewsletter").click(function(event) {
		if(($(this).val() == searchDef) || ($(this).val() == newsletterDef))
		{
			$(this).val("");
		}
	});
	
	//when focus is lost
	$(".txtSearch, .txtNewsletter").blur(inputBlurHandler);
}

//restore default text on blur if field is empty
function inputBlurHandler()
{
	if($(this).val() == "")
	{
		formTextHandler();
	}
}

//set a height equal to b`s
function equalHeights(a, b)
{
	$(a).height($(b).height());
}

/*************************************
Product description page 
*************************************/

function add1(element)
{
    input = element.parentNode.getElementsByTagName("input");
    item1 = input[0].value;
    item1 = parseInt(item1) + 1;
    input[0].value = item1;

}

function substract1(element)
{
    input = element.parentNode.getElementsByTagName("input");
    item1 = input[0].value;
    if ( parseInt(item1) > 1 )  {
    item1 = parseInt(item1) - 1;
    input[0].value = item1;
    }
}

function getProductQuantity()
{
	var quantity = parseInt($(".quantity").val());
	if(!isNaN(quantity))
	{
		return quantity;
	} else {
		return 0;
	}		
}

/*************************************
Login page
*************************************/

/* reset password reminder forms */
function resetReminderForms()
{
    $("#email1").clearForm();
    $(".captchaInput").clearForm();
}

/* custom jQuery function for clearing form elements */
$.fn.clearForm = function() {
    return this.each(function() {
      var type = this.type, tag = this.tagName.toLowerCase();
      if (tag == 'form')
        return $(':input',this).clearForm();
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
      else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
      else if (tag == 'select')
        this.selectedIndex = -1;
    });
};

/*************************************
Resize hMenu if there are less than 6 tabs
*************************************/

function menuWidthHandler()
{
    var linkCount = $("#hMenu > li").size()
    if (linkCount < 6)
    {
        $("#hMenu > li").css("width", (100/linkCount*0.985)+"%");
    }
}

/*************************************
Init
*************************************/

var Main = {
    init: function () {
        var noWhitespace = /^\s*$/

        //equalHeights(".pageContent", "#sidebar")
        //$(".pageContent").height($("#sidebar").height() - 56);
        //$(':min-height')
        if ($("#sidebar").height() > 56) {
            $(".pageContent").css({ 'min-height': $("#sidebar").height() - 56 });
            $(".pageContent_page").css({ 'min-height': $("#sidebar").height() - 56 });
        }

        formTextHandler();
        sidebarMenuHandler();

        //login page
        /*if ($(".validNoteTop").text() == "") { $(".validNoteTop").css("display", "none"); }
        if ($(".validNote").text() == "") { $(".validNote").css("display", "none"); }*/

        if (noWhitespace.test($(".validNoteTop").text())) {
            $(".validNoteTop").css("display", "none");
        }

        if (noWhitespace.test($(".validNote").text())) {
            $(".validNote").css("display", "none");
        }

        //jqzoom (product description page
        var options = {
            zoomWidth: 402,
            zoomHeight: 402,
            yOffset: -1,
            showEffect: 'show',
            hideEffect: 'fadeout',
            fadeoutSpeed: 'slow',
            title: false
        }
        $('a.zoom').jqzoom(options);
        $("#itemImages a.Thmb_image").hover(function () {
            $("#item_image a.zoom").css("visibility", "hidden");
            $("#item_image a." + $(this).attr("id")).css("visibility", "visible");
        });

        //login popup
        $(".remindPasswordPopup").click(function (event) {
            $(".passwordReminderBox").css("display", "block");
            resetReminderForms();
        });
        $(".passwordReminderBox .close").click(function (event) {
            $(".passwordReminderBox").css("display", "none");
        });

        menuWidthHandler();
    }
}

//init
$(document).ready(Main.init);

