var show_circle = true;
$(function() {
	$(document).pngFix();
	$('#caesarstone').hide();
	$('#hover_lightbox').css({opacity:0});
	$('#hover_lightbox_main').css({opacity:0});
	$('div#splash_content').hide();
	$('div#splash_content2').hide();
	$('li.caesarstone a')[0].href = '#';
	try {
		$('.home ul.section_images li#img_4 a')[0].href = '/edges/gallery/custom';
		$('.home ul.section_images li#img_4 a').html('<img src="/images/home/fpo4.jpg" title="Edges" alt="Edges" />');
	} catch(err) { }
/*
  $('.home ul.section_images li p').hover(function(el) {
    el.addClass('detail_text');
  });
*/

});

function getCategory(id) {
	var id = parseInt(id,10);
	if(parseInt(id,10) == NaN) {
		id = 1;
	}
	$.ajax({
		url: "/images/show/" + parseInt(id,10),
		cache: false,
		success: function(html){
			$('div#splash_content').hide();
			$('div#splash_content2').hide();
			if(parseInt(id,10) == 1) {
				$('div#inner_wrapper').removeClass('caesarstone_txt').removeClass('marble_txt').removeClass('custom_txt').addClass('standard_txt');
				$('.edges_gallery .standard_txt div#splash_content').fadeIn('fast');
				if(jQuery.browser.msie == true && jQuery.browser.version < 7.0) {
					$('.edges_gallery .standard_txt div#splash_content2').fadeOut('fast');
					$('.edges_gallery .standard_txt div#splash_content').fadeIn('fast');
				}
			} else if(parseInt(id,10) == 2) {
				$('div#inner_wrapper').removeClass('caesarstone_txt').removeClass('marble_txt').removeClass('standard_txt').addClass('custom_txt');
				$('.edges_gallery .custom_txt div#splash_content').fadeIn('fast');
			} else if(parseInt(id,10) == 3) {
				$('div#inner_wrapper').removeClass('standard_txt').removeClass('marble_txt').removeClass('custom_txt').addClass('caesarstone_txt');
				$('.edges_gallery .caesarstone_txt div#splash_content').fadeIn('fast');
				if(jQuery.browser.msie == true && jQuery.browser.version < 7.0) {
					$('.edges_gallery .caesarstone_txt div#splash_content').fadeOut('fast');
					$('.edges_gallery .caesarstone_txt div#splash_content2').fadeIn('fast');
				}
			} else if(parseInt(id,10) == 4 || parseInt(id,10) == 5 || parseInt(id,10) == 12 || parseInt(id,10) == 13) {
				$('div#inner_wrapper').removeClass('caesarstone_txt').removeClass('standard_txt').removeClass('custom_txt').addClass('marble_txt');
					$('.showcase_gallery .marble_txt div#splash_content').fadeIn('fast');
			} 
			if(id > 2) {
				show_circle = false;
			} else {
				show_circle = true;
			}

			var results = eval(html);
			var result_string = "<ul>";
			
			result_string += jQuery.map(results, function(el) { 
				var htmlString	=  "<li id='"+el.image.id+"'>";
				htmlString			+= "<a href='#"+el.image.title+"'><img src='/images/thumbs/" + el.image.thumb + "' class='thumb' /></a>";
				
					htmlString			+= "<img src='/images/slab/" + el.image.slab + "' class='slab' />";
					

					htmlString		+= "<img src='/images/large/" + el.image.file_name + "' class='large' />";
					
					htmlString		+= "<img src='/images/slab/" + el.image.slab + "' class='slab_detail' />";
					if(show_circle == true) {
						htmlString		+= "<img src='/images/circle/" + el.image.circle + "' class='circle' />";
					}
					htmlString		+= "<span>"+el.image.title+"</span>";
				
				htmlString			+= "</li>";
				return htmlString;
			}).join('');
			result_string += "</ul>";
			$('#results').html(result_string);

			
		// 	$('#results ul li .slab').hover(function(ev) {
		// 		ev.stopPropagation();
		// //		$('#hover_lightbox').css({'opacity': 0});
		// 	}, function() {	});
		// 	
		// 	$('#results ul li .large').hover(function(ev) {
		// 		ev.stopPropagation();
		// //		$('#hover_lightbox').css({'opacity': 0});
		// 	}, function() {	});
		// 	
		// 	$('#results ul li .circle').hover(function(ev) {
		// 		ev.stopPropagation();
		// //		$('#hover_lightbox').css({'opacity': 0});
		// 	}, function() { });

			var isAnimating = false;
			var nextAction = null;

			var fadeIn = function(){
				// console.log("fadeIn();");
				if(isAnimating) {
					nextAction = fadeIn;
					// console.log("fadeIn()::blocked, queuing self as next action");
					return;
				}
				isAnimating=true;
				$('#hover_lightbox').fadeTo('fast',0.32,function(){
					// console.log("fade in done");
					isAnimating=false;
					if(typeof(nextAction)=='function'){
						var f = nextAction;
						nextAction = null;
						f();
					}
				});				
			};

			var fadeOut = function(){
				// console.log("fadeOut();");
				if(isAnimating) {
					nextAction = fadeOut;
					// console.log("fadeOut()::blocked, queuing self as next action");
					return;
				}
				isAnimating=true;
				$('#hover_lightbox').fadeTo('fast',0,function(){
					// console.log("fade out done");
					isAnimating=false;
					if(typeof(nextAction)=='function'){
						var f = nextAction;
						nextAction = null;
						f();
					}
				});
			};

			// $('#results ul a').hover(function(ev) {
			// 	// var targetElem = $(ev.target);
			// 	// if(targetElem.hasClass("slab") || targetElem.hasClass("large") || targetElem.hasClass("circle")){
			// 	// 	return false;
			// 	// }
			// 	// if(isAnimating){
			// 	// 	return false;
			// 	// }
			// 	// isAnimating = true;
			// 	fadeIn();
			// }, function(ev) {
			// 	// var targetElem = $(ev.target);
			// 	// if(targetElem.hasClass("slab") || targetElem.hasClass("large") || targetElem.hasClass("circle")){
			// 	// 	return false;
			// 	// }
			// 	// if(isAnimating){
			// 	// 	return false;
			// 	// }
			// 	fadeOut();
			// });
			
			$('#results ul li a').hover(function() {
				fadeIn();
				$(this).parent().children().eq(3).fadeIn('fast'); // large
				if(show_circle == true) {
					$(this).parent().children().eq(5).fadeIn('fast'); // text

				} else {
					$(this).parent().children().eq(4).fadeIn('fast'); // text

				}
				$(this).css({zIndex:'2'});
				
			}, function() {
				fadeOut();
				$(this).parent().children().eq(3).fadeOut('fast'); // large 		
				if(show_circle == true) {
					$(this).parent().children().eq(5).fadeOut('fast'); // text
				} else {
					$(this).parent().children().eq(4).fadeOut('fast'); // text
				}
				$(this).css({zIndex:''});
			});
			
			$('#results ul li a').click(function(el) {
				$('#splash_content').fadeOut('fast');
				$('#splash_content2').fadeOut('fast');
				$('#results ul li').removeClass('on');
				$(this).parent().addClass('on');
				$('#results ul li img.large, #results ul li img.circle, #results ul li img.slab').fadeOut();

					$(this).parent().children().eq(2).fadeIn(); // large
					if(show_circle == true) {
						$(this).parent().children().eq(4).fadeIn(); // circle
					} else {
						$(this).parent().children().eq(3).fadeIn(); // slab
					}

				return false;
			});
			
		}
		
	});
	if(id == 3) {
		$('#edges').hide();
		$('#caesarstone').show();
	} 
	else {
		$('#edges').show();
		$('#caesarstone').hide();
	}
}

