var autoslide_curr = 1;
var current_slide = 1;
var slide_time = 8000;
var slide_interval = false;


$(function() {
	//hide initialy all slides
	$("#fade .cont div").hide();
	
	//add click action
	$("#fade .nav a").click(function() {
		var mid = $(this).attr("href").substr(1);
		var num = mid.substr(1);
		go_fade(num);
		start_autofade(); //start fading all over
		return false;
	});
	
	//show first slide
	go_fade(1);
	
	//start autofading
	start_autofade();
	
	//add hover events to product navi
	$("ul.prod li").hover(
		 function() { $(this).addClass("hover") }
		,function() { $(this).removeClass("hover") }
	);
	
	//png fix
	//$("ul.nav li").ifixpng();
	
	
	$("#sizes-prices li").each(function() {
		var len = $("#sizes-prices li").length;
		var price = $(this).find("em.price").text();
		price = price.trim();
		if( ! price ) {
			$(this).hide();
			len--;
		} //end if
		
		$("#sizes-prices").css("height", (len * 22) + "px");
	});
	
	//media
	$('.media').media({
		 width: 	320
		,height: 	240
		,autoplay: 	true
		,caption: 	false
	});
	
});

function go_fade(num) {
	//unmark all links
	$("#fade .nav li").removeClass("on");
	
	//mark selected link
	$("#fade .nav li:eq(" + (num - 1) + ")").addClass("on");
	
	//hide all shown slides and fade out image
	$("#fade .cont div").hide()
	$("#fade .img div.i" + current_slide).fadeOut(300);
	
	//show selected slide and fade in image
	$("#fade .cont div.c" + num).fadeIn("normal");
	$("#fade .img div.i" + num).fadeIn(500);
	
	//set current slide
	current_slide = num * 1;

} //end go_fade()

function start_autofade() {
	clearInterval(slide_interval);
	slide_interval = setInterval(function() { autofade(); }, slide_time);
}

function autofade() {
	var next_slide = (current_slide * 1) + 1;
	
	//console.log("num: " + next_slide + " : " + current_slide);
	
	if(next_slide > 4) next_slide = 1;
	
	go_fade(next_slide);
	
	start_autofade();
}

function kreative_fix() {
	$("body").addClass("kreative");
}


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

