jQuery.extend({
  apply: function(o, c, defaults) {
    if(o && c && typeof c == 'object'){
        for(var p in c){
            o[p] = c[p];
        }
    }
    return o;
  },
  
  
  getTarget:function(e, name) {
	var target = e.target || e.srcElement;
	var reg = name? new RegExp('^'+name+'$', 'i') : null;
	while(target && (target.nodeType != 1 || (reg && !reg.test(target.nodeName)))) {
		target = target.parentNode;
	}
	return target;
  },
  
  addStyle: function(path){
	var style = document.createElement('link');
	style.setAttribute('rel','stylesheet');
	style.setAttribute('type','text/css');
	style.setAttribute('href',path);
	document.getElementsByTagName('head')[0].appendChild(style)	;	  
  }
}); 

(function(){
	ua = navigator.userAgent.toLowerCase(),	  
	check = function(r){
		return r.test(ua);
	}
	isOpera = check(/opera/),			  
	isIE = check(/msie/),
	isIE7 = isIE && check(/msie 7/),
	isIE8 =	isIE && check(/msie 8/),
	isIE6 =	isIE && !isIE7 && !isIE8,
	isChrome = check(/chrome/),
	isWebKit =check(/webkit/),
	isSafari =!isChrome && check(/safari/),
	isSafari2 =	isSafari && check(/applewebkit\/4/),
	isSafari3 = isSafari && check(/version\/3/),
	isSafari4 = isSafari && check(/version\/4/),
	isGecko = !isWebKit && check(/gecko/),
	isGecko2 = isGecko && check(/rv:1\.8/),
	isGecko3 = isGecko && check(/rv:1\.9/),
	isWindows = check(/windows|win32/),	
	isMac = check(/macintosh|mac os x/),	
	isAir = check(/adobeair/),	
	isLinux = check(/linux/),
		
	jQuery.extend(jQuery.browser, {
		isOpera: isOpera,
		isIE:	isIE,
		isIE6: isIE6,
		isIE7: isIE7,
		isIE8: isIE8,
		isChrome: isChrome,
		isWebKit: isWebKit,
		isSafari: isSafari,
		isSafari2: isSafari2,
		isSafari3: isSafari3,
		isSafari4: isSafari4,
		isGecko: isGecko,
		isGecko2: isGecko2,
		isGecko3: isGecko3,
		isWindows: isWindows,
		isMac: isMac,
		isAir: isAir,
		isLinux: isLinux
	});
})();

(function($) {
	jQuery.fn.gallery = function(settings){
		
		return this.each(function(){							  
			var itemWidth = 604;								  
			var itemCount = $('.gallery-ct .panel', this).length;
			var itemCt = $('.gallery-ct', this);
			var self = this;
			var timer = 0;
			var currentIndex = 0;
			$(itemCt).width(itemWidth * itemCount);
			
			
			$('.nav', this).append('<ul></ul>');
			
			$('.panel', itemCt).each(function(index){		
				$(this).attr('id', 'g-panel'+index);											  
				$('.nav ul', self).append('<li><a title="' + (index + 1) + '" href="#">' + (index + 1) + '</a></li>');													
			});
			$('.nav ul li:first-child', self).addClass('active');
			$('.nav ul li:last-child', self).addClass('last');
		
    		var selectNav = function(index) {
				$('.nav ul li', self).removeClass('active');
				var nItem = $($('.nav ul li', self)[index]);
				$(nItem).addClass('active');
				return false;
	    	}

			$('.nav ul li a', self).each(function(index){
				$(this).click(function(){
					clearTimeout(timer);
					currentIndex = index;
					selectNav(currentIndex);
					fn();
					return false;									   
				});												  
			});
			
			var fn = function(){
				$(itemCt).animate({
						'left': '-' + (currentIndex * itemWidth) + 'px'
					},
					1200,
					'swing',
					function(){
						play();
					}
				);
			}
			
			var play = function(){
				clearTimeout(timer);
				timer = setTimeout(function(){
					currentIndex++;
					if(currentIndex > itemCount -1){
						currentIndex = 0;
					}
					selectNav(currentIndex);
					fn();
				}, 5 * 1000);
			}
		
			play();
			

		});
	};		  
})(jQuery);		

