/***********************************************************************
	ONLOAD FUNCTION
************************************************************************/

$(document).ready(function() {

	$("#aboutSlider").adjustHeight();
	$("#servicesSlider").adjustHeight();
	$("#creativeSlider").adjustHeight();
	$("#newsSlider").adjustHeight();

	// SCROLL TO FUNCTION
	var $menu = $("#navPrimary");
	var $menuLinks = $("#navPrimary a");
	
	$menuLinks.click(scrollPage);
		
	function scrollPage(e) {
		e.preventDefault(e);
		var whereTo = $(this).attr("href");
		var targetLink = whereTo.substring(1);
		$menuLinks.removeClass("active");
		$(this).addClass("active");
		$.scrollTo( $("#"+targetLink), 1000, {axis: 'y', queue:true, easing: '',  
			onAfter:function(whereTo) {
				setTimeout(function() {
					window.location.hash = targetLink;
					hash = window.location.hash;
				}, 100);
			}
		});
	}
	
	// MENU SLIDER FUNCTION
	menuSlider.init('navPrimary','slide');
	
	// CONTENT SLIDER
	$(function(){
		$('#servicesSlider').loopedSlider();
		$('#aboutSlider').loopedSlider();
		$('#newsSlider').loopedSlider();
		$('#creativeSlider').loopedSlider();
	});
	
	
	// ROLLOVER ALPHA FUNCTION
	$(function() {
		$.animatedRollLive("#aboutTeam li");
		$.animatedRollLive("#servicesSlider .category2 li");
		$.animatedRollLive("#creative .creativeNavPrimary li");
		$.animatedRollLive(".imgThumb li");
		return false;
	});
	
	// TOGGLE (OPEN) FUNCTION
	$(".toggleOpen").click(function(e) {
		e.preventDefault(e);
		var toggleSelect = $(this).attr("rel");
		if ($(this).hasClass("services")) {
			$("#services .index").hide();
			$("#services .content"+toggleSelect).fadeIn(700);
			$("#servicesSlider").adjustHeight();
		}
		if ($(this).hasClass("about")) {
			$("#about .index").hide();
			$("#about .content"+toggleSelect).fadeIn(700);
			$("#aboutSlider").adjustHeight();
		}
		if ($(this).hasClass("creative")) {
			$(".imgFull li").addClass("hide");
			$(".imgFull .img1").removeClass("hide");
			$("#creative .index").hide();
			$("#creative .content"+toggleSelect).fadeIn(700);
			$("#creativeSlider").adjustHeight();
		}
		if ($(this).hasClass("thumb")) {
			$(".imgFull li").addClass("hide");
			$(".imgFull .img"+toggleSelect).removeClass("hide");
		}
/*		if ($(this).hasClass("news")) {
			$("#news .index").hide();
			$("#news .content"+toggleSelect).fadeIn(700);
			$("#newsSlider').adjustHeight();
		}
		if ($(this).hasClass("newsStory")) {
			$(this).hide();
			$(this).parents().eq(1).find(".hide").fadeIn(700);
			$("#newsSlider").adjustHeight();
		}*/
		if ($(this).hasClass("news")) {
			$("#news .index").hide();
			$("#news .content"+toggleSelect).fadeIn(700);
			$("#news").adjustHeight();
		}		
    });
	
	// TOGGLE (CLOSE) FUNCTION
	$(".toggleClose").click(function(e) {
		e.preventDefault(e);
		if ($(this).hasClass("services")) {
			$("#services div.hide").hide();
			$("#services .index").fadeIn(700);
			$("#servicesSlider").adjustHeight();
		}
		if ($(this).hasClass("about")) {
			$("#about div.hide").hide();
			$("#about .index").fadeIn(700);
			$("#aboutSlider").adjustHeight();
		}
		if ($(this).hasClass("creative")) {
			$("#creative div.hide").hide();
			$("#creative .index").fadeIn(700);
			$("#creativeSlider").adjustHeight();
		}
/*		if ($(this).hasClass("news")) {
			$("#news div.hide").hide();
			$("#news .index").fadeIn(700);
			$("#newsSlider").adjustHeight();
		}
		if ($(this).hasClass("newsStory")) {
			$(this).parents().eq(1).hide();
			$(this).parents().eq(2).find(".toggleOpen").fadeIn(700);
			$("#newsSlider").adjustHeight();
		}*/
		if ($(this).hasClass("news")) {
			$("#news div.hide").hide();
			$("#news .index").fadeIn(700);
			$("#news").adjustHeight();
		}		
    });
    
    
/*	// READ MORE/LESS FUNCTION
	$('.expand .show').click(function(e) {
		e.preventDefault(e);
		var obj = $(this).parents('.expand');
		if ($(obj).hasClass('expanded')) {
		alert('it has class');
			$(obj).removeClass('expanded');
			$('.content',obj).slideUp('fast');					
			$(this).text('pepe more');
			$("#newsSlider").adjustHeight();
		} else {
		alert('it has noclass');
			$(obj).addClass('expanded');
			$('.content',obj).slideDown('fast');
			$(this).text('pepe less');
			$("#newsSlider").adjustHeight();
		}
		return false;
	});*/

    
	// BACKGROUND POSITION FUNCTION
	/*	
	$(function(){
		$('#navPrimary a')
			.css( {backgroundPosition: "0px 0px"} )
			.mouseover(function(){
				$(this).stop().animate({backgroundPosition:"(0px 48px)"}, {duration:200})
			})
			.mouseout(function(){
				$(this).stop().animate({backgroundPosition:"(0px 0px)"}, {duration:500, complete:function(){
					$(this).css({backgroundPosition: "0px 0px"})
				}})
			})
	});
	*/
		
});

