//set the animation speed default values:
var ANIMSPEED=1200, COLORANIMSPEED=1200;
var cssBlinkColors=new Array(), colorIntervalID, featureScrollID, diffBetwnScrollDivs;
var intervalId, intervalIdTV=0, curBnr_2=curBnr_tv=0;
var curBnr=3;
var featureScrollID, diffBetwnScrollDivs, featureDlWidth, scrollReverse=false, scrollSpeed=3;
var curPos=0, contactInt;

function contact_form_ready(){	
	//contact form element focus or blur event	
		jQuery('#si_contact_form2 input, #si_contact_form2 textarea').each(function() {
			var default_value = this.value;
			jQuery(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			jQuery(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		});		
		clearInterval(contactInt);	
}


jQuery(document).ready(function(){	
	
		autoScroller('vmarquee');
		
		contactInt=setInterval('contact_form_ready()',2000);		
		
		//faq slide show/hide	
	jQuery('.collapseCont > p').click(
			function(){
			if(jQuery(this).next().is(':hidden')){
				jQuery('.collapseCont').removeClass('activeCollapse');
				jQuery('.collapseInner').slideUp('slow');
				jQuery(this).next().slideDown('slow').parent().addClass('activeCollapse');
			}
			else{
				jQuery('.collapseInner').slideUp('slow');
				jQuery('.collapseCont').removeClass('activeCollapse');	
			}
		});
		//contact page tabing
		jQuery("ul.ContactTabs li").eq(0).addClass("activeTab");
		jQuery("dl.contactTabDetail dd").eq(0).css('display','block');
		jQuery("ul.ContactTabs li").click(
			function(){
				var crntIndex=jQuery("ul.ContactTabs li").index(this);
				var allBtns =jQuery("ul.ContactTabs li");
				var allTabs =jQuery("dl.contactTabDetail dd");
				
				jQuery(allTabs).fadeOut();
				jQuery('.mapCont').eq(crntIndex).html(jQuery('.mapCont').eq(crntIndex).html());
				jQuery(allTabs[crntIndex]).css('display','block');
				jQuery(allBtns).removeClass("activeTab");
				jQuery(this).addClass("activeTab");
			}
		);
			
		//Inner pages sidebar menu
		jQuery('.sideMenu li span').click(
			function(){			
			if(jQuery(this).next().is(':hidden')){
				jQuery('.sideMenu li ul').hide();
				jQuery(this).next().show();
			}
			else{
				jQuery('.sideMenu li ul').hide();	
			}
		});
		jQuery('.sideMenu li:first-child').css('border-top','0');
		jQuery('.sideMenu li:last-child').css('border-bottom','0');
		
		/*jQuery('.content:last-child').css('background','none');*/
		
       //position h1,h2,link
	   /*if(jQuery('.aboutInner').length > 0){
		   var aboutPos=jQuery('.aboutInner').offset().top;
		   var newPos=aboutPos;
		   jQuery('a.torontoLink').css('top',newPos+'px').show();
		   var newPos=newPos + 50;
		   jQuery('h1').css('top',newPos+'px').show();
		   var newPos=newPos+50;
		   jQuery('h2').css('top',newPos+'px').show();
		   var h2Left=jQuery('h2').offset().left;
		   var h2Width=jQuery('h2').outerWidth();
		   var newH2Width=h2Width-h2Left;
		   jQuery('h2').css('width',newH2Width+'px');
		  }*/
		  
	   //look for anchor
	   var curURL=window.location.href;
	   var urlArr=curURL.split('#');
	   var anc=urlArr[urlArr.length-1];
	   if(anc){
			if(anc=='web')	   
		   		pageChange(0);
			else if(anc=='mobile')	   
		   		pageChange(1);
			else if(anc=='contact')	   
		   		pageChange(2);
	   }
	   
	   
	   //testimonials rotator
	    playBnr();
	  
	  //testimonial popup
	  jQuery('a.showPop').click(function(){
			abcd(this);
		});
	   
	   
	   //disk hover
	   jQuery('.desk').children('div').hover(
			function(){
				jQuery(this).addClass('activeDesk');	
			},
			function(){
				jQuery(this).removeClass('activeDesk');	
			}
		);
	   
	   jQuery('.deskSubTittle').click(
			function(){
				jQuery(this).find('.popUp').stop(true,true).toggle();	
			}/*,
			function(){
				jQuery(this).find('.popUp').fadeOut();
			}	*/							 
										 
		);
	   
	 //client scroller
	 	/*	
			featureDlWidth=0;
			jQuery('.scrollX li').each(
				function(){
					featureDlWidth= featureDlWidth + jQuery(this).outerWidth();
				}
			);
		*/
	    featureDlWidth=(parseInt(jQuery('.scrollX li:first').outerWidth()) + 16) *jQuery('.scrollX li').length;
		diffBetwnScrollDivs=featureDlWidth-jQuery('.scrollInner').width();
		jQuery('.scrollX').css('width',featureDlWidth);
		if(diffBetwnScrollDivs > 0){
			diffBetwnScrollDivs -= diffBetwnScrollDivs % scrollSpeed;
			jQuery('.scrollOutter .btnLeft, .scrollOutter .btnRight').hover(
				function(){
					if(jQuery(this).is('.scrollOutter .btnLeft')){
						var sign = scrollReverse? '-' : '+';
						featureScrollID=setInterval('scrollFeature('+sign+'1)', 5);
					}
					else{
						var sign = scrollReverse? '+' : '-';
						featureScrollID=setInterval('scrollFeature('+sign+'1)', 5);
					}
				},
				function(){clearInterval(featureScrollID)}
			);
		}				
	   
	   
	   //portfilio scroller
	   featureDlWidth=(parseInt(jQuery('.galleryList li:first').outerWidth()) + 50) *jQuery('.galleryList li').length;
	   jQuery('.galleryList').css('width',featureDlWidth);
	   var allButnsLen=jQuery('ul.galleryList li').length;

        jQuery('#menu > ul > li').click(function(){
		var index=jQuery('#menu > ul > li').index(this);
		jQuery('.current-menu-item').removeClass('current-menu-item');
		jQuery('#menu li').eq(index).addClass('current-menu-item');
		
		pageChange(index);
	});

       jQuery('a#logo').click(function(){
           jQuery('.current-menu-item').removeClass('current-menu-item');
		  	
			var targetOffset = jQuery('#homeWrap').offset().top;
			jQuery('html,body').animate({scrollTop: targetOffset-128}, 1000);
			/*var wrapOffset =jQuery('#wrap_home').offset().top;
			targetOffset =targetOffset-wrapOffset-128;
			jQuery('#wrap_home').animate({top: -targetOffset+'px'}, 1000);*/
		  
		  /* var topPosss;
            topPosss=0;
            $('div.slideMe').css('display', 'block');
                $('#wrap_home').stop(true).animate({top:topPosss},ANIMSPEED*3/2,function(){
                //var new_index=index+1;
                $('div.slideMe:not(:eq(0))').css('display', 'none');
				
              });*/
	});

        jQuery('a.next').click(function(){
             curBnr++;
             //alert(allButnsLen);
            // alert(curBnr);

             if(curBnr > (allButnsLen))
                 {
                  curBnr=allButnsLen;
                 }
             else{
                  jQuery('ul.galleryList').stop(true).animate({"left": "-=309px"},ANIMSPEED*3/4,'');
             }
	});

        jQuery('a.pre').click(function(){
            curBnr--;
             if(curBnr < 3)
                 {
                    curBnr=3;
                 }
             else{
                 jQuery('ul.galleryList').stop(true).animate({"left": "+=309px"},ANIMSPEED*3/4,'');
             }
	});

	jQuery('.gallaryBox').hover(
		function(){
			if(jQuery(this).find('p').is(':hidden')){
				//jQuery('ul.galleryList li').find('p').slideDown();
				jQuery(this).find('h6').stop(true,true).slideUp();
                jQuery(this).find('p').stop(true,true).slideDown();
			}
			else{
				jQuery(this).find('h6').slideDown();
                jQuery(this).find('p').slideUp();

			}
		});
			
	//For google map tab		
	 //jQuery('.ContactTabs li a').click(function(){
	 	//alert(123);
		//ContactTab(this);
     //});
	 
	 
	//TV slider
	jQuery('.tv').click(
		function(){
			curBnr_tv=0;
			jQuery('ul.tvslider li').hide().eq(0).show();
			playBnrTV();
		}
	);
	jQuery('ul.tvslider li').click(
		function(){
			var slideLength=jQuery('ul.tvslider li').length;
			index=jQuery('ul.tvslider li').index(this);
			if(curBnr_tv > 2 && index < slideLength-1){
					jQuery('ul.tvslider li').hide();
					jQuery(this).next().show();
					curBnr_tv=index;
					pauseBnr();
					playBnrTV();
				}
			}
	)
	
 });

function pageChange(index){
    //alert(index);
    /*var topPos, bodyClass, menuBgColor;
    if(index==0) 	  {topPos='-1712%';}
    else if(index==1) {topPos='-2395%';}
    else if(index==2) {topPos='-3000%';}

    //animate:
    $('div.slideMe').css('display', 'block');
    $('#wrap_home').stop(true).animate({top:topPos},ANIMSPEED*3/2,function(){
    var new_index=index+1;
    $('div.slideMe:not(:eq('+new_index+'))').css('display', 'none');
	});*/
	
	
	if(index==0) 	  {topPos='webWrap';}
    else if(index==1) {topPos='mobileWrap';}
    else if(index==2) {topPos='contactWrap';}
	
	
	var targetOffset = jQuery('#'+topPos).offset().top;
	jQuery('html,body').animate({scrollTop: targetOffset-128}, 1000);
	/*var wrapOffset =jQuery('#wrap_home').offset().top;
	targetOffset =targetOffset-wrapOffset-128;
	jQuery('#wrap_home').animate({top: -targetOffset+'px'}, 1000);*/
	
	
  
}


function goWork(){
	var targetOffset = jQuery('#workWrap').offset().top;
	jQuery('html,body').animate({scrollTop: targetOffset-128}, 1000);	
	
}

// On window load. This waits until images have loaded which is essential
	jQuery(window).load(function(){

		// Fade in images so there isn't a color "pop" document load and then on window load
		jQuery(".scrollX img").fadeIn(500);

		// clone image
		jQuery('.scrollX img').each(function(){
			var el = jQuery(this);
			el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
				var el = jQuery(this);
				el.parent().css({"width":this.width,"height":this.height});
				el.dequeue();
			});
			this.src = grayscale(this.src);
			
		});

		// Fade image
		jQuery('.scrollX img').mouseover(function(){
			jQuery(this).parent().find('img:first').stop().animate({opacity:1}, 500);
		})
		jQuery('.img_grayscale').mouseout(function(){
			jQuery(this).stop().animate({opacity:0}, 500);
		});
		
		
	});

	// Grayscale w canvas method
	function grayscale(src){
		
		var canvas = document.createElement('canvas');
		var ctx = canvas.getContext('2d');

		var imgObj = new Image();
		imgObj.src = src;
		canvas.width = imgObj.width;
		canvas.height = imgObj.height;
		ctx.drawImage(imgObj, 0, 0);
		var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
		for(var y = 0; y < imgPixels.height; y++){
			for(var x = 0; x < imgPixels.width; x++){
				var i = (y * 4) * imgPixels.width + x * 4;
				var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
				imgPixels.data[i] = avg;
				imgPixels.data[i + 1] = avg;
				imgPixels.data[i + 2] = avg;
			}
		}
		
		
		ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
		
		
		return canvas.toDataURL();
    }
	
	
	//testimonials
	 function startBnrPlay(){
            var allButnsLen_2=jQuery('.testimonialList li').length;
            if(curBnr_2==(allButnsLen_2-1))
                    curBnr_2=0;
            else
                    curBnr_2++;
            changeBnr();
    }

    function changeBnr(){
                    jQuery('.testimonialList li').fadeOut("slow").eq(curBnr_2).fadeIn("slow");
    }

   function playBnr(){
        intervalId=setInterval('startBnrPlay()',7000)
   }





