$(document).ready(function(){
	$('#sliderr').hide();
	$('body').append('<div id="loading"><img src="/assets/images/loading.gif" alt="Loading..."/></div>');
	$('#loading').click(function(){
		$(this).fadeOut('slow');
	});
	$.facebox.settings.closeImage = '/assets/images/closelabel.png';
	$.facebox.settings.loadingImage = '/assets/images/loading.gif';
	$.facebox.settings.overlay = false;
	$('#enquire').live('click', function() {
		var href = $(this).attr('href');
		$.facebox(function() {
			$('body').append('<div id="facebox_overlay" class="facebox_hide facebox_overlayBG" style="display: block;"></div>');
			$("#facebox .content").empty().load(href+" #enquiry_form", function(response, status, xhr) {
				$("#enquiry_form").validate({
					errorPlacement: function(error, element) { }
				});
			});
		});
		return false;
	});
	$(document).bind('afterClose.facebox', function() {
		if($('#facebox_overlay').length > 0) {
			$('#facebox_overlay').remove();
		}
	});
	$('.modal, a[rel*=modal]').facebox();
	$('#scroller .menu a').click(function() {
		var href = $(this).attr('href');
		$('#scroller .menu li').removeClass('active');
		$('#scrollable .jcarousel-skin').hide();
		$(href).parent().parent().parent().show();
		$(this).parent().addClass('active');
		return false;
	});
	$('#scrollable ul').jcarousel({
		scroll: 1,
		wrap: 'circular'
	});
	$('#vscrollable ul').jcarousel({
		scroll: 1,
		vertical: true
	});
	$('body.category-faqs #posts div.the_content').hide();
	$('body.category-faqs #posts h3').click(function() {
		$('body.category-faqs #posts div.the_content').hide();
		$('body.category-faqs #posts h3').removeClass('active');
		$(this).addClass('active').next('div.the_content').show();
	}).filter(":first").click();
	$('input.text').click(function() {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});
	$('input.text').blur(function() {
		if (this.value == '') {
			this.value = this.defaultValue;
		}
	});
	$('#content table:not(.nostripe) tbody tr:odd').addClass("odd");
	$('#vscrollable a').live('click', function(e) {
		var rel = $(this).attr('rel');
		var href = $(this).attr('href');
		$.address.value(rel);
		switchTo(href, rel);
		return false;
	});
	$.address.externalChange(function(e){
		if(e.value != '/') {
			var url = window.location.pathname;
			var href = '/products'+e.value+'/';
	    switchTo(href, e.value.replace('\/',''));
		}
  });
	$('body#home #scrollable ul li a').click(function(){
		var url = $(this).attr('rev');
		window.location = url;
		return false;
	});
	$("#subscribe").validate({
		errorPlacement: function(error, element) { }
	});
	$("#enquiry_form").validate({
		errorPlacement: function(error, element) { }
	});
});
$(window).load(function() {
	$('#loading').fadeOut('slow');
	$('#sliderr').nivoSlider({
		prevText: 'prev',
		nextText: 'next',
		effect: 'fade',
		pauseTime: 6000
	});
	$('#adverts').nivoSlider({
		effect: 'fade',
		pauseTime: 4000
	});
	if($('#sliderr img').length <= 1) {
		$('.nivo-controlNav, .nivo-prevNav, .nivo-nextNav').hide();
	}
	if($('.jcarousel-clip-vertical > ul > li').length <= 3) {
		$('.jcarousel-prev-vertical, .jcarousel-next-vertical').addClass('hidden');
	}
	$('#sliderr').fadeIn('slow');
});
function switchTo(href, code)
{
	jQuery('#loading').show();
	jQuery("#ajaxify").empty().load(href+" #ajaxify", function() {
		jQuery('#sliderr').nivoSlider({
			prevText: 'prev',
			nextText: 'next',
			effect: 'fade',
			pauseTime: 6000
		});
		if(jQuery('#sliderr img').length <= 1) {
			jQuery('.nivo-controlNav, .nivo-prevNav, .nivo-nextNav').hide();
		}
		jQuery('#loading').hide();
	});
	if(code != '') {
	  $.address.title(code+' | Collections | Intricately Designed. Intimately Handcrafted. Timelessly Elegant | Cape Cobra');		
	}
}
