/*
	LoungeRoom Online Solutions: www.loungeroom.nl
*/


function initialize() {
	
	$(document).ready(function(){
		//startAnimate();
		var tab;
		var srch;
		var ku;
		
		$(document).pngFix();
		$('.img').minmax();
		$('.image').minmax();
		$('.frame').minmax();
		
		overall(0);
		initSearchResults(0);
		
		$("#search").submit(function(e) {
			e.preventDefault();
			//clearTimeout(ku);
			trmy = $("#searchValue").val();
			performSearch(trmy);
		}); // end search
		
		$("#searchResults a.close").click(function(e) {
			e.preventDefault();
			$("#searchResults").slideUp();
			window.location = '#';
			searchterm=false;
		});

		search_done = window.location.hash;
		if(search_done.indexOf('#find_') == 0) {
			trmy = search_done.substr(6);
			//alert(trmy);
			performSearch(trmy);
		}
		
		// enlarge photo
		$('.enlarge').click(function(e) {
			e.preventDefault();
			$('.large').removeClass('hide');
			writehtml = '<div class="frame">' + $('#product_image').html() + '<a class="clickandclose" href="#close"><img src="/assets/images/close.png" alt="" /></a></div>';
			$('.container').html(writehtml).addClass('image').removeClass('hide');
		});
		
		$("td.select input").hide();
		$("td.select a").addClass('jquery');
		$("input.inactive").addClass('jquery');
		
		$("#selection td.select a").click(function(e) {
			$(this).parent().parent().children().children().not(this).addClass('stripe').removeClass('active');
			$(this).removeClass('stripe').addClass('active');
			updateQuicksearch(0,'home');
		});
   
		$("#selection input.reset").click(function(e) {resetQuicksearch(0,'home'); });

		$("form#selection_static").submit(function(e) {
			e.preventDefault();
			if($("#selection input.find").hasClass('active')) {
				// zoek producten
				statusOn(); startAnimate('animate');
				$("#quicksearch").slideUp(function() {
					val1 = $("#budget a.active").attr('rel');
					val2 = $("#gender a.active").attr('rel');
					val3 = $("#age a.active").attr('rel');
					$.post("/ajax/quick.search.php?ajax=true", { budget: val1, gender: val2, age: val3 }, function (got) {
						$("#quicksearch").html(got).slideDown(function() {
							statusOff();
							stopAnimate('animate');
						});
					});
				});
			} else {
				// niets doen
				void(0);
			}
				
		});
 
		$("#selection_category td.select a").click(function(e) {
			$(this).parent().parent().children().children().not(this).addClass('stripe').removeClass('active');
			$(this).removeClass('stripe').addClass('active');
			updateQuicksearch(0,'category');
		});
   
		$("#selection_category input.reset").click(function(e) {resetQuicksearch(0,'category'); });

		$("form#selection_static_category").submit(function(e) {
			e.preventDefault();
			if($("#selection_category input.find").hasClass('active')) {
				// zoek producten
				statusOn(); startAnimate('animate');
				$("#quicksearch").slideUp(function() {
					val1 = $("#budget a.active").attr('rel');
					cid = $("#c_id").val();
					$.post("/ajax/quick.search.php?ajax=true", { budget: val1, category: cid }, function (got) {
						$("#quicksearch").html(got).slideDown(function() {
							statusOff();
							stopAnimate('animate');
						});
					});
				});
			} else {
				// niets doen
				void(0);
			}
				
		});

 	});
	
	
}

function init_home(vv) {
	//void(0);
	//$(".shadow").dropShadow({left: -2, top: 2, blur: 1, color: "#000", swap: false});
	//left_slideShow("1",0);
	//window.setTimeout("left_slideShow(\'2\',0);",2000);
}

var fl;
var itemheight;
var height;
var hoesnel;
var opened;

slideopened = new Array();
numopened = new Array();
slideinterval = new Array();
function left_slideShow(d,v) {
	numopened[d] = $(".slideContents.i"+d).children().length;
	left_show(d,0);
	hoesnel = 5000;
	
	slideinterval[d] = setInterval("left_flash('"+d+"');",hoesnel);
	$(".slideContents.i"+d).mouseover(function(o) {
		clearInterval(slideinterval[d]);
	});
	$(".slideContents.i"+d).mouseout(function(o) {
		clearInterval(slideinterval[d]);
		slideinterval[d] = setInterval("left_flash('"+d+"');",hoesnel);
	});
}

