// JavaScript Document

// JavaScript Document
function fade(crap){
	$(crap).fadeOut(200);
}

var current = 1;
var gal_length;
var timeout = 1000;
var timein = 1000;
var forward_time;
$(document).ready(function(){
	$li = $('.gallery li');
	
	gal_length = $li.length;
	
	
	for(i=0; i < $li.length;i++){
		//add navigation number
		$('#gal_navigation').append('<span id="item-'+(i+1)+'" title="'+(i+1)+'">'+(i+1)+'</span>');
		
		if(i+1 >= current){
			$li[i].id = 'active';
		}else{
			$li[i].id = '';
			$($li[i]).css({display:'none'});
		}
		$li[i].className = 'galItemnum_'+(i+1);
	}
	
			update_num_gal();
	$('#gal_navigation span').click(function(){
		this_id = $(this).attr('title');
		if(current == this_id){
			return false;
		}else{
			clearTimeout(forward_time);
			
			$($li[current-1]).fadeOut(1000,function(){
													//after animation
													$(this).css({display:'none'})
												});
			
			$($li[this_id-1]).fadeIn(1000);
			
			current = this_id;
			
		update_num_gal();
			forward_time = setTimeout("set_theTime()",timein);
		}
	});
	
	$li = $('.gallery li');
	//show next item
	$('.next_gal_item').click(function(){
		//process here
		show_next_item(current);
	});
	
	//show prev item
	$('.prev_gal_item').click(function(){
		//process here
		show_prev_item(current);
	});
	
	forward_time = setTimeout("show_next_item("+current+")",timeout);
	
});

function show_next_item(id){
	clearTimeout(forward_time);
	$li = $('.gallery li');
	if(current == $li.length){
		id = 0;
		to_id = id;
		gal_length = $li.length;
		
		$($li[$li.length-1]).fadeOut(1000,function(){
												//after animation
												$(this).css({display:'none'})
											});
		$($li[to_id]).fadeIn(1000);
		current = to_id+1;
	}else{
		from_id = id;
		to_id = id;
		
		gal_length = $li.length;
		
		$($li[from_id-1]).fadeOut(2000,function(){
												//after animation
												$(this).css({display:'none'})
											});
		
		$($li[to_id]).fadeIn(2000);
		current = to_id+1;
	}
	
	update_num_gal();
	forward_time = setTimeout("set_theTime()",timein);
}

function set_theTime(){
	forward_time = setTimeout("show_next_item("+current+")",timeout);
}

function show_prev_item(id){
	clearTimeout(forward_time);
	$li = $('.gallery li');
	if(id == 1){
		
		gal_length = $li.length;
		$($li[0]).fadeOut(2000,function(){
												//after animation
												$(this).css({display:'none'})
											});
		$($li[$li.length-1]).fadeIn(2000);
		current = $li.length;
		
	}else{
		from_id = id-1;
		to_id = id;
		
		gal_length = $li.length;
		$($li[from_id]).fadeOut(2000,function(){
												//after animation
												$(this).css({display:'none'})
											});
		$($li[from_id-1]).fadeIn(2000);
	}
		current = from_id;
	forward_time = setTimeout("set_theTime()",timein);
	update_num_gal();
}

function update_num_gal(){
	$('#gal_navigation span').css({
															fontWeight: 'normal',
														}).animate({
															fontSize:'12px'
														});
	$('#item-'+current).css({
											fontWeight: 'bold'
										}).animate({
											fontSize:'20px'
										})
	
}

var item_number;
var item_width;
var item_holder;
var scroll_limit;
var cName;
var current_lastItem = 0;
var current_firstItem = 3;

var lhide=false;
var rhide=false;
var next_link;
var next_trig;

$(document).ready(function(){
 cName = $('#catname').html();
	item_number = $('.prodwrapper').length;
	item_holder = $('.prodwrapper');
	item_width = 320;
	scroll_limit = (item_width*(item_number-3))-20;
	$('.prodList_container').animate({scrollLeft:"-="+(item_width)})
	$('#prodList_slider').width(item_width*item_number);
	
	$('.pscroll_left').animate({
											opacity: 0
										},'fast');
	
	$('.pscroll_right').animate({
											opacity: 0
										},'fast');
	
	
	$('.pscroll_left').mouseover(function(){
											$(this).animate({
													opacity: 1
											},'fast')
										})
	
	
	$('.pscroll_left').mouseout(function(){
											$(this).animate({
												opacity: 0
											},'fast')
										})
	
	
	
	$('.pscroll_right').mouseover(function(){
											$(this).animate({
													opacity: 1
											},'fast')	
										})
	
	
	$('.pscroll_right').mouseout(function(){
											$(this).animate({
												opacity: 0
											},'fast')
										})
	
/*	$('.pscroll_left').mouseover(function(){
											$(this).animate({
																width:220+'px'
															},'fast',function(){
															})
											})
	$('.pscroll_left').mouseout(function(){
											if(!lhide){
											$(this).animate({
																width:65+'px'
															})
											}
										})
	
	$('.pscroll_right').mouseover(function(){
											$(this).animate({
																width:220+'px'
															},'fast',function(){
																
											})
										})
	
	$('.pscroll_right').mouseout(function(){
											if(!rhide){
												$(this).animate({
																	width:65+'px'
																})
											}
										})*/
	togglePscrollLeft('hide');

		next_text = $('#next_cat').attr('title');
		
	if($('#next_cat').attr('title') != ''){
		$('.pscroll_right').html('<br>'+next_text).click(function(){
				window.location = $('#next_cat').val();
		})
	}
	
	write_Nav();
})