//TV slider

function playBnrTV(){
		if(intervalIdTV)
			pauseBnr();
			
		intervalIdTV=setInterval('startBnrPlayTV()',2000)
	}
function startBnrPlayTV(){
            if(curBnr_tv==2)
				document.getElementById('player').play();
			
			var slideLength=jQuery('ul.tvslider li').length;
			if(curBnr_tv < slideLength-1){
                curBnr_tv++;
            	changeBnrTV();
			}
    }
function changeBnrTV(){
                    jQuery('.tvslider li').hide().eq(curBnr_tv).show();
    }	
function pauseBnr(){clearInterval(intervalIdTV)}	
	





	
	function abcd(e){//alert(e.tagName);
		var email = jQuery(e).next('input').val();
		var send = 'yes';
		var data='send='+send+'&email='+email;
		   //alert(email);
	//start the ajax
		jQuery.ajax({
			//this is the php file that processes the data and send mail
			url: TemplatePath+'/ajax.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);
				jQuery('.import').html(html);
			}
		});
	 }
	 
	 

function scrollFeature(scrDirection){
	if(0 < curPos || curPos < -diffBetwnScrollDivs){
		clearInterval(featureScrollID);
		curPos = (0 < curPos)? 0 : -diffBetwnScrollDivs;
	}
	else if(!( (curPos==0 && scrDirection==1) || (curPos == -diffBetwnScrollDivs && scrDirection == -1) )){
		curPos+=(scrollSpeed * scrDirection);
		jQuery('.scrollX').css('left', curPos+'px');
	}
}



