/*
 * Tintencenter Javascript
 */

function tcResize() 
{
    var h = $(window).height();
    var w = $(window).width();
		var ww, wl;

		if (w < 980)
		{
			ww=960;
			wl=10;
		}
		else if (w > 1024)
		{
			ww=1004;
			wl=parseInt((w-ww)/2);
		}
		else 
		{
			ww=w-20;
			wl=10;
		}

		$('#head_search').css('width', ww+'px');
		if (navigator.appVersion.indexOf('Win') != -1)
		{
			$('#head_search').css('left', (wl+1)+'px');
		}
		else
		{
			$('#head_search').css('left', wl+'px');
		}
		$('#wrapper').css('width', ww+'px');
		$('#content').css('width', (ww-416)+'px');

		/*
		$.cookie('_WWPX', ww, { expires: null, path: '/', domain: 'tintenpatrone.com', secure: false });		
		$.cookie('_WLPX', wl, { expires: null, path: '/', domain: 'tintenpatrone.com', secure: false });
		*/

		$.cookie('_WWPX', ww, { expires: null, path: '/', domain: 'tp-new.test', secure: false });		
		$.cookie('_WLPX', wl, { expires: null, path: '/', domain: 'tp-new.test', secure: false });
		wl=parseInt( (ww-416) / 182);

		$('ul.gList').each(function() {
				if (!$(this).attr('nocheck'))
				{
					var ww = parseInt($('#content').css('width'));
					var gl = parseInt( ww / $(this).attr('gWidth') );

					if (gl > $(this).attr('gMax')) gl=$(this).attr('gMax');

					if ($(this).attr('ccols') != gl)
					{
						$(this).attr('ccols', gl);
						$(this).children('li').css('width', parseFloat( ($(this).attr('gSize') - $(this).attr('gPad')) / gl )+'%');

						if ($(this).attr('gLines'))
						{
							gl=(gl* parseInt($(this).attr('gLines')))-1;
							$(this).children('li').css('display', '');
							$(this).children('li:gt('+gl+')').css('display', 'none');
						}
					}

				}
			});
}

function tcScroll()
{
	var t = $(window).scrollTop();
	var o = $('#head_top').outerHeight();

	if ( (t > o+1) && !($('#head_search').hasClass('search_toppos')) ) 
	{
		$('#head_search').addClass('search_toppos');
		$('#head_top').css('margin-top', '29px');
	}
	else if ( (t <= o+1) && $('#head_search').hasClass('search_toppos')) 
	{
		$('#head_search').removeClass('search_toppos');
		$('#head_top').css('margin-top', '0px');
	}
}

$(document).ready(function() {
		
		if ($('#txtSearch').length > 0)
		{
			$('#txtSearch').autocomplete({
					source: '/catalog/n_search.php',
					minLength: 3,
					select: function( event, ui ) {
						if (ui.item.link) window.location.href=ui.item.link;
					}

				}).data("autocomplete")._renderItem = function (ul, item) {
					$(ul).toggleClass('uiac');
					ul.css('z-index', 10);
					if (item.link)
					{
						return $('<li class="uiac"></li>').data('item.autocomplete', item).append('<a href="'+item.link+'"><span class="link">' + item.name + '</span><span class="dfr price"><span>'+item.price + '</span> &euro;</span><br class="brclear"></a>').appendTo( ul );
					}
					else
					{
						return $('<li class="uiac"></li>').data('item.autocomplete', item).append('<div class="info">'+item.info+'</div>').appendTo( ul );
					}
				};
		}

			$('.dotip').each(function() {
					$(this).qtip({
					content: $('#'+$(this).attr('from')).html(), position: { corner: { target: $(this).attr('qtarget'), tooltip: $(this).attr('tooltip') } }, style: { name: $(this).attr('sname'), tip: { corner: $(this).attr('corner') }, width: { min: $(this).attr('wmin'), max: $(this).attr('wmax') } },
						show: { delay: 0, effect: { type: 'fade', length: 0 } }, hide: { delay: $(this).attr('hdelay'), fixed: true } });
				});

		tcResize();
//		jQuery.event.add(window, "load", tcResize);
		jQuery.event.add(window, "resize", tcResize);
		jQuery.event.add(window, "scroll", tcScroll);

	  $("#mainmenu li").hover(function() { $(this).addClass("iehover"); }, function() { $(this).removeClass("iehover"); });

		$('#teaser').cycle({fx: 'scrollUp', timeout: 5000, delay: -2500, pause: true});
		$('.gscrollUp').cycle({fx: 'scrollUp', timeout: 5000, delay: -2500, pause: true});
		$('#testWinner').cycle({fx: 'fade', timeout: 5000, delay: -2500, pause: true, height: 170, fit: 1});
//		$('.gOverlay').transify({opacityOrig:.8,opacityNew:.95,fadeSpeed:25});
//
	$('ul#mainmenu li.sub').click(function() { $(this).parent('ul').hide(); });



		$('.gTOFlow').each(function() {
				var t, ch;

				if (parseInt($(this).css('max-height')) > 0)
					ch=parseInt($(this).css('max-height'));
				else
					ch=parseInt($(this).css('height'));

				if ($(this).children('a').first().height() > ch)
				{
					t=$(this).children('a').first().html();
					while(t.length > 0 && $(this).children('a').first().height() > ch)
					{
						t = t.substr(0, t.length - 1);
						while (t.substr(t.length -1, 1) == ' ') t=t.substr(0, t.length-1);
						$(this).children('a').first().html(t+'...');
					}
				}
			});





	});


