/*
Coded: reza@eSoftArena.com
*/

//set the animation speed default values:
var eyesOnAfterTimeMin=100, eyesOnAfterTimeMax=400;
var eyesOffAfterTimeMin=100, eyesOffAfterTimeMax=2500;
var eyesOnOffTime, twitterBlinkEyesID, featureDlWidth;
var ANIMSPEED=1200, COLORANIMSPEED=1200;
var cssBlinkColors=new Array(), colorIntervalID, featureScrollID, diffBetwnScrollDivs;

//returns a random integer between & including min to max:
function randomTime(min, max){return Math.floor(Math.random() * (max - min + 1)) + min}

$(document).ready(function(){
	
	//initialize:
	$('ul.menu_list').width($('ul.menu_list > li').outerWidth()*$('ul.menu_list > li').length);
	
	//Hover:
	$("ul.menu_list > li").hover(
		function(){
			if($.browser.msie && $.browser.version <=6)	$(this).addClass('hover');
			$(this).stop(true,true).queue(function(){
				$(this).height('auto').queue(function(){
					if($(this).height() < $(this.parentNode).height())
						$(this).height(''); //to get min-height;
					else{
						var high=$(this).outerHeight();
						$(this).height('').stop(true,true).animate({height:high});
					}
					$(this).dequeue();
				});
				$(this).dequeue();
			});
		},
		function(){
			if($.browser.msie && $.browser.version <=6) $(this).removeClass('hover');
			$(this).stop(true,true).animate({height: $(this.parentNode).css('height')});
		}
	);
	
	$("a.img_box").hover(
		function(){$(this).append('<em></em>')},
		function (){$(this).find('em').remove()}
	);
	
	if($.browser.msie && $.browser.version <=6){
		$("ul#top_nav li b").hover(
			function(){$(this).addClass('hover')},
			function(){$(this).removeClass('hover')}
		);
	}
	
	//$(".txt_area").focus(function(){$(this).empty()}).blur(function(){$(this).text('Message')});
	
	$('#top_nav > li > b').click(function(){
		pageChange($('#top_nav > li > b').index(this));
	});
	
	$('#menu_box .menu_arrowButn, #menu_box > h2').click(function(){
		$('#menu_box').stop(true)
			.animate({width: $('#menu_box').hasClass('menuOpened')? 0 : $('ul.menu_list').width()},ANIMSPEED*2/3)
			.toggleClass('menuOpened');
	});
	
	//LOAD CSS-COLORs:
	for(var i=0; i < $('#services h3').length; i++){
		cssBlinkColors[i]=$('#services h3:eq('+i+') a').css('color');
	}//alert(cssBlinkColors);
	colorAnim();
	
	//twiter eyes blink:
	twitterBlinkEyes();
	
	//DRAG and DROP:
	$('#services h3')
		.hover(
			function(){$(this).append('<small class="handle">Drag Me</small>')},
			function(){$(this).find('small.handle').remove()}
		)
		.each(function(){$(this).css('width',$(this).width())})
		.bind('dragstart',function( event ){
			if ( !$(event.target).is('.handle') ) return false;
			$( this ).addClass('active');
		})
		.bind('drag',function( event ){
			$( this ).css({
				top: event.offsetY,
				left: event.offsetX
				});
		})
		.bind('dragend',function( event ){
			$( this ).removeClass('active');
		});
	
	
	featureDlWidth=$('#featured_Box .featuredIn dl dd:first').outerWidth()*$('#featured_Box .featuredIn dl dd').length;
	diffBetwnScrollDivs=featureDlWidth-$('#featured_Box .featuredIn').width(); //alert(diffBetwnScrollDivs);
	$('#featured_Box .featuredIn dl').css('width',featureDlWidth);
	if(diffBetwnScrollDivs > 0){
		$('#featured_Box .butnPre, #featured_Box .butnNext').hover(
			function(){
				if($(this).is('#featured_Box .butnNext')){
					featureScrollID=setInterval('scrollFeature(-1)',5);}
				else
					featureScrollID=setInterval('scrollFeature(+1)',5);
			},
			function(){clearInterval(featureScrollID)}
		);
	}
	
	
	$('#brand_Box').css('margin-top', parseInt($('body').innerHeight())*10/100);
	//alert($('#brand_Box').css('margin-top'));
	//alert($('body').innerHeight());
	
	//restyle overlay
	if($('#overlay').length > 0){
		var  height=$('body').height();
		var  width=$('body').width();
		$('#overlay').css('opacity',0.5).css('height',height+'px').css('width',width+'px');	
	}
	
 


	//custom select box
	if( $('.slct_box').length > 0 ) $('.slct_box').selectbox();

	//redirect to index page:
	if($("body").hasClass("portfolio"))
		pageChange(1, false);
	else if($("body").hasClass("quote"))
		pageChange(2, false);
 });

var curPos=0;
function scrollFeature(direction){
	if(0 < curPos || curPos < -diffBetwnScrollDivs){
		clearInterval(featureScrollID);
		if(0 < curPos){ 
			curPos=0; 
		}
		else{ 
			curPos = -diffBetwnScrollDivs;
			$('#featured_Box .butnNext').addClass('butnDisabled');
		}
	}
	else if(!(curPos==0 && direction==1)){
		curPos+=(4*direction);
		$('#featured_Box .featuredIn dl').css('left', curPos+'px');
		$('#featured_Box .butnNext').removeClass('butnDisabled');
		$('#featured_Box .butnPre').removeClass('butnDisabled');
	}
	else if(curPos==0){
		$('#featured_Box .butnPre').addClass('butnDisabled');		
	}
}

