var min=10;
var max=16;

function increaseFontSize()
{
	increase('p');
	increase('span');
	increase('div');
	increase('a');
}
function decreaseFontSize()
{
	decrease('p');
	decrease('span');
	decrease('div');
	decrease('a');
}

function increase(tagname)
{
   var item = document.getElementsByTagName(tagname);
   for(i=0;i<item.length;i++) {
   	if(item[i].className != "menulink")
   	{
      if(item[i].style.fontSize) {
         var s = parseInt(item[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 2;
      }
      item[i].style.fontSize = s+"px";
    }
   }
}
function decrease(tagname)
{
   var item = document.getElementsByTagName(tagname);
   for(i=0;i<item.length;i++) {
   	if(item[i].className != "menulink")
   	{
      if(item[i].style.fontSize) {
         var s = parseInt(item[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 2;
      }
      item[i].style.fontSize = s+"px";
     }
   }
}

$(document).ready(function()
{
/*
    $('#help a').fancybox({
                         //'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders
                         });

    $('#AdditionalInfo').fancybox({
                         //'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders
                         });
    $('#Archived_Product').fancybox({
                         //'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders
                         });
	$('#Tuotetietokansio_box').fancybox({
                         //'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders,
                         'autoDimensions' : false,
                         'autoScale' : false,
                         'width' : 425,
                         'height' : 355,
                         'overlayOpacity' : 0.3
                         });
    $('#Tuotetietokansio').fancybox({
                         //'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders
                         });

    $("a.fancyimage").fancybox({
                         //'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders
                         });
	$("a#tukku_info").fancybox({
                         //'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders
                         });
	$("a#productSheetHelpIcon").fancybox({
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders,
                         'autoDimensions' : false,
                         'autoScale' : false,
                         'width' : 500,
                         'height' : 500
                         });
	$("a#FolderEditInfo").fancybox({
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders
                         });
	$("a#ArchivedProductEditInfo").fancybox({
                         //'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders
                         });
*/
    $('#help a, #AdditionalInfo, #Archived_Product, #Tuotetietokansio_box, #Itsepalvelu_box, #Advanced_Search, #Tuotetietokansio, a.fancyimage, a#tukku_info, a#FolderEditInfo, a#ArchivedProductEditInfo').fancybox({
                         'zoomSpeedIn': 300,
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders,
                         'autoDimensions' : false,
                         'autoScale' : false,
                         'width' : 425,
                         'height' : 355,
                         'overlayOpacity' : 0.3
                         });

    $("a#productSheetHelpIcon, a#Tuotetietokansio_ohjeet").fancybox({
                         'zoomSpeedOut': 300,
                         'overlayShow': true,
                         'hideOnContentClick': false,
                         'callbackOnClose': checkTableHeaders,
                         'autoDimensions' : false,
                         'autoScale' : false,
                         'width' : 500,
                         'height' : 500,
                         'overlayOpacity' : 0.3
                         });
                         
    $('#keyword').autofill("hakusana(t)");
    $('#electricalNumber').autofill("sähkönumero tai sen alku");

    $('#search-form').submit(function()
    {
        if($('#keyword').val() == "hakusana(t)")
        {
    	    $('#keyword').val("");
        }
    	if($('#electricalNumber').val() == "sähkönumero tai sen alku")
        {
      	    $('#electricalNumber').val("");
		}
    });
    $('#keyword').focus();
});

function checkTableHeaders()
{
    if ( $('#product-table-headers-controls').length > 0 )
    {
        setTableHeaders();
    }
}

function hide(label)
{
    var elem = $(label);
    if (elem != null) elem.addClass('hidden');
}

function show(label)
{
    elem = $(label);
    if ( elem != null )
    {
        elem.removeClass('hidden');
        elem.show();
    }
}

function disableEnterKey(e)
{
    var key;

    if(window.event) // IE
    {
        key = window.event.keyCode;
    }
    else // Moz
    {
        key = e.which;
    }
    return (key != 13);
}

function clearSearch()
{
    document.getElementById('keyword').value="";
    document.getElementById('electricalNumber').value="";
    document.getElementById('keyword').className = "";
    document.getElementById('electricalNumber').className = "";
}

function setBasketDisplay(itemCount)
{
    if ( itemCount == 0 )
    {
        $('#basket-cell').removeClass('basket-cell-has-content');
        $('#basket-cell').addClass('basket-cell-empty');
        $('#basket-content-info').html('<a href=\"' + systemUrl + 'tuotetietokansio/\">' + arrText['Product_Basket_Empty_Front_Page'] + '</a>');
    }
    else
    {
        $('#basket-cell').removeClass('basket-cell-empty');
        $('#basket-cell').addClass('basket-cell-has-content');
        var infoText = new String(arrText['Product_Basket_Info_Front_Page']);
        infoText = '<a href=\"' + systemUrl + 'tuotetietokansio/\">' +infoText.replace('%d', itemCount+'') + '</a>';
        infoText += ' <a href="#" onClick="setBasketEmpty(); return false;"><img src="/images/icons/png/trashbin.png" id="basketEmpty" alt="' + arrText['Product_Basket_Do_Empty_Front_Page'] + '" title="' + arrText['Product_Basket_Do_Empty_Front_Page'] + '"></a>';
        $('#basket-content-info').html(infoText);
    }
}

function setBasketEmpty()
{
    var url = new String(window.location);
    if ( url.indexOf('tuotetietokansio') != -1 )
    {
        window.location = systemUrl + 'tuotetietokansio/?Action=clear';
    }
    // Search / Product sheet
    else if ( $('#product-table').length > 0 || $('#similar-product-table').length > 0 )
    {
        removeFromBasket(-1);
    }
    // Other views
    else
    {
        $.ajax({
            type: "POST",
            data: "Module=Product&File=basket&Action=clearAjax",
            url: ajaxUrl,
            async: true,
            success: function(response)
            {
                setBasketDisplay(0);
            }
        });
    }
}

jQuery.fn.autofill = function (v) {

	if($(this).val() == "")
	{
		$(this).val(v);
		$(this).addClass("prefilledInput");
	}

	$(this).focus(function()
	{
		if($(this).val() == v)
		{
			$(this).val("");
			$(this).removeClass("prefilledInput");
		}
	});

	$(this).blur(function()
	{
		if($(this).val() == "")
		{
			$(this).val(v);
			$(this).addClass("prefilledInput");
		}
	});

}