function xxxsendMail(){	
		//Get the data from all the fields
		var name = jQuery('input[name=name]');
		/*var companyname = jQuery('input[name=companyname]');
		var telephone = jQuery('input[name=telephone]');*/
		var email = jQuery('input[name=email]');
		/*var website = jQuery('input[name=website]');
		var budget = jQuery('input[name=budget]');
		var comment = jQuery('textarea[name=comment]');*/
		var comment = jQuery('textarea[name=comment]');
		/*var gfx_check = jQuery('input[name=gfx_check]');*/

		jQuery('.error').html('').hide();
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (name.val()=='') {
			name.addClass('hightlight');
			name.focus();
			jQuery(name).prev().find('span').html('Enter Your Name');
			
			//jQuery('.error').html('Enter Your Name').show();
			return false;
			
		} else {
			name.removeClass('hightlight');
			jQuery(name).prev().find('span').html('');
		}
		
		if (email.val()=='') {
			email.addClass('hightlight');
			email.focus();
			jQuery(email).prev().find('span').html('Enter Your Email');
			//jQuery('.error').html('Enter Your Email').show();
			return false;
		}else {
			jQuery(email).prev().find('span').html('');
			email.removeClass('hightlight');
		}
		
		if (!email.val().match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/))
		{
			//alert("Please enter a valid Email Address");
			email.addClass('hightlight');
			email.focus();
			jQuery(email).prev().find('span').html('Please enter a valid Email Address');
			//email.addClass('hightlight');
			//jQuery('.error').html('Enter a valid Email').show();
			return false;
		} else {
			jQuery(email).prev().find('span').html('');
			email.removeClass('hightlight');
		}
		
		

		
		if (comment.val()=='') {
			comment.addClass('hightlight');
			comment.focus();
			jQuery(comment).prev().find('span').html('Enter a message');
			//jQuery('.error').html('Enter a comment').show();
			return false;
		} else {
			jQuery(comment).prev().find('span').html('');
			comment.removeClass('hightlight');
			
		}
		
		/*if (gfx_check.val()=='') {
			gfx_check.addClass('hightlight');
			gfx_check.focus();
			jQuery('.error').html('Enter the security code').show();
			return false;
		} else gfx_check.removeClass('hightlight');
		*/
		//organize the data properly
		//var data = 'name=' + name.val() + '&companyname=' + companyname.val() + '&telephone=' + telephone.val() + '&website=' + website.val() + '&budget=' + budget.val() + '&email=' + email.val() + '&comment=' + comment.val() + '&gfx_check=' + encodeURIComponent(gfx_check.val());
		
		var data = 'name=' + name.val() + '&email=' + email.val() + '&comment=' + comment.val();
		
		//disabled all the text fields
		jQuery('.textsub').attr('disabled','true');
		
		//show the loading sign
		jQuery('.loading').show();
		
		//start the ajax
		jQuery.ajax({
			//this is the php file that processes the data and send mail
			url: TemplatePath+"/includes/process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html==-1) {
					jQuery('.error').html('Please Enter valid security code').show();
					gfx_check.addClass('hightlight');
					gfx_check.focus();
				}
				else if (html==1) {					
					//hide the form
					jQuery('.form').fadeOut('slow');					
					
					//show the success message
					//jQuery('.done').fadeIn('slow');
					//jQuery('.error').html('').hide();
					
					window.location.href='/thank-you';
					
				//if process.php returned 0/false (send mail failed)
				} else jQuery('.error').html('Message could not be sent. please try again.').show();//alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	}
	
	
	function clearFields(){
		document.feedback.reset();
		return false;
	}
	
function load(){
	/*document.forms['si_contact_form2'].elements["si_contact_email"].value='E-mail';	
	document.forms['si_contact_form2'].elements["si_contact_name"].value='Name';
	document.forms['si_contact_form2'].elements["si_contact_message"].value='Message';*/
}

function ContactTab(d){
	//var ind = jQuery('.ContactTabs li a').eq(index).attr('title');
	var ind = jQuery(d).children().html();;
	data = 'title='+ind;
	//start the ajax
	jQuery.ajax({
	//this is the php file that processes the data and send mail
	url: TEMPLATEPATH+'/ajax-2.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);
		jQuery('.contactTabDetail').html(html);
		//$('#avail').html(html);
	}
});

}


