/**
 * Project: CountryLifeStyle
 * Datum: 14 December 2010
 * Programmeur: S. Evers
**/

(function($){

	$(document).ready(function(){
		
	});
	
	$.extend({
		fakeClick: 0,
		urlScript: function(){
			var script = window.location.pathname;
			script = script.substring(script.lastIndexOf('/') + 1);
			return script;
		},
		homebanners: {
			fade: 1000,
			sleep: 5000,
			collection: [],
			idx: 0,
			obj: '.home-banners',
			locked: 0,
			init: function(options){
				var cache = [];
				options = $.extend({ 
					fade: 1000,
					sleep: 5000,
					imgs: [] 
				}, options);
				
				this.locked = 1;
				
				this.fade = options.fade;
				this.sleep = options.sleep;
				
				$.each(options.imgs, function(iIndex, value){
					if(iIndex > 1){
						var cacheImage = document.createElement('img');
						cacheImage.src = value.img;
						cache.push(cacheImage);
						$('.img-' + iIndex).attr('src', value);
					}
				});
				
				this.collection = $('.product');
				
				$('.product').css('display','none').css('opacity',0);
				$('.product:first').css('display','block').css('opacity',1);
				
				$('ul#products a').bind('click', function(e){
					e.preventDefault();
					
					nextIdx = parseInt($(this).attr('class').replace('idx-',''));
					var ileft = parseInt($('#products').css('left').replace('px',''));
					if((parseInt(nextIdx) != parseInt($.homebanners.idx)) && $.homebanners.locked == 0){
						if(parseInt($.homebanners.collection.size()) > 9){
							if(($.homebanners.idx >= 7 && $.homebanners.idx < ($.homebanners.collection.size() - 2)) && ((ileft > (30 - (($.homebanners.collection.size() - 10) * 94))))){
								$('#products').animate({
									left: '-=94px' 
								},500);
							}
							if($.homebanners.idx == ($.homebanners.collection.size() - 1)){
								$('#products').animate({
									left: '30px' 
								},500);
							}
						}
						if($.fakeClick == 0) $('.home-banners').stopTime();
						$.fakeClick = 0;
						$.homebanners.locked = 1;
						$('ul#products li').removeClass('active');
						$(this).parent().addClass('active');
						$('#idx-' + $.homebanners.idx).animate({
							opacity: 0
						}, $.homebanners.fade, function(){
							$(this).css('display','none');
						});
						
						$('#idx-' + nextIdx)
							.css('display', 'block')
							.css('opacity', '0')
							.animate({
								opacity: 1
							}, $.homebanners.fade, function(){
								$.homebanners.idx = nextIdx;
								$.homebanners.locked = 0;
							});
					}
				});
				
				$('#product-container').bind('mouseover', function(e){
					// on right position
					var yPos = false;
					var xPos = false;
					
					// mouse x & y
					var x = (parseInt(e.clientX) - parseInt($(this).offset().left));
					var y = (parseInt(e.clientY) - parseInt($(this).offset().top));
					
					// element height and width + offset width
					var pW = parseInt($(this).css('width').replace('px','')) + 30;
					var pH = parseInt($(this).css('height').replace('px',''));
					
					// Checks
					if(x >= 30 && x <= pW) xPos = true;
					if(y > (pH - 150)) yPos = true;
					
					var ileft = parseInt($('#products').css('left').replace('px',''));
					
					if(xPos == true && yPos == true){
						if(parseInt($.homebanners.collection.size()) > 9){
							if(x > (pW / 2)){ // right
								if(((ileft > (30 - (($.homebanners.collection.size() - 10) * 94))))){
									$('#products').animate({
										left: '-=94px' 
									},500);
								} else {
									$('#products').clearQueue();
								}
							} else { // left
								if(ileft < (30 - 94)){
									$('#products').animate({
										left: '+=94px' 
									},500);
								} else {
									$('#products').clearQueue();
								}
							}
						}
					}
				});
				
				this.locked = 0;
								
				return this;
			},
			start: function(){
				var self = this;
				$(this.obj).everyTime(this.sleep, "cls-fading", function(){
					if(self.locked == 0){
						var cur = $('ul#products li').find('a.idx-' + self.idx).parent();
						
						if(cur.next().find('a[rel=preview] img').is('.home-img')){
							$.fakeClick = 1;
							cur.next().find('a').click();
						} else {
							$.fakeClick = 1;
							$('ul#products > li:first > a').click();
						}
					}
				});
			},
			stop: function(){
				this.locked = 1;
				$(this.obj).stopTime("cls-fading");
			}
		}
	});

})(jQuery);