/***********************************************************************
	LINKS FUNCTION
************************************************************************/

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}

window.onload = externalLinks;

/***********************************************************************
	MENU SLIDER FUNCTION
************************************************************************/

var menuSlider=function(){
	var m,e,g,s,q,i; e=[]; q=8; i=8;
	return{
		init:function(j,k){
			m=document.getElementById(j); e=m.getElementsByTagName('li');
			var i,l,w,p; i=0; l=e.length;
			for(i;i<l;i++){
				var c,v; c=e[i]; v=c.title; if(v=="active"){s=c; w=c.offsetWidth; p=c.offsetLeft}
				c.onmouseover=function(){menuSlider.mo(this)}; c.onmouseout=function(){menuSlider.mo(s)};
				c.onclick=function (){ s=this; };
			}
			g=document.getElementById(k); g.style.width=w+'px'; g.style.left=p+'px';
		},
		mo:function(d){
			clearInterval(m.tm);
			var el,ew; el=parseInt(d.offsetLeft); ew=parseInt(d.offsetWidth);
			m.tm=setInterval(function(){menuSlider.mv(el,ew)},i);
		},
		mv:function(el,ew){
			var l,w; l=parseInt(g.offsetLeft); w=parseInt(g.offsetWidth);
			if(l!=el||w!=ew){
				if(l!=el){var ld,lr,li; ld=(l>el)?-1:1; lr=Math.abs(el-l); li=(lr<q)?ld*lr:ld*q; g.style.left=(l+li)+'px'}
				if(w!=ew){var wd,wr,wi; wd=(w>ew)?-1:1; wr=Math.abs(ew-w); wi=(wr<q)?wd*wr:wd*q; g.style.width=(w+wi)+'px'}
			}else{clearInterval(m.tm)}
}};}();

/***********************************************************************
	CAPTION FUNCTIONS
************************************************************************/