function pageChange(index, NOANIM){
	var topPos, bodyClass, menuBgColor;
	if(index==0) 	  {topPos='0%';		menuBgColor='#941b6a';	bodyClass='home'; colorAnim()}
	else if(index==1) {topPos='100%';	menuBgColor='#1b3061';	bodyClass='portfolio'; clearInterval(colorIntervalID)}
	else if(index==2) {topPos='-100%';	menuBgColor='#14467f';	bodyClass='quote'; clearInterval(colorIntervalID)}
	
	//additional jobs:
	$('body').attr("class", bodyClass);
	$('#top_nav > li').removeClass('pgSlct').eq(index).addClass('pgSlct');
	
	//animate or not:
	if(NOANIM===undefined){ //easeInOutBack/easeOutBounce/easeOutElastic
		$('#wrap_home').stop(true).animate({top:topPos},ANIMSPEED*3/2,'easeInOutBack');
		$('#menu_box h2').animate({backgroundColor: menuBgColor},ANIMSPEED*2);
	}
	else{
		$('#wrap_home').stop(true).css({'top':topPos});
		$('#menu_box h2').css({'background-color': menuBgColor});
	}
}

function colorAnim(){
	colorIntervalID=setInterval(function(){
		$('#services h3 a').stop(true).each(function(index){
			$(this).animate({color: 'rgb(148,27,106)'},COLORANIMSPEED,function(){
				$(this).animate({color: cssBlinkColors[index]},COLORANIMSPEED);
			});
		});
	}, COLORANIMSPEED*2+200);
}


function twitterBlinkEyes(){
	clearInterval(twitterBlinkEyesID);
	if($('a#twitter').parent().hasClass('twitterBlinkOff')){
		$('a#twitter').parent().removeClass('twitterBlinkOff');
		eyesOnOffTime = randomTime(eyesOffAfterTimeMin, eyesOffAfterTimeMax);
	}
	else{
		$('a#twitter').parent().addClass('twitterBlinkOff');
		eyesOnOffTime = randomTime(eyesOnAfterTimeMin, eyesOnAfterTimeMax);
	}
	twitterBlinkEyesID=setInterval('twitterBlinkEyes()', eyesOnOffTime);
}
function display_num(id){
		alert(id);
		display_ajaxCall(id);
		
}
function display_ajaxCall(id){

	var data = 'id='+id;
	
	//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: 'show_num.php',	
			
			//POST method is used
			type: "POST",
			//pass the data			
			data: data,		
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {				
				//alert(html);
				$('#avail').html(html);
			}		
		});
}

function showPro(id){

		proj_ajaxCall(id,0);
		//alert(id);
}

function showProHome(id){

		proj_ajaxCall(id,1);
		//alert(id);
}



function proj_ajaxCall(id,t){
	var str ='<span class="pop_close" onclick="closePop()"></span><span class="pop_butns"></span><ul id="portfolio_popIn"><li><div id="ajax_loader"><img src="images/ajax-loader.gif" alt="" /></div></li></ul>';
	
	if(t==1){
		var h=$(window).height()-50;
		$('#portfolio_popIn').css('height',h+'px').css('overflow','auto');	
	}
	$('#portfolio_pop').html(str);
	$('#overlay').fadeIn();
	$('#portfolio_pop').fadeIn();

	var data = 'id='+id;
	//alert(id);
	//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: 'ajax.php',	
			
			//POST method is used
			type: "POST",
			//pass the data			
			data: data,		
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {				
				//
				$('#portfolio_pop').html(html);
				$('#portfolio_popIn li .proCont').eq(0).fadeIn();
				if(t==1){
					var h=$(window).height()-70;
					$('#portfolio_popIn').css('height',h+'px').css('overflow','auto');	
				}

				
				//project details
					if($('.pop_butns').length > 0 ){
						$('.pop_butns a').click(
							function(){
				
								var index=$('.pop_butns a').index(this);
								$('#portfolio_popIn li .proCont').hide();
								$('#portfolio_popIn li .proCont').eq(index).fadeIn(1000);
								$('.pop_butns a').removeClass('slctpop');
								$(this).addClass('slctpop');
							}
						);
					}
			}		
			
		});
}

function closePop(){
	$('#portfolio_pop').html('');
	$('#overlay').fadeOut();
	$('#portfolio_pop').fadeOut();	
}

function fieldcheck(){
	//alert('alert');	
	document.getElementById('errMsg').style.display = 'none';
	//document.getElementById('sucMsg').style.display = 'none';
	//alert(1);
		var name = trim(document.getElementById('name').value);
		var message = trim(document.getElementById('message').value);
		var email = trim(document.getElementById('email').value);
		
		
		if( name==''  ||  name=='Your Name (Company Name)' || email=='' || email=='Your Email Address (Web Address)' ||  message==''  ||  message=='Message'){
			//alert('Please complete the required fields (Name, Email,Telephone).');
			document.getElementById('errMsg').innerHTML = 'Please complete the required fields (Name, Email, Message).';
			document.getElementById('errMsg').style.display = 'block';
			return false;
		}
		
		else if( !checkEmail(email) ){
			//alert('Please enter a valid email address.');
			document.getElementById('errMsg').innerHTML = 'Please enter a valid email address.';
			document.getElementById('errMsg').style.display = 'block';
			return false;
			}
		
	
  }




function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function checkEmail(emial){
	var str=emial;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		return true;
	else
		return false;
}