

function validateEmail(email) { 
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ;
	return email.match(re);
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE TOGGLE
----------------------------------------------------------------- */
jQuery.fn.slideToggle = function(speed, easing, callback) {
	return this.animate({width: 'toggle',opacity: 'toggle'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE IN
----------------------------------------------------------------- */
jQuery.fn.slideIn = function(speed, easing, callback) {
	return this.animate({width: 'show',opacity: 'show'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE OUT
----------------------------------------------------------------- */
jQuery.fn.slideOut = function(speed, easing, callback) {
	return this.animate({width: 'hide',opacity: 'hide'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- ANIMATING BACKGROUNDS
----------------------------------------------------------------- */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);
/* -----------------------------------------------------------------
	JQUERY GERENRL FUNCTIONS
----------------------------------------------------------------- */
jQuery(document).ready(function() {
	
	jQuery(document).pngFix();
    
	// LAST CHILD : ADD FIRST / LAST CLASSES
	jQuery("li:last-child").addClass("last");
    jQuery("li:first-child").addClass("first");
	
	// ODD CHILD : ADD ODD CLASS
	jQuery("ul li:odd").addClass("odd");
	// EVEN CHILD : ADD EVEN CLASS
	jQuery("ul li:even").addClass("even");
	
	
	/* Enable product thumbs */
	jQuery('a.thumb').each(function(i) {
		//don't do this to homepage featured items!
		if($(this).closest(".featured").length==0){
			jQuery(this).bind('click', function() {
				var id = jQuery(this).attr('id').split('-');
				id = id[id.length-1];
			
				if((id - 0) == id && id.length > 0) {
					id = '-'+id;
				} else {
					id = '';
				}

				var src = jQuery('img', this).attr('src').replace(/-sm/, '-med');
				var href = jQuery(this).attr('href');
				var alt = jQuery('img', this).attr('alt');
				var title = jQuery(this).attr('title');

				jQuery('#img-src'+id).attr('src', src);
				jQuery('#img-src'+id).attr('alt', alt);
				jQuery('#img-href'+id).attr('href', href);
				jQuery('#img-href'+id).attr('title', title);

				return false;
			});
		}
	});

	/* set HOVER class */
	jQuery("li").hover(function () { 
		jQuery(this).addClass('hover');
	}, function () {
		jQuery(this).removeClass('hover');
	});
	
	/* set NAVIGATION hover */
	jQuery('#navigation ul.level-0 > li > a').css({
		backgroundPosition: "100% -109px"
	});
	jQuery('#navigation ul.level-0 > li')
	.mouseenter(function(){
		jQuery(this).children('a').stop().animate({backgroundPosition:"(100% -153px)"}, {duration:300});
	})
	.mouseleave(function(){
		jQuery(this).children('a').stop().animate({backgroundPosition:"(100% -109px)"}, {duration:300});
	});
	
	jQuery('.shiploader').cycle({delay:  2000, speed:  1000}); 
	
	//header banner
	var banner=$("#banner ul"),
		bannerAnimSpeed=1000,
		bannerAnimDelay=6000;
	function slideInNext(){
		var cIndex=parseInt(banner.attr('data-index')),
			count=banner.children().length,
			nIndex=(cIndex+1==count ? 0 : cIndex+1);
		//slide out the current banner
		if(cIndex!=-1)
			banner.children().eq(cIndex).animate({
				'left':-200},bannerAnimSpeed);
		
		//slide in the new banner
		banner.children().eq(nIndex).animate({
			'left':900},0,function(){
				$(this).animate({
					'left':392},bannerAnimSpeed);
			});
			
		//set the index
		banner.attr("data-index",nIndex);
		
		//do it again!
		setTimeout(function(){slideInNext()},bannerAnimDelay);
	}
	//initialize
	banner.attr("data-index","-1");
	banner.children().css("left",900);
	slideInNext();
	
	var featItems=$(".featured .items .thumb"),
		featIndex=0;
	if(featItems.length){
		featItems.each(function(){
			//homepage featured items overlay opacity for ie
			$(this).find(".overlay .text").animate({'opacity':0.8},0);
			/*$(this).hover(function(){
				$(this).find(".overlay").stop().animate({'opacity':0},0,function(){
					$(this).animate({'opacity':0.9},300)
				});
			},function(){
				$(this).find(".overlay").stop().animate({'opacity':0.9},0,function(){
					$(this).animate({'opacity':0},300)
				});
			});*/
			//initialize colorbox for featured projects
			$(this).attr("rel","featured_project").parent().find(".popup").wrapInner('<div id="featuredPopup_'+featIndex+'" class="featuredPopup" />');
			$(this).colorbox({width:520, inline:true, href:"#featuredPopup_"+featIndex});
			featIndex++;
		});
	};
	
	
	
	//project map left hover fixer!
	var mapItems=$("#project_map ul.world li a"),
		mapWidth=685,
		hoverWidth=250;
	if(mapItems.length){
		mapItems.each(function(){
			if(parseInt($(this).css("left").replace("px",""))>mapWidth-hoverWidth)
				$(this).addClass('left');
		});
	};
	
	//product share resize to fit
	$('.share-popup').livequery(function(){
			//find content height
			var newHeight=$(this).outerHeight();
			//max window height
			if(newHeight>$(window).height()-100)
				newHeight=$(window).height()-100;
			//set new height
			$('#TB_ajaxContent').css({'height':newHeight});
			$('#TB_window').css({'margin-top':-(newHeight+45)/2});
		},function(){});
	
	//product share form submit
	var shareFormSubmit=function(e){
				e.preventDefault();
				//validate fields
				var $this=$('form.share-with-friend'),validated=true;
				$this.find('input.input').each(function(){
					if((this.name.substr(this.name.length-5,5)=='email' && !validateEmail(this.value))
						|| this.value==''){
							$(this).addClass('error');
							validated=false;
						}else{ $(this).removeClass('error'); }
				});
				if(!validated) return false;
				//post the form
				$.post($this.attr('action'),$this.serialize(),function(data){
						if(data=='success'){
							$this.replaceWith('<div class="form-status pass clear">Your message was successfully sent!<br><a href="javascript:void(0);" onclick="tb_remove();">Close</a></div>');
						}else{ $this.prepend('<div class="form-status fail clear">Your message failed to send. Please try again later.</div>'); }
					});
			}
	//for most browsers
	$('form.share-with-friend').live('submit',shareFormSubmit);
	//for ie browsers
	$("form.share-with-friend button[type=submit]").live("click",shareFormSubmit);
	
});





