$.linksEnabled=true;
(function($){
	var sources;
	var index;
	var intervalId;
	var slideF;
	var slideB;
	
	$.fn.dewCrossFade=function(options){
		var defaults={
			autoPlay:false
		};  
		var options=$.extend(defaults,options);  
		return this.each(function(){
			var $this=$(this);
			slideB=$this.find('.slides');
			// Add front slide
			slideF=$('<div style="position:absolute;width:'+slideB.css('width')+';height:'+slideB.css('height')+';"/>');
			slideB.append(slideF);
			// Create array of slides
			sources=[];
			slideB.find('img').each(function(){
				sources.push(this.src);
				$(this).remove();
			});
			if(sources.length){
				// Set first slide
				index=0;
				slideB.css('background-image','url('+sources[0]+')');
				slideF.css('background-image','url('+sources[0]+')');
				// Controls
				$this.find('.controls a').each(function(index){
					if(this.rel===''){
						this.rel=index;
					}
					$(this).click(function(){
						stop();
						slideTo(this.rel);
						return false;
					});
				});
				// Auto slide
				if(options.autoPlay)
					start();
				slideB.click(function(){stop();});
			}
		});
	};
	var start=function(){
		intervalId=setInterval(function(){slideBy(1);},6000);
	};
	var stop=function(){
		clearInterval(intervalId);
	};
	var slideBy=function(n){
		n=index+n;
		var l=sources.length;
		if(n<0){
			n=l-1;
		}
		if(n>=l){
			n=0;
		}
		slideTo(n);
	};
	var slideTo=function(n){
		index=n;
		if($('#homepage').length){
			slideF.css({'background-image' : slideB.css('background-image'),'background-size' : '440px 330px'}).stop(true);
			slideB.css({'background-image' : 'url('+sources[index]+')' , 'background-size' : '440px 330px'});
			slideF.fadeTo(0,1).fadeOut(1200,'swing',function(){
				slideF.css({'background-image' : 'url('+sources[index]+')' , 'background-size' : '440px 330px'}).fadeIn(1);
			});
		} 
		if($('#products').length){
			slideF.css({'background-image' : slideB.css('background-image'),'background-size' : '440px 330px'}).stop(true);
			slideB.css({'background-image' : 'url('+sources[index]+')' , 'background-size' : '440px 330px'});
			slideF.fadeTo(0,1).fadeOut(1200,'swing',function(){
				slideF.css({'background-image' : 'url('+sources[index]+')' , 'background-size' : '440px 330px'}).fadeIn(1);
			});
		}
	};
})(jQuery);

$(function(){
	//home
	if('#homepage'){
		$('#slideshow').dewCrossFade({autoPlay:true});
		$('#slider').tinycarousel({display:1});
		var wrap=$('.wrap');
		if($('p',wrap).height()>wrap.height()){
			$('.read-more').show().filter('a').fancybox({
				overlayShow:true,
				overlayColor:'#000',
				padding:0,
				width:400
			});
		}
	}
	//links
	$('#links .link a').click(function(){
		return $.linksEnabled;
	});
	//texts
	$('#texts .text a.img').fancybox({
		onStart:function(){
			return $.linksEnabled;
		},
		overlayShow:true,
		overlayColor:'#000'
	});
	//products
	$('#products .product a').fancybox({
		onComplete:function(){
			$('#product-details').dewCrossFade({});
		},
		onStart:function(){
			return $.linksEnabled;
		},
		overlayShow:true,
		overlayColor:'#000',
		padding:0
	});
	if(window.location.hash.substr(0,9)=='#product_'){
		$(window.location.hash+' a').eq(0).click();
	}
	//login
	$('#login').fancybox({
		overlayColor:'#000',
		overlayShow:true,
		padding:0,
		titleShow:false,
		transitionIn:'elastic',
		transitionOut:'elastic',
		onComplete:function() {
			$('input[name=username]').focus();
		}
	});
});

