/*
 * http://jquery.com/
 */

var last_loaded_page="home";
 
$(document).ready(function() {
	slider1(2500, 680); // set delay between the automatic slider and width of photos 
});

function slider1($secs, $photo_wth) {
	var isClicked = 1;
	
	$addC = $(".itemidx.current").length;
	if($addC == 0) {		
		$(".itemidx:first").addClass("current");	
	}
	
	$allPort = ($(".itemidx").length) - 1;	$wthDivide = 935 - $photo_wth;	$tileWth = parseInt($wthDivide / $allPort);	
	$caption_wth = $photo_wth - 63;	
	$caption_wth = $caption_wth + 'px';	
	$photo_wth = $photo_wth + 'px';
	$(".itemidx").css("width", $tileWth);	
	$(".itemidx.current").css("width", $photo_wth);	
	$(".caption").css({ width: $caption_wth, opacity: 0 });	
	if(typeof(isClicked) == 'undefined') { 
		var isClicked = 0; 
	}
	$(".itemidx").click(function() {
		isClicked = 1;	
		var classClicked = $(this).attr('class');	
		
		var load_page="";
		if (this.getAttribute('id')=="d1") load_page="home";
		if (this.getAttribute('id')=="d2") load_page="aboutus";
		if (this.getAttribute('id')=="d3") load_page="services";
		if (this.getAttribute('id')=="d4") load_page="portfolio";
		if (this.getAttribute('id')=="d5") load_page="contactus";
		
		if((classClicked !== 'itemidx current')||(load_page!=last_loaded_page)) {
			if(classClicked !== 'itemidx current') {
				$(".itemidx.current").removeClass("current").addClass("prev").stop().animate({ width: $tileWth }, 630);	
				$(this).addClass("current").css("width", $tileWth).stop().animate({ width: $photo_wth }, 730);	
				$(".itemidx.prev").removeClass("prev");
				
				if (load_page=="home") div="1";
				if (load_page=="aboutus") div="2";
				if ((load_page=="services")||(load_page=="applications")) div="3";
				if (load_page=="portfolio") div="4";
				if (load_page=="contactus") div="5";
				for (i=1;i<6;i++) {
					if (document.getElementById("a"+i)) {
						document.getElementById("a"+i).className="";
					}
				}
				if (document.getElementById("a"+div)) {
					document.getElementById("a"+div).className="active";
				}
			}
			getPageContent(load_page);
			last_loaded_page=load_page;
		}	
	}
	);
	if(typeof(isHovered) == 'undefined') {
		var isHovered = 0; 
	}	
	$(".itemidx").hover(function() {	
		isHovered = 1;	
	}, function() {	isHovered = 0;	});	
	
	setInterval(function() {	
		if(isHovered == 0 && isClicked == 0) {	
			nextItm('680px', $tileWth);	
		}	
	}, $secs);
}

function nextItm($photo_wth, $tile_width) {
	$current = $(".itemidx.current");
	$next = $(".itemidx.current").next();	
	if($next.attr('class') == undefined) {	
		$next = $(".itemidx:first");	
	} else {
	}	
	$current.removeClass("current").css("width", $photo_wth).stop().animate({ width: $tile_width }, 630);	
	$next.addClass("current").css("width", $tile_width).stop().animate({ width: $photo_wth }, 730);
}

function getPageContent(page) {
	div=document.getElementById("maincontent");
	if (div) {
		div.innerHTML="<center><img src='images/indi.gif'></center>";
	}
	
	$.ajax({
		type: 'get',
		url: page+'.html',
		dataType: 'html',
		data: {},
		success: function(data){
			div=document.getElementById("maincontent");
			if (div) {
				div.innerHTML=data;
				setupZoom();
			}
		},
		error: function(){
		},
		complete: function(){
		}
	});
}

function openPage(page) {
	var div="";
	if (page=="home") div="1";
	if (page=="aboutus") div="2";
	if ((page=="services")||(page=="applications")) div="3";
	if (page=="portfolio") div="4";
	if (page=="contactus") div="5";
	if (div!="") {
		object_div=document.getElementById("d"+div);
		if (object_div) {
			var classClicked = $(object_div).attr('class');	
			if((classClicked !== 'itemidx current')||(page!=last_loaded_page)) {
			
				if(classClicked !== 'itemidx current') {
					for (i=1;i<6;i++) {
						if (document.getElementById("a"+i)) {
							document.getElementById("a"+i).className="";
						}
					}
					if (document.getElementById("a"+div)) {
						document.getElementById("a"+div).className="active";
					}
					$tileWth="63";
					$photo_wth="680px";
					$(".itemidx.current").removeClass("current").addClass("prev").stop().animate({ width: $tileWth }, 630);	
					$(object_div).addClass("current").css("width", $tileWth).stop().animate({ width: $photo_wth }, 730);	
					$(".itemidx.prev").removeClass("prev");
				}
				getPageContent(page);
				last_loaded_page=page;
			} 
		}
	}
}

function sendContactForm() {
	error=false;
	error_text="";
	if (document.contactform.txtName.value=='') {
		error=true;
		if (error_text!='') error_text+=", ";
		error_text+="Your name";
	}
	if (document.contactform.txtCompanyName.value=='') {
		error=true;
		if (error_text!='') error_text+=", ";
		error_text+="Company Name";
	}
	if (document.contactform.companySize.value=='') {
		error=true;
		if (error_text!='') error_text+=", ";
		error_text+="Company Size";
	}
	if (document.contactform.txtIndustry.value=='') {
		error=true;
		if (error_text!='') error_text+=", ";
		error_text+="Industry";
	}
	if (document.contactform.txtTelephone.value=='') {
		error=true;
		if (error_text!='') error_text+=", ";
		error_text+="Telephone";
	}
	var reg = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)");
	if (!reg.test(document.contactform.txtEmail.value)) {
		error=true;
		if (error_text!='') error_text+=", ";
		error_text+="Your Email";
	}
	if (document.contactform.budget.value=='') {
		error=true;
		if (error_text!='') error_text+=", ";
		error_text+="Budget";
	}
	if (document.contactform.txtMessage.value=='') {
		error=true;
		if (error_text!='') error_text+=", ";
		error_text+="Message";
	}
	if (error){
		alert("Please fill the following fields: "+error_text+".");
		return false;
	}
			
	$.ajax({
		type: 'post',
		url: 'contactus.php',
		dataType: 'html',
		data: {txtName:document.contactform.txtName.value, 
				txtCompanyName:document.contactform.txtCompanyName.value,
				companySize:document.contactform.companySize.value,
				txtIndustry:document.contactform.txtIndustry.value,
				txtTelephone:document.contactform.txtTelephone.value,
				txtEmail:document.contactform.txtEmail.value,
				budget:document.contactform.budget.value,
				txtMessage:document.contactform.txtMessage.value
			  },
		success: function(data){
			div=document.getElementById("maincontent");
			if (div) {
				div.innerHTML=data;
				setupZoom();
				last_loaded_page="contactthankyou";
			}
		},
		error: function(){
		},
		complete: function(){
		}
	});
	div=document.getElementById("maincontent");
	if (div) {
		div.innerHTML="<center><img src='images/indi.gif'></center>";
	}
}

