/* ESTOLICKY.JS
 * Author: A.Kiss, Monumental (kiss@monumental.sk)
 */

/* DEBUG */
var debug = false;
var debugTime = '';
function whatTime (){ 
	var d = new Date();
	dt = setTimeout(function(){debugTime = whatTime();},500);
	return(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()); }
var dt = setTimeout(function(){debugTime = whatTime();},500);
function debugLog (m) { if(debug) console.log('['+debugTime+'] '+m); }

$(function(){
	/*
	 * ROTATOR PART
	*/
	var shown = 1;
	var speed = 500;
	var duration = 8000;
	// END OF SETTINGS
	var r = $('#rotator');
	if (r.size()>0){
		var rc = r.find('.control');
			rc.css({display:'block'})
			  .find('li').addClass('pagenumber');
		var rs = r.find('.slide').css({display:'block'});
		var max = rs.size();
						
		r.find('.slide:not(:eq('+(shown-1)+'))').css({opacity:'0'});
		rc.find('li:eq('+(shown-1)+')').addClass('active');
		
		function rotateTo(which, calledByTimer){
			if (which == shown) return;
			if (which > max) which = 1;

			debugLog('tick: '+shown+' -> '+which);
			
			var h = rs.eq(shown-1);
			var s = rs.eq(which-1);
			
			rc.find('li').eq(shown-1).removeClass('active');
			rc.find('li').eq(which-1).addClass('active');

			shown = which;
			
			h.animate({opacity: '0'},speed);
			s.animate({opacity: '1'},speed*2).addClass('active');
			
			clearTimeout(t); t = setTimeout(function(){ rotateTo(shown+1,1); }, duration);
		}
		
		rc.find('li').click(function(){
			rotateTo ($(this).text());
		});
		
		var t = setTimeout(function(){ rotateTo(shown+1,1); }, duration);
	}
	/* 
	 * OTHER
	 */
	//IE6 hover
	$('#subnavigation li').hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover'); }
	);
	
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
	
	if (jQuery.browser.msie && (ie55 || ie6)) {
		$("img[src$=png]").each ( function() { DD_belatedPNG.fixPng( this ); });
		DD_belatedPNG.fix('#search');
	}
	
	$('#search input[type=text]').focus(function(){
		if ($(this).val() == 'hľadaný výraz') {
			$(this)
				.val('')
				.addClass('active');
		}
	});
	
	$('#search input[type=text]').blur(function(){
		if ($(this).val() == '') {
			$(this)
				.val("hľadaný výraz")
				.removeClass('active');
		}
	});
	
	$('#fb-link').click(function(){
		$('#fb-box').animate({
			marginLeft: '0px'
		}, 450);
		return false;
	}, function(){
		$('#fb-box').animate({
			marginLeft: '-305px'
		}, 450);
	});
	
	
	
	/* FADOVANIE PRODUKTOV
	var h; var previous=null;
	function changeAll (opacity,tonull) { $('ul.products li').stop().animate({opacity:opacity},250); if (tonull) {previous = null;} }
	$('ul.products li').hover(function(){
		clearTimeout(h);
		if (previous!=null){ previous.stop().animate({opacity:'.6'}); }
		else { changeAll(.6,false); }
		previous = $(this);
		previous.stop().animate({opacity:'1'});
	},function(){
		h = setTimeout (function(){changeAll('1',true)},100);
	});*/
});
