/**
 * Gproxy
 */
//Login header
(function($) {
	jQuery.fn.slide = function(){
		var intTimeSlider = 0;
		var currentSlide = 0;
		var objSldie = jQuery(this);
		var SldieItems = jQuery('li', objSldie);
		var totalSlideItems = jQuery(SldieItems).size();
		
		jQuery(objSldie).addClass('objSldie').wrap('<div class="wrapSlide" />')
		var wrapSlide = jQuery(objSldie).parent('.wrapSlide');
		
		
		jQuery(objSldie).css('visibility', 'hidden');
		
		jQuery(wrapSlide).css('position', 'relative').append('<ul class="wrapBtnsSlide"></ul>');
		var wrapBtnsSlide = jQuery('.wrapBtnsSlide', wrapSlide);
		
		jQuery(SldieItems).each(function(i){
			jQuery('<li class="btnSlide btnslidepos'+i+'" id="'+i+'"><span><strong>'+jQuery(this).find('img').attr('alt')+'</strong></span></li>').appendTo(wrapBtnsSlide);
			jQuery(this).addClass('banner'+i);
		});

		jQuery('.btnSlide', wrapSlide).click(function(){
			currentSlide = jQuery(this).attr('id');
			jQuery('.btnSlide.active span', wrapSlide).animate({paddingTop: 0}, 300);
			jQuery('.btnSlide.active strong', wrapSlide).animate({paddingTop: '14px'}, 300);
			jQuery('.btnSlide.active', wrapSlide).animate({height: '14px', marginTop: '20px'}, 300).removeClass('active');
			//jQuery('.btnSlide', wrapSlide).removeClass('active');
			
			jQuery('span', this).animate({paddingTop: '7px'}, 300);
			jQuery('strong', this).animate({paddingTop: '4px'}, 300);
			jQuery(this).animate({height: '34px', marginTop: 0}, 300).addClass('active');
			
			jQuery(SldieItems).removeClass('active');
			jQuery("li:eq("+currentSlide+")", objSldie).addClass('active').fadeTo(400, 1, function(){
				jQuery("li:not(.active)", objSldie).fadeTo(0, 0);
			});
		});
		
		function runSlider() {
			var active = jQuery('li.active', wrapBtnsSlide).attr('id');
			var next = (parseInt(active) + 1);
			if (next == totalSlideItems) {
				next = 0;
			}
			jQuery('li:eq('+next+')', wrapBtnsSlide).click();
			timerSlide();
		}
		
		function timerSlide() {
			clearTimeout(intTimeSlider);
			intTimeSlider = setTimeout(runSlider, 8000);
		}
		
		jQuery('.wrapBtnsSlide li', wrapSlide).hover(function(){
			clearTimeout(intTimeSlider);
		}, function(){
			timerSlide();
		});
		
		window.onload = function(){
			jQuery(SldieItems).removeClass('active').fadeTo(0, 0).filter('li:first', objSldie).addClass('active').fadeTo(400, 1);
			
			jQuery('li', wrapBtnsSlide).css({height: '14px', marginTop: '20px'});
			jQuery('li span', wrapBtnsSlide).css({paddingTop: 0});
			jQuery('li strong', wrapBtnsSlide).css({paddingTop: '14px'});
			
			jQuery('li', wrapBtnsSlide).removeClass('active').filter('li:first', wrapBtnsSlide).addClass('active').css({height: '34px', marginTop: 0});
			jQuery('li:first span', wrapBtnsSlide).css({paddingTop: '7px'});
			jQuery('li:first strong', wrapBtnsSlide).css({paddingTop: '4px'});
			
			jQuery(wrapSlide).css('background', 'none');
			jQuery(objSldie).css({opacity: 0, visibility: "visible"}).animate({opacity: 100}, 500, function(){
				timerSlide();
			});
		}
	}

	//Open or CLose div (used in header logion wrap and stores in home)
	jQuery.fn.BoxSlideDown = function(boxOptions){
		var boxSettings = jQuery.extend({
			textClose: 		'Close',
			btnOpen: 		'',
			btnOpenExtra: 	true,
			hideBox:		true,
			minHeight:		''
			
		}, boxOptions);
		
		var boxOptions = jQuery.extend(boxSettings, boxOptions);

			return this.each(function(){
			
				var box 				= jQuery(this);
				var btnShowHide 		= jQuery(boxOptions.btnOpen);
				var h 					= jQuery(box).attr('offsetHeight');
				
				if(boxOptions.btnOpenExtra){
					jQuery('<div class="closebox">'+boxOptions.textClose+'</div>').prependTo(box);
					var btnShowHideExtra 	= jQuery('.closebox', box);
				}
				
				if(!btnShowHide){
					btnShowHide = jQuery(this);
				}
				
				if(boxOptions.hideBox && !boxOptions.minHeight){
					jQuery(box).height(0);
				}
				
				if(boxOptions.hideBox && boxOptions.minHeight){
					jQuery(box).height(boxOptions.minHeight);
				}
				
				if(boxOptions.btnOpenExtra){
					jQuery(btnShowHideExtra).click(function(){ jQuery(btnShowHide).click(); });
				}
	
				if(boxOptions.hideBox && !boxOptions.minHeight){
					jQuery(btnShowHide).toggle(function(){
						jQuery(box).animate({ height: h }, 300);
					}, function(){
						jQuery(box).animate({ height: "0" }, 300);
					});
				}
				
				if(boxOptions.hideBox && boxOptions.minHeight){
					jQuery(btnShowHide).toggle(function(){
						jQuery(box).animate({ height: h }, 300);
					}, function(){
						jQuery(box).animate({ height: boxOptions.minHeight }, 300);
					});
				}
				
				if(!boxOptions.hideBox){
					jQuery(btnShowHide).toggle(function(){
						if(boxOptions.minHeight){
							jQuery(box).animate({ height: boxOptions.minHeight }, 300);
						}else{
							jQuery(box).animate({ height: 0 }, 300);
						}
					}, function(){
						jQuery(box).animate({ height: h }, 300);
					});
				}
			
		});
		
	};

	//Login header form validation
	jQuery.fn.validate = function(valOptions){
		
		var settings = jQuery.extend({
			errorMessage: 	'The fields are not correct.',
			place: 			this
		}, valOptions);
			
		var Newform = this;
		
		function removeMessages(t){
			
			function removeAnimation(){
				jQuery('.message', settings.place).animate({opacity: 0}, 500, function(){ jQuery(this).remove(); clearTimeout(ta); });
			}
			
			if(t){
				var ta = setTimeout(removeAnimation, 5000);
			}else{
				jQuery('.message', settings.place).remove();
			}
		}
		
		function validationMessages(message){
			removeMessages();
			jQuery(settings.place).prepend('<li class="message">'+message+'</li>');
			removeMessages(true);
		}
	
		function validateForm(){
			var checkRequired = true;
			
			jQuery('.required', Newform).each(function(){
				if(jQuery(this).val() == ''){
					checkRequired = false;
				}
			});
			
			jQuery('.required.email', Newform).each(function(){
				if(!/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/.test(jQuery(this).val())) {
					checkRequired = false;
				}
			});
			
			if(checkRequired == false){
				validationMessages(settings.errorMessage);
				return false;
			}else{
				return true;
			}
		}
		
		this.submit(function(){
			if(!validateForm()){
				return false;
			}
		});
	}

	//Carousel Products (used in home and compare)
	jQuery.fn.CarouselProducts = function(cpOptions){
		var cpSettings = jQuery.extend({
			controlls:	true
		}, cpOptions);
		
		var cpOptions = jQuery.extend(cpSettings, cpOptions);
		
		return this.each(function(){
			
			//prepare html
			jQuery(this).wrap('<div class="cpWrap" />');
			var cp = jQuery(this);
			
			var cpWrap = jQuery(cp).parent('div.cpWrap');
			jQuery(cp).wrap('<div class="cpItemsWrap" />');
			
			jQuery(cp).children('li').addClass('cpItem');
			var cpItem = jQuery('.cpItem ', cp);
			
			jQuery(jQuery(cp).children('.cpItem:first')).before(jQuery(jQuery(cp).children('.cpItem:last')));
			
			//controllers
			if(cpOptions.controlls){
				jQuery(cpWrap).append('<div class="cpBtnLeft" /><div class="cpBtnRight" />');
				var cpBtnRight = jQuery('.cpBtnRight', cpWrap);	
				var cpBtnLeft = jQuery('.cpBtnLeft', cpWrap);
				var cpBtnsActive = true;
				
				jQuery(cpBtnRight).click(function(){
					if(cpBtnsActive){
						cpBtnsActive = false;
						var cpItemMargin = jQuery(cpItem).css('margin-right');
						var cpItemWidth = jQuery(cpItem).outerWidth() + parseInt(cpItemMargin);
						var cpLeftIndent = parseInt(jQuery(cp).css('left')) - cpItemWidth;
						jQuery(cp).animate({'left' : cpLeftIndent}, 200,function(){
							jQuery(jQuery(cp).children('.cpItem:last')).after(jQuery(jQuery(cp).children('.cpItem:first')));
							jQuery(cp).css({'left' : '-'+cpItemWidth+'px'});
							setTimeout(function(){
								cpBtnsActive = true;
							}, 200);
						});
					}
				});
				
				jQuery(cpBtnLeft).click(function(){
					if(cpBtnsActive){
						cpBtnsActive = false;
						var cpItemMargin = jQuery(cpItem).css('margin-right');
						var cpItemWidth = jQuery(cpItem).outerWidth() + parseInt(cpItemMargin);
						var cpLeftIndent = parseInt(jQuery(cp).css('left')) + cpItemWidth;
						jQuery(cp).animate({'left' : cpLeftIndent}, 200, function(){	
							jQuery(jQuery(cp).children('.cpItem:first')).before(jQuery(jQuery(cp).children('.cpItem:last')));
							jQuery(cp).css({'left' : '-'+cpItemWidth+'px'});
							setTimeout(function(){
								cpBtnsActive = true;
							}, 200);
						});
					}
				});	
			
			}
		});
	}
	//Tabs frontend
	jQuery.fn.tabs = function(){
		var tabContainers = this.find('.tab-content');
		tabContainers.hide().filter(':first').show();
		jQuery('.tab_navigation a', this).click(function () {
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			jQuery(this).parent().parent().find('li').removeClass('active');
			jQuery(this).parent().addClass('active');
			return false;
		}).filter(':first').click();
	}
})(jQuery);
jQuery(function(){
//compare
	jQuery('.open-compare').click(function(){
		var href = jQuery(this).attr('href');
		popWin(href,'compare','top:0,left:0,width=820,height=600,resizable=yes,scrollbars=yes');
		return false;
	});
});