$(document).ready(function() {
	
	$('.section_images').hover(function() {
			$('#hover_lightbox_main').animate({opacity:0.52},'fast');
	}, function() {
			$('#hover_lightbox_main').animate({opacity:0},'fast');
	});

	$('.home .section_images li').hover(function() {
		var myId = this.id.substr(4,this.id.length-4);
		
		var pageClass = document.body.className;
		$("#"+pageClass+"_rollover_displayer_"+myId).animate({opacity:1},'fast');
	}, function() {
		var myId = this.id.substr(4,this.id.length-4);
		var pageClass = document.body.className;
		$("#"+pageClass+"_rollover_displayer_"+myId).animate({opacity:0},'fast');
	});

	$('.caesarstone .section_images li, .edges .section_images li, .showcase .section_images li').hover(function() {
		var myId = this.id.substr(4,this.id.length-4);
		var pageClass = document.body.className;
		$("#"+pageClass+"_rollover_displayer_"+myId).animate({opacity:0.72},'fast');
	}, function() {
		var myId = this.id.substr(4,this.id.length-4);
		var pageClass = document.body.className;
		$("#"+pageClass+"_rollover_displayer_"+myId).animate({opacity:0},'fast');
	});

	$('#map_small').click(function() {
		$('#map_large').fadeIn('fast');
		$('#hover_lightbox_main').animate({opacity:0.52},'fast');
	});
	$('#map_large').click(function() {
		$(this).fadeOut('fast');
		$('#hover_lightbox_main').animate({opacity:0},'fast');
	});
});
