$(document).ready(function() {

	// variables

	$.config = {
		showcaseSlide : 0,
		showcaseSlideAnimate : true,
		showcaseSlideMax : 0,
		showcaseSlideWidth : 0
	}

	// navigation : primary

	$navPrimary = $('ul#navigation-primary > li');
	$navPrimary.bind('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			$navPrimary.removeClass('active');
			$(this).addClass('active');
		};
		if (event.type == 'mouseout') {
			$navPrimary.removeClass('active');
		};
	});
	
	$navPrimary.find('a').attr('title', '');

	// showcase

	$showcase = $('div.showcase');
	$showcaseLinks = $showcase.find('> ol');
	$showcaseLink = $showcaseLinks.find('> li a');
	$showcaseSlides = $showcase.find('> div.mask div.slides');
	$showcaseSlide = $showcaseSlides.find('> div.slide');
	$showcasePointer = $showcase.find('hr.index');
	
	$showcasePointer.width($showcaseLinks.width()+11);
	
	$.config.showcaseSlideWidth = $showcase.width();
	$.config.showcaseSlideMax = $showcaseSlide.size();
	
	$showcaseSlide.css('width', $.config.showcaseSlideWidth);
	$showcaseSlides.css('width', $.config.showcaseSlideWidth * $.config.showcaseSlideMax);
	$showcaseLink.first().addClass('active');
	
	function showcaseSlide() {
    $showcaseSlides.animate({ left: $.config.showcaseSlide * -$.config.showcaseSlideWidth }, { duration: 500, easing: "easeOutQuint" });
  }

	function showcaseFade() {
		$showcaseSlides.fadeOut(250, function() {
			$showcaseSlides.css('left', $.config.showcaseSlide * -$.config.showcaseSlideWidth);
			$showcaseSlides.fadeIn(250);
		});
	}
	
	$showcase.hover(function() {
		$.config.showcaseSlideAnimate = false;
	}, function() {
		$.config.showcaseSlideAnimate = true;
	});
	
	$showcaseLink.click(function() {
		$showcaseLink.removeClass('active');
		$(this).addClass('active');
		$.config.showcaseSlide = $showcaseLink.index($(this));
		showcaseFade();
		return false;
	});
	
	setInterval(function () {
		if ($.config.showcaseSlideAnimate) {
	    $.config.showcaseSlide = $.config.showcaseSlide + 1 == $.config.showcaseSlideMax ? 0 : $.config.showcaseSlide + 1;
	    showcaseFade();
			$showcaseLink.removeClass('active');
			$showcaseLink.eq($.config.showcaseSlide).addClass('active');
		};
	}, 5500);
	
	// window
	
	$(window).resize(function() {
		$.config.showcaseSlideWidth = $showcase.width();
		$showcaseSlide.css('width', $.config.showcaseSlideWidth);
		$showcaseSlides.css('left', $.config.showcaseSlide * -$.config.showcaseSlideWidth);
	});
	
	// product photo
	
	$productPhoto = $('div#product-photo div.photo');
	$productPhoto.hide().fadeIn(800);
	
	// category-products thumbnails
	
	$categoryProductsThumbs = $('div.category div.product-image');
	$categoryProductsThumbs.hover(function() {
		$categoryProductsThumbs.stop().fadeTo(300, 0.5);
		$(this).stop().fadeTo(300, 1);
	}, function() {
		$categoryProductsThumbs.stop().fadeTo(1500, 1);
	});
	
	// product thumbnails
	
	$productThumbs = $('div.product-thumbnails a.thumbnail');
	$productPhoto = $('div.photo a img');
	
	$productThumbs.css('opacity', '0.5').filter('.active').fadeTo(300, 1);
	
	$productThumbs.click(function() {
		$productThumbs.removeClass('active').stop().fadeTo(600, 0.5);
		$(this).addClass('active').stop().fadeTo(300, 1);
		var image = $(this).attr('href');
		$productPhoto.fadeOut(300, function() {
			$(this).attr('src', image).fadeIn(300).parent().attr('href', image);
		});
		return false;
	});
	
	$productThumbs.hover(function() {
		$(this).stop().fadeTo(300, 1);
	}, function() {
		if (!$(this).hasClass('active')) {
			$(this).stop().fadeTo(600, 0.5);
		};
	});
	
	// title attr
	
	$("li.cat-item a").each(function(){
	 $(this).removeAttr('title');
	});

	// lightbox
	
	$('a.lightbox').lightBox();
	
	// archives
	
	var $_GET = {};

	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
	    function decode(s) {
	        return decodeURIComponent(s.split("+").join(" "));
	    }
	    $_GET[decode(arguments[1])] = decode(arguments[2]);
	});
	
	if ($('body').hasClass('category-news') || $('ul.archives').hasClass('News')) {
		$('ul.archives li a').each(function(index) {
		  $(this).attr('href', $(this).attr('href') + '?archives=News');
		});
	} else if ($('body').hasClass('category-weblog') || $('ul.archives').hasClass('Blog')) {
		$('ul.archives li a').each(function(index) {
		  $(this).attr('href', $(this).attr('href') + '?archives=Blog');
		});
	} else if ($('body').hasClass('archive') || $('body').hasClass('single')) {
		var archive = $('div#navigation-context h2:first a').text();
		$('ul.archives li a').each(function() {
		  $(this).attr('href', $(this).attr('href') + '?archives=' + archive);
		});
	};
	
	$('textarea').mouseover(function() {
		$(this).focus();
	});
	
	$('ul.blogroll a[href^="http://"]').attr("target", "_blank");
	
	// corners
	
	if ($.browser.msie) {
		$('#navigation-primary > li').css('position', 'relative').addClass('corners c-w12 c-tl c-tr');
		$('#navigation-context > ul').css('position', 'relative').addClass('corners c-w16 c-tl c-bl');
		$('#navigation-context > ul li a').css('position', 'relative').addClass('corners c-r16 c-tr c-br');
		$('div.showcase').css('position', 'relative').addClass('corners c-w16 c-bl c-br');
		$('#navigation-top').addClass('corners c-w12 c-bl c-br');
		$('div.feature-panel').addClass('corners c-w16 c-bl c-br');
		$('div.columns.services div.column').css('position', 'relative').addClass('corners c-w16 c-tl c-tr c-bl c-br');
		$('#call-to-action').css('position', 'relative').addClass('corners c-w16 c-tl c-tr c-bl c-br');
		$('div.download').css('position', 'relative').addClass('corners c-w12 c-tl c-tr c-bl c-br');
		$('div.navigation-box > ul').css('position', 'relative').addClass('corners c-w16 c-tl c-tr c-bl c-br');
		$('div.navigation-box > ul li a').css('position', 'relative').addClass('corners c-r12 c-tr c-br');
		$('div.feature-panel h2 a').css('position', 'relative').addClass('corners c-b16 c-tl c-tr c-bl c-br');
		$('div.product-thumbnails').css('position', 'relative').addClass('corners c-w16 c-bl c-br');
		$('div.pullout').css('position', 'relative').addClass('corners c-w16 c-tl c-tr c-bl c-br');
		$('div#column-content div.feature ol').addClass('corners c-w16 c-tl c-tr c-bl c-br');
		$('div#column-content div.feature ul').addClass('corners c-w16 c-tl c-tr c-bl c-br');
		$('h2.box-header').css('position', 'relative').addClass('corners c-w16 c-tl c-tr');
		$('a.more-link').addClass('corners c-w12 c-tl c-tr c-bl c-br');
		$('p.prev').addClass('corners c-w12 c-tl c-tr c-bl c-br');
		$('p.next').addClass('corners c-w12 c-tl c-tr c-bl c-br');
		$('p.comment-number').addClass('corners c-w12 c-tl c-tr c-bl c-br');
		$('div.category p.comment-number').addClass('corners c-b12 c-tl c-tr c-bl c-br');
		
		$('.corners').append('<b class="tl"></b><b class="tr"></b><b class="bl"></b><b class="br"></b>');
	};
		
});