(function($) {
	jQuery.fn.slideMenu = function(settings){
		
		return this.each(function(){									  							  
			var mHeight = $('ul', this).height();
			var mWidth = $('ul', this).width();


			if(mHeight){
				
				$('ul li', this).removeClass('x-menu-item');
				$('ul li a', this).removeClass('x-menu-text');
				
				 $(this).append('<img class="arrow" src="images/s.gif"/>');
				 $('ul', this).wrap('<div class="sub-menu-wrap"></div>');
				 $('ul', this).wrap('<div class="sub-menu"></div>');
				  $('ul', this).wrap('<div class="sub-menu-inner"></div>');
				  $('.sub-menu-inner', this).after('<div class="sub-menu-bg"></div>');
				  
				  mHeight = $('ul', this).height();
				  
				  $('.sub-menu-inner', this).width(mWidth).height(mHeight);
				  $('.sub-menu-bg', this).width(mWidth).height(mHeight + 12);
				  $('.sub-menu', this).width(mWidth).height(mHeight);
				 
				 $('ul', this).show();

				 $('.sub-menu-wrap', this).width($('.sub-menu', this).width() + 2);
				 $('.sub-menu', this).css({'top': '-' + (mHeight) + 'px'});
				 
				 $('ul li', this).hover(function(){
					$(this).addClass('sub-over');												 
				 }, function(){
					 $(this).removeClass('sub-over');	
				 });
				 
				 
			}
		
			var show = function(obj){
				$(obj).addClass('over');
				$('.sub-menu-wrap', obj).height(mHeight + 12);
				$('.sub-menu', obj).stop().animate({top: 0}, 450, 'swing');
			}
		
			var hide = function(obj){
				if(mHeight){
					$('.sub-menu', obj).stop().animate({top: '-' + (mHeight) + 'px'}, 450, 'swing', function(){
						$('.sub-menu-wrap', obj).height(0);
						$(obj).removeClass('over');
					});
				}
				else{
					$(obj).removeClass('over');
				}
			}
		
			$(this).hover(function(){
				show(this);
			}, function(){
				hide(this);
			});								  
		});
	};
})(jQuery);	