function PscrollLeft(){
	$('.prodList_container').css({opacity:1}).animate({
		scrollLeft:"-="+(item_width*3),
		opacity:1
	},1000,function(){
		if($('.prodList_container').scrollLeft() <= 0){
			current_firstItem = 3;
			current_lastItem = 0;
			togglePscrollLeft('hide');
			write_Nav();
		}else{
			togglePscrollRight('show');
			current_firstItem = current_firstItem-3;
			current_lastItem = current_lastItem-3;
			write_Nav();
		}
	})
}

function PscrollRight(){
	$('.prodList_container').css({opacity:1}).animate({
		scrollLeft:"+="+item_width*3,
		opacity:1
	},1000,function(){
	if($('.prodList_container').scrollLeft() >= scroll_limit){
		current_firstItem = item_number;
		current_lastItem = current_firstItem-4;
		togglePscrollRight();
		//last item
	}else{
		togglePscrollLeft('show');
		current_firstItem = current_firstItem+3;
		current_lastItem = current_firstItem-4;
	}
	})

	if(next_trig > 0){
		window.location = next_link;
	}
	write_Nav();
		$(this).animate({
							opacity:1
						});
}

function write_Nav(){
	if(current_lastItem !=''){
		$('.pscroll_left').html(item_holder[current_lastItem].title+'<br>'+cName+'<br>'+(current_lastItem+1)+'/'+item_number);
	}
	if($('.prodList_container').scrollLeft() >= scroll_limit){
	}else{
		$('.pscroll_right').html(item_holder[current_firstItem].title+'<br>'+cName+'<br>'+(current_firstItem+1)+'/'+item_number);
	}
}

function write_Nav2(){
	if(current_lastItem !=''){
		$('.pscroll_left').html(item_holder[current_lastItem].title+'<br>'+cName+'<br>'+(current_lastItem+1)+'/'+item_number);
	}
	if($('.prodList_container').scrollLeft() >= scroll_limit){
	}else{
		$('.pscroll_right').html(item_holder[current_firstItem].title+'<br>'+cName+'<br>'+(current_firstItem+1)+'/'+item_number);
	}
}

function togglePscrollLeft(trig){
	next_trig = 0;
	next_link = '';
	if(trig == 'show'){
		lhide = false;
		$('.pscroll_left').fadeIn('fast');
		$('.arrow1').show();
	}else{
		lhide = true;
		prev_text = $('#prev_cat').attr('title');
		if($('#prev_cat').attr('title') != ''){
			$('.arrow1').show();
			$('.pscroll_left').html('<br>'+prev_text).click(function(){
				window.location = $('#prev_cat').val();
			});
		}else{
			$('.pscroll_left').hide();
			$('.arrow1').hide();
		}
	}
}

function togglePscrollRight(trig){
	if(trig == 'show'){
		rhide = false;
		$('.pscroll_right').fadeIn('fast');
		$('.arrow2').show();
		next_trig = 0;
		next_link = '';
	}else{
		rhide = true;
		next_text = $('#next_cat').attr('title');
		if($('#next_cat').attr('title') != ''){
			$('.arrow2').show();
			$('.pscroll_right').html('<br>'+next_text).click(function(){
				window.location = $('#next_cat').val();
			});
		}else{
			next_trig = 0;
			next_link = '';
			$('#next_cat').hide();
			$('.arrow2').hide();
		}
		//$('.pscroll_right').hide();
	}
}

function add_quantity(id){
	old_val = $('#'+id).val();
	new_val = parseInt(old_val)+1;
	$('#'+id).val(new_val);
	$('#cart_form').submit();
}

function deduct_quantity(id){

	old_val = $('#'+id).val();
	new_val = parseInt(old_val)-1;
	$('#'+id).val(new_val);
	$('#cart_form').submit();
}


function delete_product(id){
	//$('#'+id+':checkbox');
	document.getElementById(id).checked = 'checked';
	$('#cart_form').submit();
}

var shipping_price;

function show_total_price(firstP,secondP,id){
	shipping_price = id;
	finalP = parseFloat(firstP)+parseFloat(secondP);
	$('#cart_total').html(finalP+'&nbsp; EUR');Cufon.replace('#Contents', {
		hover: true
    });
}

function show_this(id){
	$('#'+id).fadeIn('fast');
}

function fade_this(id){
	$('#'+id).css({opacity:0}).delay(3000).animate({
															opacity:1
														},2000);
}


function blackout_effect(){
	alert('asd');
}

function close_this(id){
	$('#'+id).fadeOut('fast');
}