// CAPTION (RED)
this.captionRed = function(){
	
		xCaptionRed = 52;
		yCaptionRed = -110;
		
	$("a.captionRed").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? this.t : "";
		this.n = this.name;
		this.name = "";	
		var d = (this.n != "") ? this.n : "";
		$("body").append("<p id='captionRed'>"+ c +"</p>");								 
		$("#captionRed")
			.css("top",(e.pageY - xCaptionRed) + "px")
			.css("left",(e.pageX + yCaptionRed) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#captionRed").remove();
		this.name= this.n;	
		$("#captionRed").remove();
    });	
	$("a.captionRed").mousemove(function(e){
		$("#captionRed")
			.css("top",(e.pageY - xCaptionRed) + "px")
			.css("left",(e.pageX + yCaptionRed) + "px");
	});			
};

$(document).ready(function(){
	captionRed();
});

// CAPTION (BLUE)
this.captionBlue = function(){
	
		xCaptionBlue = 66;
		yCaptionBlue = -37;
		
	$("a.captionBlue").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? this.t : "";
		this.n = this.name;
		this.name = "";	
		var d = (this.n != "") ? this.n : "";
		$("body").append("<p id='captionBlue'>"+ c +"<br/><em> "+ d +"<em/></p>");								 
		$("#captionBlue")
			.css("top",(e.pageY - xCaptionBlue) + "px")
			.css("left",(e.pageX + yCaptionBlue) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#captionBlue").remove();
		this.name= this.n;	
		$("#captionBlue").remove();
    });	
	$("a.captionBlue").mousemove(function(e){
		$("#captionBlue")
			.css("top",(e.pageY - xCaptionBlue) + "px")
			.css("left",(e.pageX + yCaptionBlue) + "px");
	});			
};

$(document).ready(function(){
	captionBlue();
});

/***********************************************************************
	CONTACT FORM FUNCTIONS
************************************************************************/

// SEND FUNCTION

function doSubmit()
{
	var Name = document.getElementById('contactName').value;
	var email = document.getElementById('contactEmail').value;
	var Message = $("#contactMessage").val(); 
			
	$.post('sendMail.php',{contactName:Name, contactEmail:email,contactMessage:Message}, function(data){$('#MailMessage').html(data);});

	document.getElementById('contactName').value = "";
	document.getElementById('contactEmail').value = "";
	document.contactForm.contactMessage.value = "";
}

function doConfirm()
{
	msg='';
	if(document.getElementById('contactName').value=='')
	{
		msg=msg+"Name field can not be empty<br />";
	}
	if(document.getElementById('contactEmail').value=='')
	{
		msg=msg+"Email address field can not be empty<br />";
	}
	else
	{
		if (echeck(document.getElementById('contactEmail').value)==false)
		{
			document.getElementById('contactEmail').value ="";
			msg=msg+"Please enter a Valid Email Address<br />";
		}
	}
	document.getElementById('error').innerHTML = msg;
	if(msg == "")
	{
		return true;
	}
	else
	{
		return false;
	}
}

// ADDRESS VALIDATION FUNCTION

var bugchars = '!#$^&*()+|}{[]?><~%:;/,=`"\'';
function CharsInBag(s)
{   
	var i;
	var lchar="";
    // Search through string's characters one by one.
    // If character is not in bag.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
		if(i>0)lchar=s.charAt(i-1)
        if (bugchars.indexOf(c) != -1 || (lchar=="." && c==".")) return false;
    }
    return true;
}
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is not a number.
        var c = s.charAt(i);
        if ((c >= "0") && (c <= "9") && (c != ".")) return false;
    }
    // All characters are numbers.
    return true;
}
function echeck(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var lastdot=str.lastIndexOf(dot);
	if (str.indexOf(at)==-1)
	{
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
	   return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr || str.substring(lastdot+1)=="")
	{
		return false;
	}
	 
	 if (str.indexOf(at,(lat+1))!=-1)
	 {
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	 {
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1)
	 {
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1)
	 {
		return false;
	 }
	if(CharsInBag(str)==false)
	{
		return false;
	 }
	 var arrEmail=str.split("@");
	 var ldot=arrEmail[1].indexOf(".");
	 if(isInteger(arrEmail[1].substring(ldot+1))==false)
	 {
		alert("Invalid E-mail ID");
		return false;
	 }
	 return true;
}