function carousel(){
	var speed = 1000;
	var easing = 'swing';
	var itemContainer = $('#testimonials .t-items');
	var headers = $('#testimonials .t-header li em a');
	var navLeft = $('#testimonials .nav-left');
	var navRight = $('#testimonials .nav-right');
	var wrap = $('#testimonials .t-body-ct');
	var totalWidth = 0;
	var itemLen = $('.panel-wrap', itemContainer).length;
	var currentIndex = -1;
	var timer = null;
	var iInterval = 5;
	var locked = false;
	var widths = [];
	
	$(itemContainer).append($('.panel-wrap', itemContainer).eq(0).clone());
	
	$('.panel-wrap', itemContainer).each(function(index){
		widths[index] = $(this).width();
		totalWidth += $(this).width();
	});	
	
	$(itemContainer).width(totalWidth);
	itemLen = $('.panel-wrap', itemContainer).length;
	
	var self = this;
	$(headers).each(function(index){
		$(this).hover(function(){
			$(this).addClass('over');							  
		}, function(){
			$(this).removeClass('over');
		});		
		
		$(this).click(function(){
			self.stop();							   
			self.switchItem(index);		
			return false;							   
		});
	});
	
	$(navLeft).attr('title', 'Left');
	$(navRight).attr('title', 'Right');
	
	$(navLeft).click(function(){
		self.stop();
		var index = currentIndex - 1;
		if(index < 0){
			index = itemLen - 2;
		}
		self.switchItem(index);	
	});

	$(navRight).click(function(){
		self.stop();
		var index = currentIndex + 1;
		if(index > itemLen - 1){
    		index = 0;
  		}		
		self.switchItem(index);	
	});
	
	
	
	this.getPos = function(index){
		var pos = 0;
		for(var i=0; i<index; i++){
			pos += widths[i];
		}
		return pos;
	}


	
	this.changeItem = function(index){
		currentIndex = index;
		$(itemContainer).stop();
		$(headers).removeClass('active').eq(index).addClass('active');
		if(index == itemLen - 1){
			$(headers).removeClass('active').eq(0).addClass('active');
		}
		var leftstr = '-' + (this.getPos(index)) + 'px';
		itemContainer.animate({
				'left':leftstr
				},
				speed,
				easing,
				function(){
					if(index == itemLen - 1){
						$(itemContainer).css('left', '0px');
						currentIndex = 0;
					}
				}
			);			
	}
	
	this.switchItem = function(index){
		currentIndex = index;
		$(itemContainer).stop();
		$(headers).removeClass('active').eq(index).addClass('active');
		if(index == itemLen - 1){
			$(headers).removeClass('active').eq(0).addClass('active');
		}
		var leftstr = '-' + (this.getPos(index)) + 'px';
		itemContainer.animate({
				'left':leftstr
				},
				speed,
				easing,
				function(){
					if(index == itemLen - 1){
						$(itemContainer).css('left', '0px');
						currentIndex = 0;
					}
					self.start();
				}
			);			
	}	
	
	this.stop = function(){
		clearTimeout(timer);
	}
	
	this.start = function(){
		var fn =function(self){
				return function(){
  					self.play();
				};
			};
		timer = setTimeout(fn(this),iInterval*1000);		
	}
	
	this.play = function(){
		if(locked){
			return false;
		}
		this.stop();
		currentIndex++;
		if(currentIndex > itemLen - 1){
    		currentIndex = 0;
  		}
  		this.changeItem(currentIndex);
		this.start();		
	}
	
	this.play();
}

if($.browser.isIE){	
	$.addStyle('css/stylefix.css');
}

$(document).ready(function(){
	$('#top-nav .x-menu-item:first-child ul').width(195);						   
	$('#top-nav .x-menu-item').slideMenu();
	if($('body').hasClass('home')){
		carousel();
	}	
	$('#footer #copy-right ul li:last-child, #footer #contact-info ul li:first-child').addClass('last');
	$('.txt').focus(function(){
		$(this).addClass('txt-focus');							 
	});
	$('.txt').blur(function(){
		$(this).removeClass('txt-focus');							 
	});	
	
	$('.blog').each(function(){
		$('.blogs-addcommentinput', this).eq(0).parent('form').wrap('<div class="comment-form-wrap"></div>');	
		$('.comment-form-wrap', this).each(function(){
			$('.blogs-submitinput', this).parent().prev().hide();	
			$('.blogs-authorinput, .blogs-commentinput', this).focus(function(){
				$(this).addClass('txt-focus');							 
			});
			$('.blogs-authorinput, .blogs-commentinput', this).blur(function(){
				$(this).removeClass('txt-focus');							 
			});	
			$('form', this).validate({
				rules: {
					'cntnt01author': {
						required: true
					},
					'cntnt01authoremail': {
						required: true,
						email: true
					},
					'cntnt01text': {
						required: true
					}
				},
				errorPlacement: function(error, element) {
					$(element).addClass('error');
				}					
			});
		});
	});

	
	$('#contact-form').each(function(){
		$('form', this).validate({
			rules: {
				name: {
					required: true
				},
				email: {
					required: true,
					email: true
				},
				company: {
					required: true
				},
				website: {
					required: true,
					url: true
				},
				message: {
					required: true
				}				
			},
			errorPlacement: function(error, element) {
				$(element).addClass('error');
			}			
		});									 
	});
	
});