function left_show(d,val) {
	$(".slideContents.i"+d).children().eq(val).animate({ 
		left: '0px'
	}, 1500, function(f) {
		slideopened[d] = val;
	});
}
function left_flash(d) {
	$(".slideContents.i"+d).children().eq(slideopened[d]).animate({ 
		left: '-400px' 
	}, 1500, function(f) {
		$(this).css({ left: '400px'});
	});

	nowOpen = slideopened[d]+1;
	teller = numopened[d]-1;
	if(slideopened[d] >= teller) {
		nowOpen=0;
	}
	left_show(d,nowOpen);
}

function initSearchResults(v) {
	$("a.searchresult").click(function(e) {
		pid = $(this).attr("rel");
		href = $(this).attr("href");
		e.preventDefault();
		$.get("/ajax/search.convert.php?ajax=true", { p_id: pid }, function(data) {
			window.location = href;
		});
	});
	
}

function overall() {
	$(".link").click(function(e) {
		window.location=$(this).attr("rel");
	});
   
	// wishlist
	wishinited=false;
	$("a.wish").click(function(e) {
		if(wishinited==false) {
			e.preventDefault();
			wishinited=true;
			statusOn();
			
			productid = $(this).attr("rel");
			$.get('/ajax/wishlist.php?ajax=true', { add: productid }, function(data) {
				$("#write_shortlist").html(data);
				statusOff();
				wishinited=false;
			});
		}
	}); // end wishlist
	
	// VENSTER sluiten
	$('.clickandclose').click(function(e) {
		e.preventDefault();
		$('.large').addClass('hide');
		$('.container').html('').addClass('hide').removeClass('bestellen').removeClass('image');
	});

	// bestellen
	orderinited=false;
	$("a.order").click(function(e) {
		if(orderinited==false) {
			e.preventDefault();
			orderinited=true;
			statusOn();
			//popup('/wishlist/', '220', '300', 'wishlist');
			
			productid = $(this).attr("rel");
			href = $(this).attr("href");
			//$('.large').removeClass('hide');
				
			//$.get('/ajax/order.page.php?ajax=true', { product: productid }, function (o) {
				//$('.container').html(o).addClass('bestellen').removeClass('hide');
				$.get('/ajax/wishlist.php?ajax=true', { add: productid }, function(data) {
					$("#write_shortlist").html(data);
					window.setTimeout("statusOff();",2000);
					orderinited=false;
					window.location=href;
				});
			//});
		}
	}); // end bestellen
	
	// verwijder uit snelkeuze
	$("a.remove").click(function(e) {
		e.preventDefault();
		statusOn();
		
		productid = $(this).attr("rel");
		$(this).parent().parent().remove();
		$.get('/ajax/wishlist.php?ajax=true', { remove: productid }, function(data) {
			statusOff();
			//$("#write_shortlist").html(data);
			$("#shortlist .row:odd").removeClass('noteven').addClass('even');
			$("#shortlist .row:even").removeClass('even').addClass('noteven');
			num_short = $("#shortlist .product_wishlist").children().length;
			if(num_short == 0) {
				statusOn();
				$.get('/ajax/wishlist.php?ajax=true', function(data) {
					$("#write_shortlist").html(data);
					statusOff();
				});
			}
		});
	}); // end verwijder uit snelkeuze

}


var searchterm;
function performSearch(srch) {
	if(srch != searchterm) {
		searchterm = srch;
		statusOn();
		$("#searchValue").removeClass('error');
		$("#searchResults").slideUp(function() {
			$("#found_products").html('');
			$("#found_tags").html('');
			startAnimate('animate');
			startAnimate('found_tags');
			if (srch.length > 0) {
				window.location = '#find_'+srch;
				$.post('/ajax/search.php?ajax=true', { zoek: srch }, function(data) {
					$("#found_products").html(data);
					stopAnimate('animate');
					$("#searchResults").slideDown(function() {
							$.post('/ajax/search_tags.php?ajax=true', { zoek: srch }, function(data) {
								$("#found_tags").html(data);
								statusOff();
							});
							
					});
				});
			} else {
				$("#searchValue").addClass('error');
				stopAnimate('animate');
				statusOff();
			}
		});
	} 
}

		
function popup(url, w, h, name) {
	window.open(url,name,'height='+h+', width='+w+', menubar=no, status=yes, scrollbars=yes, resizable=yes, toolbar=yes, location=no');		
}

function startAnimate(empty) {
	$("#"+empty).html('<img src="/assets/images/ajax_diap.gif" alt="" />');
}
function stopAnimate(empty) {
	$("#"+empty).html('');
}

function startAnimate2(empty) {
	$("#"+empty).html('<div align="center"><img src="/assets/images/squares.gif" alt="" /></div>');
}
function statusOn(empty) {
	$("#status").html('<img src="/assets/images/squares.gif" alt="" />');
}
function statusOff(empty) {
	$("#status").html('');
}
function addToList(additto,add) {
	$("#"+additto).prepend(add);
}