function sendMailCustom(){
	return sendMailCustomForm('contactBox1');	
}


function sendMailCustomForm(cls){
		var name = jQuery('.'+cls+' input[name=con_name]');
		var email = jQuery('.'+cls+' input[name=con_email]');
		var comment = jQuery('.'+cls+' textarea[name=con_sms]');
		var captcha = jQuery('.'+cls+' input[name=con_captcha]');
		
		if (name.val()=='') {
			jQuery(name).prev().show();
			return false;
		}
		else{
			jQuery(name).prev().hide();	
		}
		
		
		if (email.val()=='') {
			jQuery(email).prev().show();
			return false;
		}
		else{
			jQuery(email).prev().hide();	
		}
		
		if (!email.val().match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/))
		{
			jQuery(email).prev().show();
			return false;
		}
		else{
			jQuery(email).prev().hide();	
		}
			
			
		
		if (comment.val()=='') {
			jQuery(comment).prev().show();
			return false;
		}
		else{
			jQuery(comment).prev().hide();	
		}
		
		if (captcha.val()=='') {
			jQuery(captcha).prev().show();
			return false;
		}
		else{
			jQuery(captcha).prev().hide();	
		}
		
		
		
		//start the ajax
		jQuery.ajax({
			//this is the php file that processes the data and send mail
			url: TemplatePath+"/includes/captcha-test.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: 'val='+captcha.val(),		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				
				if(html==1){
					jQuery('.'+cls+' form').submit();
				}
				else{
					jQuery(captcha).prev().show();	
				}
					
			}		
		});
		
		
		return false;
}


