var title_prefix = "Interior icons - ";

$(document).ready(function() {
    $('.slide-1').cycle({
		fx: 'scrollHorz',
    speed: 500 ,
    prev: '.prev-1',
    next: '.next-1',
		pager: '.pager-1',
    nowrap:  1, 
    timeout: 0 
	});
});

$(document).ready(function() {
    $('.slide-2').cycle({
		fx: 'scrollHorz',
    speed: 500 ,
    prev: '.prev-2',
    next: '.next-2',
		pager: '.pager-2',
    nowrap:  1, 
    timeout: 0 
	});
});

$(document).ready(function() {
    $('#slide-3').cycle({
		fx: 'scrollHorz',
    speed: 500 ,
    prev: '.prev-3',
    next: '.next-3',
		pager: '.pager-3',
    nowrap:  1, 
    timeout: 0 
	});
});

$(document).ready(function() {
 $(".search").click(function () {
      $("#search").toggle();
      $(".in").focus();
	  return false;
    });
});

$(document).ready(function() {
    $('#slice').cycle({
		fx: 'fade',
    speed:  500 , 
		pager: '.pager2', 
    timeout: 4000 
	});
});

// Events (re-)load
function loadEvents() {

	if ($("#tabs").length > 0)
	{
		// Product Detail Tabs
		$('#tabs').delay(2000).tabs({ fxFade: true, fxSpeed: 'fast' });
	}
	
	if ($("#img-brovse").length > 0)
	{
		// Image browse
		$('#img-brovse').cycle({
			fx: 'fade',
			speed:  500 , 
			pager: '.pager2',
			nowrap:  1, 
			timeout: 0,
			containerResizeImproved: 1
		});
	}
	
	if ($("#prodForm").length > 0)
	{
		// Product input fields
		$('#prodForm').submit(function() {
			var isfunc = typeof window.formvalidator0;
			if(isfunc == 'function' && formvalidator0(this)) {
				return true;
			}
			return false;
		});
	}
}

$(document).ready(function() {
    $('#slide-product ul li a').live('click', function(){
		if ($(this).parent().hasClass('active') == false) {
			// No good support for IE6, thus page reload (no xmlhttp)
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				document.location.href = $(this).attr('href');
			}
			$('#slide-product ul li').each(function(){
				$(this).removeClass('active');
			})
			$("#product-detail-wrap")
				.html('<div class="loading"></div>')
				.load($(this).attr('href') + ' #product-detail-wrap', function(){
					$(this).hide().fadeIn(200);
					document.title = title_prefix + $("#product-detail h1").text();
					loadEvents();
				});
			$(this).parent().addClass('active');
		}
		return false;
	});
});

$(document).ready(function() {
	// add event handlers (image browse + product cycle + form onsubmit checkers)
	loadEvents();

    $('#search form').submit(function(){
	
		var url = $(this).attr('action') + '?s=' + $("input.in", this).eq(0).val();
		
		if ($("#product-detail-wrap").length > 0)
		{
			$("#slide-product").html('<div class="loading"></div>');
			$("#product-detail-wrap").html('<div class="loading"></div>');
			$("#header ul li a.section").removeClass('active');
			
			$.get(url, function(data){
				$("#slide-product").
					fadeOut(200, function(){
						$(this).html($("#slide-product", data).html()).fadeIn(200).cycle('destroy').cycle({
							fx: 'scrollHorz',
						speed: 500 ,
						prev: '.prev-4',
						next: '.next-4',
							pager: '.pager-4',
						nowrap:  1, 
						timeout: 0 
						})
					});
				$("#product-detail-wrap").stop(true,true).fadeOut(200, function(){ $(this).html($("#product-detail-wrap", data)).fadeIn(200); loadEvents(); });
				$("#slide-products h2").stop(true,true).hide().text($("#slide-products h2", data).text()).fadeIn(200);
				document.title = title_prefix + $("#product-detail h1", data).text();
				$("#search").fadeOut(500);
			});
			return false;
		}
		else
		{
			document.location.href = url;
		}
	});
});

$(document).ready(function() {
	$("#header ul li a.section").click(function(){
	
		if ($("#product-detail-wrap").length == 0)
			return true;

		if ($(this).hasClass('active'))
			return false;
	
		$("#slide-product").html('<div class="loading"></div>');
		$("#product-detail-wrap").html('<div class="loading"></div>');
		$("#header ul li a.section").removeClass('active');
		$(this).addClass('active');
		
		$.get($(this).attr('href'), function(data){
			$("#slide-product").
				fadeOut(200, function(){
					$(this).html($("#slide-product", data).html()).fadeIn(200).cycle('destroy').cycle({
						fx: 'scrollHorz',
					speed: 500 ,
					prev: '.prev-4',
					next: '.next-4',
						pager: '.pager-4',
					nowrap:  1, 
					timeout: 0 
					})
				});
			$("#product-detail-wrap").stop(true,true).fadeOut(200, function(){$(this).html($("#product-detail-wrap", data)).fadeIn(200); loadEvents();});
			$("#slide-products h2").stop(true,true).fadeOut(200).text($("#slide-products h2", data).text()).fadeIn(200);
			document.title = title_prefix + $("#product-detail h1", data).text();
		});
		return false;
	});
});

$(document).ready(function() {
    $('#slide-product').cycle('destroy').cycle({
		fx: 'scrollHorz',
    speed: 500 ,
    prev: '.prev-4',
    next: '.next-4',
		pager: '.pager-4',
    nowrap:  1, 
    timeout: 0 
	});
});

$(document).ready(function() {
	$("ul.person-list li a").click(function(){
		var url = $(this).attr('href');
		$("div.person-box")
			.animate({ width: 0, opacity: 0.2 }, 200, function (){
				$(this).load(url + ' div.person-box', function(){
					$("img", this).hide();
					$(this).css({opacity: 0, width: ''}).animate({opacity: 0.5}, 200, function(){ $(this).animate({opacity: 1.0}, 200); $("img", this).fadeIn(500); });
					document.title = "Interior icons - Designers - " + $(".person-box h2").eq(0).text();
				});
			});
		
		$("ul.person-list li a").removeClass('active');
		$(this).addClass('active');
		return false;
	});
});
