var slideSpeed = 400;
var fadeSpeed = 300;
var randomnumber=Math.floor(Math.random()*10000000)
var regex_email = new RegExp((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/));
var error = "";

//----------------------------------------------	
//  Preload Images Code
//----------------------------------------------	
$(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
      var args_len = arguments.length;
      for (var i = args_len; i--;) {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        cache.push(cacheImage);
      }
    }
});
//----------------------------------------------	
//  EOF Preload Images Code
//----------------------------------------------	

$(document).ready(function() {		

//----------------------------------------------	
//  Navigation Code		
//----------------------------------------------	
		function changeCurrent(current){
			if ( $('.current').attr("id") != current ){
  			$('.current').animate({
            right: - ( $('#'+current).width() + 40 )
          }, slideSpeed, 'linear', function(){
    		$(".current").removeClass("current"); 
    		$('#'+current).animate({
              	right: 0 
      		}, slideSpeed, 'linear', function(){
						$('#'+current).addClass("current"); 
					});
      	});
			}
		}
		
    $('.menu a').click(function(){
				current = $(this).attr("id").substr(0,$(this).attr("id").length -5);
				$('.nav_active').removeClass("nav_active"); 
				$(this).parent().addClass("nav_active"); 
				
				changeCurrent(current);
     		return false;
    });
			
		
		
		//fix for IE on load
		$('#home-link').css('color', '#b1ba09');
		$('#portfolio-link').css('color', '#60b637');
		$('#contact-link').css('color', '#0099ff');
		
		//additional navigation links througout the page
		$('#logo').click(function(){changeCurrent("home");return false;});
		
		$('.link_portfolio').click(function(){changeCurrent("portfolio");return false;});
//----------------------------------------------	
//  EOF navigation Code
//----------------------------------------------
	
	//call preload images function
	$.preLoadImages("/images/portfolio/lwbc.jpg", "/images/portfolio/icp.jpg", "/images/portfolio/fratalli2.jpg", "/images/portfolio/asa.jpg","/images/portfolio/isupply.jpg","/images/body_2.png","/images/body_3.png","images/body_4.png","/images/body_5.png","images/body_6.png");

	//call preload content function
	//$.loadContent("portfolio.php", "contact.php");
	$('#home').load('/home.php?q='+randomnumber,function(){
//----------------------------------------------	
//  Home Page Tabs Code
//----------------------------------------------	
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	$('.link_portfolio').click(function(){changeCurrent("portfolio");return false;});
	
	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		
		//IE6 png transparency
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			$("ul.tabs li").css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/trans_bg.png', sizingMethod='scale')");
			$("ul.tabs li").css("background-image","url(/images/blank.gif)");
			$(this).css("filter","none");
  		$(".tab_container").css("background-image","url(/images/"+$(this).attr("id")+".gif)");
		}else{
			$(".tab_container").css("background-image","url(/images/"+$(this).attr("id")+".png)");
		}
		var activeTab = $(this).find("a").attr("title"); //Find the href attribute value to identify the active tab + content
		$("#"+activeTab).show(); //Fade in the active ID content
//----------------------------------------------	
//  EOF Home Page Tabs Code
//----------------------------------------------	
	});
	
	$('#contact').load('/contact.php?q='+randomnumber, function(){
//----------------------------------------------	
//  Contact Us Validation
//----------------------------------------------	
		$("#ContactForm").submit(function(){
  	  var validate = true;
			$('#ContactForm').children().each(function(){
         if ( $(this).attr("title") == "required" && $(this).attr("value") == "" ){
				 		$(this).click(function(){$(this).css("border-color","#ccc");});
						$(this).css("border-color","#f00");
						if (validate) error = "Please Fill Out All Required Fields";
						validate = false;
				 }
				 if ( $(this).attr("name") == "Email" ){
						if ( !regex_email.test($(this).attr("value")) ){
							$(this).click(function(){$(this).css("border-color","#ccc");});
							$(this).css("border-color","#f00");
							if (validate) error = "Please Enter a Valid Email Address";
							validate = false;
						}
				 }
      });
			if ( validate == false ){
				$("#required").css("display","inline");
				$("#required").html(error);
				return false;
			}
		});
//----------------------------------------------	
//  EOF Contact Us Validation
//----------------------------------------------	
	});
	$('#portfolio').load('/portfolio.php?q='+randomnumber,function(){
//----------------------------------------------		
//  Portfolio Code
//----------------------------------------------	
		port_floor = 0;
		port_ceiling = ( 475 - $('.port > li > .port_link').size()*95 );

		function portScroll(position){
			var moveTo = parseInt($('.port').css('left').replace('px','')) + position;
			
			(moveTo > port_ceiling) ? moveTo = moveTo : moveTo = port_ceiling;
			(moveTo < port_floor) ? moveTo = moveTo : moveTo = port_floor;
			
			$('.port').animate({left: ( moveTo ) }, 100, 'linear');
		}
		
		$('#port_r').click(function(){
			portScroll(-190);
		});
		$('#port_l').click(function(){
			portScroll(190);
		});
		
		$('.port_link').hover(function(){
			$(this).addClass("active"); 
		},function(){
			if ( $(this).attr("class") != 'port_link port_link_active' ){
				$(this).removeClass("active"); 
			}
		});
		
		$('.port_link').click(function(){
			current = '#'+$('.port_link_active').attr("alt");
			current2 = '#'+$(this).attr("alt");
			$('.port_link_active').removeClass("port_link_active");
			$(this).addClass("port_link_active"); 
			$(current).fadeOut(fadeSpeed,function(){
				$(current2).fadeIn(fadeSpeed,function(){
					$(current).removeClass("port_client_active");
  				$(current2).addClass("port_client_active");
  			});
			});
		});
//----------------------------------------------
//  EOF Portfolio Code
//----------------------------------------------
	});
		return false;
	});
});

//----------------------------------------------
//  Google Analytics Asynchronous Tracking
//----------------------------------------------
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7235845-4']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