function sendMailCustomInner(){
		var name = jQuery('.footerIn input[name=con_name]');
		var email = jQuery('.footerIn input[name=con_email]');
		var comment = jQuery('.footerIn textarea[name=con_sms]');	
		var captcha = jQuery('.footerIn input[name=con_captcha]');
		
		if (name.val()=='Name') {
			jQuery(name).css('border','1px solid red');
			return false;
		}
		else{
			jQuery(name).css('border','1px solid #c7c7c7');
		}
		
		
		if (email.val()=='E-mail') {
			jQuery(email).css('border','1px solid red');
			return false;
		}
		else{
			jQuery(email).css('border','1px solid #c7c7c7');	
		}
		
		if (!email.val().match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/))
		{
			jQuery(email).css('border','1px solid red');
			return false;
		}
		else{
			jQuery(email).css('border','1px solid #c7c7c7');	
		}
			
			
		
		if (comment.val()=='Message') {
			jQuery(comment).css('border','1px solid red');
			return false;
		}
		else{
			jQuery(comment).css('border','1px solid #c7c7c7');
		}
		
		if (captcha.val()=='Security Code') {
			jQuery(captcha).css('border','1px solid red');
			return false;
		}
		else{
			jQuery(captcha).css('border','1px solid #c7c7c7');
		}
		
		//start the ajax
		jQuery.ajax({
			//this is the php file that processes the data and send mail
			url: TemplatePath+"/includes/captcha-test.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: 'val2='+captcha.val(),		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				
				if(html==1){
					jQuery('.footerIn form').submit();
				}
				else{
					jQuery(captcha).css('border','1px solid red');
				}
					
			}		
		});
		
		
		return false;
}
