// JavaScript Document
var BASE='/';
$(document).ready(function() {
	$('input[type=button], input[type=submit]').button();
	
    jQuery.easing.def = 'easeInOutCubic';
	$.extend($.fn.jScrollPane.defaults, { animateInterval:50, wheelSpeed: 5});

	var config_heading = {    
		sensitivity: 5, // number = sensitivity threshold (must be 1 or higher)    
		interval: 20, // number = milliseconds for onMouseOver polling interval    
		over: function(){
			//$("#header #under").stop().fadeOut(3000);
			$("#header #over").stop().fadeIn(3000);
		}, // function = onMouseOver callback (REQUIRED)    
		timeout: 50, // number = milliseconds delay before onMouseOut
		out: function(){
			//$("#header #under").stop().fadeIn(3000);
			$("#header #over").stop().fadeOut(3000);
		} // function = onMouseOut callback (REQUIRED)    
	};
	$('#header a').hoverIntent(config_heading);
	
	$('#menu ul li a').css("background-position", "-100px 0px");
	$('#menu ul li a').css("opacity", 0.5);
	
	var config = {    
		sensitivity: 5, // number = sensitivity threshold (must be 1 or higher)    
		interval: 50, // number = milliseconds for onMouseOver polling interval    
		over: function(){
			$(this).stop().animate({ opacity: 1, backgroundPosition:'(0px -180px)', easing:'easeInOutCubic'}, 300);
		}, // function = onMouseOver callback (REQUIRED)    .animate({backgroundPosition:'(0px -360px)', opacity: 1, easing:'easeInOutCubic'}, 500)
		timeout: 50, // number = milliseconds delay before onMouseOut    
		out: function(){$(this).stop().animate({backgroundPosition:'(-100px 0px)', opacity: 0.5, easing:'easeInOutCubic'}, 300);} // function = onMouseOut callback (REQUIRED)    
	};
	$("#menu ul li a").hoverIntent(config);
});

function showElements(element) {
	var config = {    
		sensitivity: 5, // number = sensitivity threshold (must be 1 or higher)    
		interval: 50, // number = milliseconds for onMouseOver polling interval    
		over: function(){$(this).stop().animate({opacity: 1}, 200).animate({opacity: 1}, 100);}, // function = onMouseOver callback (REQUIRED)    
		timeout: 50, // number = milliseconds delay before onMouseOut    
		out: function(){$(this).stop().animate({opacity: 0.7}, 200);} // function = onMouseOut callback (REQUIRED)     
	};	
	
	element.fadeIn(500, function() {
		if(element.attr('class') != 'title') {
			element.click(function(){
				var query = $(this).attr('id').split('f');
				getFurniture(query[0],query[1],tors);
			}).css('cursor', 'pointer').animate({opacity: 0.7}, 200).hoverIntent(config);
		}	
	});
	if(element.is("#sidecontent li:nth-child(16n)") || element.is("#sidecontent li:last-child")) {
		$('#sidecontent').jScrollPane();
		$('#sidecontent').css("overflow", "auto");
		$('#sidecontent .jScrollPaneContainer').css("height", "400px");
	}
	
	if(typeof element.next() != 'undefined') {
		setTimeout(function() { showElements(element.next()); }, 100);
	}
}

function getFurniture(type, style, tors) {
	var query='type='+type+'&style='+style+'&tors='+tors;
	//$('#overcontent').fadeOut(300);
	$("#maincontent").fadeOut(300, function(){
		$.ajax({
			url: BASE + 'furnitures/get_furniture',
			data: query,
			success: function(html){
				$('#maincontent').html(html);
				$("#maincontent").fadeIn(500);
				
				loaderIn();
				
				$('#furniture_select').change(function() {
					var typestyle = $(this).find('option:selected').attr('value');
					var tors = $('#tors').val();
					var current = $('#current').val();
					if(tors == 0) getFurniture(current, typestyle, tors);
					else getFurniture(typestyle, current, tors);
				});
		
				showElementsWithImages($("#maincontent .furniture:first"));
				
				$('#maincontent img').css("opacity", 0.8);
				var config = {    
					sensitivity: 5, // number = sensitivity threshold (must be 1 or higher)    
					interval: 50, // number = milliseconds for onMouseOver polling interval    
					over: function(){$(this).stop().animate({opacity: 1}, 200);}, // function = onMouseOver callback (REQUIRED)    
					timeout: 50, // number = milliseconds delay before onMouseOut    
					out: function(){$(this).stop().animate({opacity: 0.7}, 200);} // function = onMouseOut callback (REQUIRED)    
				};
				$("#maincontent img").hoverIntent(config); 
				
				$('#maincontent .furniture').click(function () { 
					showContent($(this).find("img").attr("id"));
				});			
			}
		}); 
	});
}

function showContent(furniture_id) {
	loaderIn();
	$.ajax({ 
		url: BASE + "furnitures/furniture_content",
		data: "furniture_id=" + furniture_id,
		cache: false,
		success: function(html){
			$('#contentcontainer').fadeOut(200,function(){
				$('#overcontent').html(html);
				$('#overcontent').find('img').load(function(){
					$("#overcontent").fadeIn(500); 	
					loaderOut();
				});
			
				$("button.close").click(function () { 
					$("#overcontent").fadeOut(500,function() {					
						$("#contentcontainer").fadeIn(500);
						$("#jscrollpane").css('height','450px').css('width','660px').jScrollPane();
						$("#sidecontent").css('height','450px').css('width','300px').jScrollPane();
					});
				});
				
				$("#overcontent .other3 img").click(function(){
					element=$(this).attr('id'); 
					$("#overcontent .main2 img").each(function(){
						if($(this).css('display')!='none') {
							$(this).fadeOut(200,function(){$("#main"+element).fadeIn(200);});
						}
					});
				}).css('cursor', 'pointer');
			});
		}
	});
}

function showElementsWithImages(element) {
	if(element.is("#jscrollpane div:nth-child(4n)")) {
		$('#jscrollpane').css("overflow", "auto");
		$('#jscrollpane').jScrollPane();
		$('#jscrollpane').css("left", "0px");
		$("#overcontent").css("left", "0px");
	}
	
	element.fadeIn(500);
					
	if(typeof element.next().find('img').attr('id') == 'undefined') {
		loaderOut();
		$('#jscrollpane').jScrollPane();
	} else {
		setTimeout(function() { showElementsWithImages(element.next()); }, 200);	
	}
}

function specialsLinks() {
	$('#maincontent img').css("opacity", 0.8);
	var config = {    
		sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		interval: 20, // number = milliseconds for onMouseOver polling interval    
		over: function(){$(this).stop().animate({opacity: 1.2}, 200).animate({opacity: 1}, 100);}, // function = onMouseOver callback (REQUIRED)    
		timeout: 50, // number = milliseconds delay before onMouseOut    
		out: function(){$(this).stop().animate({opacity: 0.8}, 200);} // function = onMouseOut callback (REQUIRED)    
	};
	$("#maincontent img").hoverIntent(config); 
	
	$('#maincontent .furniture').click(function () { 
		showContent($(this).find("img").attr("id"));
	});
}

function loaderIn() { $("#loading").stop().fadeIn(200); }
function loaderOut() { $("#loading").stop().fadeOut(200); }
