Tooltip = {
    _isEnabled: function() {
        return $.cookie('show_help');
    },

    _updateSwitch: function() {
        if (this._isEnabled())
            this._switch.addClass('on').text('wyłącz');
        else
            this._switch.removeClass('on').text('włącz');
    },

    _activateSwitch: function() {
        this._switch = $('#tooltip-toggle a');

        this._switch.click(function() {
            if (Tooltip._isEnabled())
                $.cookie('show_help', null);
            else
                $.cookie('show_help', '1', { expires: 7 });

            Tooltip._updateSwitch();
        });

        this._updateSwitch();
    },

    _readLists: function() {
        this._list = new Object;

        $('#tooltip-list li, #fixed-tooltip-list li').each(function() {
            var fixed = false

            if ($(this).parent().get(0).id == 'fixed-tooltip-list')
                fixed = true;

            Tooltip._list[$(this).attr('id').replace('-tt', '')] = fixed
        });

        this._queryString = '';

        for (id in this._list)
            Tooltip._queryString += '#' + id + ', ';
    },

    _activeOn: function(element) {
        var id = $(element).attr('id');

        if (this._list[id] == true || (this._list[id] == false && this._isEnabled()))
            return true

        return false;
    },

    _fetch: function(element) {
        return $('#' + element.id + '-tt');
    },

    _activateTriggers: function() {
        $(this._queryString).hover(
            function() {
                if (Tooltip._activeOn(this))
                    Tooltip._fetch(this).fadeIn(300);
            },

            function() {
                if (Tooltip._activeOn(this))
                    Tooltip._fetch(this).fadeOut(200);
            }
        ).mousemove(function(event) {
            var tip = Tooltip._fetch(this);

            if (tip)
                tip.css({top: event.pageY + 14, left: event.pageX + 14});
        });
    },

    init: function() {
        this._activateSwitch();
        this._readLists();
        this._activateTriggers();
    }
}

$(document).ready(
	function() {
		$('span.tooltip2').cluetip({splitTitle: '|', arrows: true, dropShadow: false, cluetipClass: 'jtip'});
		$('div.tooltip2').cluetip({splitTitle: '|', arrows: true, dropShadow: false, cluetipClass: 'jtip'});
		$.alerts.dialogClass = 'style_1';

    $('#showmeResults').hover(function() {
      window.hideResults = false;
    }, function() {
        var autocomplete = $(this);
        window.hideResults = true;
        setTimeout(function() {
          if (window.hideResults === true) {
            autocomplete.hide();
          }
        }, 600);
    });

		$("#cat_expand1").click(
			function()
			{
				$('#category-browser-collection li.rootCat').toggle();
				$('#cat_collapse1').toggle();
				$('#cat_collapse2').toggle();
				$('#cat_expand1').toggle();
				$('#cat_expand2').toggle();
			});

		$("#cat_expand2").click(
			function()
			{
				$('#category-browser-collection li.rootCat').toggle();
				$('#cat_collapse1').toggle();
				$('#cat_collapse2').toggle();
				$('#cat_expand1').toggle();
				$('#cat_expand2').toggle();
			});

		$("#cat_collapse1").click(
			function()
			{
				$('#category-browser-collection li.rootCat').toggle();
				$('#cat_collapse1').toggle();
				$('#cat_collapse2').toggle();
				$('#cat_expand1').toggle();
				$('#cat_expand2').toggle();
			});

		$("#cat_collapse2").click(
			function()
			{
				$('#category-browser-collection li.rootCat').toggle();
				$('#cat_collapse1').toggle();
				$('#cat_collapse2').toggle();
				$('#cat_expand1').toggle();
				$('#cat_expand2').toggle();
			});

			 $("ul.sf-menu").supersubs({
            minWidth:    32,   // minimum width of sub-menus in em units
            maxWidth:    37,   // maximum width of sub-menus in em units
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over
                               // due to slight rounding differences and font-family
        }).superfish();

        Tooltip.init();

        state = $('#cmp-toggle').find('span');
        if($.cookie('comparer') == 'none')
        {
        	$('#cmp-content').hide();
        	$('#cmp-buttons').hide();
        	state.html('Zobacz porównanie &darr;');
        }
        else
        {
        	state.html('Schowaj porównanie &uarr;');
        	$('#cmp-content').show();
        	$('#cmp-buttons').show();
        }
	}
);

function dodajPorownanie(checkbox, id)
{
	if(checkbox.checked == true)
	{
		xajax_dodajPorownanie(id);
		return false;
	}
	else
	{
		xajax_usunPorownanie(id);
		return false;
	}
}

function dodajPorownanieProdukt(id)
{
	xajax_dodajPorownanie(id);

	return false;
}

function usunPorownanieLista(id)
{
	xajax_usunPorownanie(id);
	return false;
}

function maxComparisions()
{
	jAlert('Możesz maksymalnie porównywać 6 produktów na raz.', 'Uwaga');
}

function minComparisions()
{
	jAlert('Musisz mieć przynajmniej 2 produkty w porównywarce.', 'Uwaga');
}

function productExists()
{
	jAlert('Ten produkt znajduje się już w Twoim koszyku', 'Uwaga');
}

function setExists()
{
	jAlert('Ten zestaw znajduje się już w Twoim koszyku', 'Uwaga');
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function jumpTo(addr)
{
	document.location.href = addr;
}

function setError( elementID )
{
	xajax.$(elementID).style.border='1px solid red';
	xajax.$(elementID).style.backgroundColor='#FF6F75';
}
function unsetError( elementID )
{
	xajax.$(elementID).style.border='1px solid #b3bfb2';
	xajax.$(elementID).style.backgroundColor='#eee';
}
