/* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/ */

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});
/* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/ */

/*jQuery Droppy */
$.fn.droppy = function() {
  
  this.each(function() {
    
    var root = this, zIndex = 1000;
    
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!$.data(subnav, 'cancelHide')) {
          $(subnav).slideUp();
        }
      }, 500);
    }
  
    function show() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', true);
      $(subnav).css({zIndex: zIndex++}).slideDown();
      if (this.nodeName.toLowerCase() == 'ul') {
        $(getActuator(this)).addClass('hover');
      }
    }
    
    $('ul, li', this).hover(show, hide);
    $('li', this).hover(
      function() { $(this).addClass('hover'); },
      function() { $(this).removeClass('hover'); }
    );
    
  });
  
};


/*
 * jQuery Catfish Plugin - Version 1.3
 */

jQuery.fn.catfish = function(options) {
	this.settings = {
		closeLink: 'none',
		animation: 'slide',
		height: '30'
	}
	if(options)
		jQuery.extend(this.settings, options);
	
	if ( this.settings.animation != 'slide' && this.settings.animation != 'none' && this.settings.animation != 'fade' ) {
		alert('animation can only be set to \'slide\', \'none\' or \'fade\'');
	}
	
	var id = this.attr('id');
	settings = this.settings;
	jQuery(this).css('padding', '0').css('height', this.settings.height + 'px').css('margin', '0').css('width', '100%');
	jQuery('html').css('padding', '0 0 ' + ( this.settings.height * 1 + 50 ) + 'px 0');
	if ( typeof document.body.style.maxHeight != "undefined" ) {
		jQuery(this).css('position', 'fixed').css('bottom', '0').css('left', '0');
	}
	
	if ( this.settings.animation == 'slide' ) {
		jQuery(this).slideDown('slow');
	}
	else if ( this.settings.animation == 'fade' ) {
		jQuery(this).fadeIn('slow');
	}
	else {
		jQuery(this).show();
	}
	if ( this.settings.closeLink != 'none' ) {
		jQuery(this.settings.closeLink).click(function(){
			jQuery.closeCatfish(id);
			return false;
		});
	}
	
	// Return jQuery to complete the chain
	return this;
};
jQuery.closeCatfish = function(id) {
	this.catfish = jQuery('#' + id);
	jQuery(this.catfish).hide();
	jQuery('html').css('padding', '0');
	jQuery('body').css('overflow', 'visible'); // Change IE6 hack back
};



function scrollTo(selector) {
    var targetOffset = $(selector).offset().top;
    $('html,body').animate({scrollTop: targetOffset}, 1500);
}


function changeZIndex(target, index){
	if(mainMenuStatus == 0 ){	$(target).css("zIndex", index);	}
}

function nudgeDiv (target){
    $(target).animate({left:"+=5px"},30).animate({top:"+=5px"},30)
    .animate({top:"-=10px"},30).animate({left:"-=10px"},30)
    .animate({top:"+=5px"},30).animate({left:"+=5px"},30)
    .animate({left:"+=5px"},30).animate({top:"+=5px"},30)
    .animate({top:"-=10px"},30).animate({left:"-=10px"},30)
    .animate({top:"+=5px"},30).animate({left:"+=5px"},30);	
}


var mainMenuStatus = 0;
var lastMenuOpen ="";

function hideMenu(target){
	$(target).animate({ marginTop: "166px"}, 500, 'easeOutSine', function(){changeZIndex("#mainMenu", 3)}) 
}

var lastPortfolioLI = "";
function loadProjectDetail(id, target, obj){
	$(obj).parent().parent().slideUp();
	$(target).slideDown();

	$(obj).parent().parent().parent().removeClass().addClass("projectDivHover");

	loadAjaxData('porfoliolist-project-detail.php', target, 'projectID='+id, 'white');
	scrollTarget = $(obj).parent().parent().parent().parent();
	//scrollTo(scrollTarget);
	
	$("#"+lastPortfolioLI).children().children(".portfolioDetail").slideUp();
	$("#"+lastPortfolioLI).children().children(".projectName").slideDown();		
	$("#"+lastPortfolioLI).children().removeClass().addClass("projectDiv");		
	
	lastPortfolioLI = $(obj).parent().parent().parent().parent().attr("id");
}

$(document).ready(function(){

    $("#mainMenu div").hover(function(){ 
		mainMenuStatus=1; 
		$("#mainMenu").css("zIndex", 5); 
		$(this).stop().animate({marginTop: "0" }, 500, 'easeOutSine');  
		lastMenuOpen = "#"+$(this).attr("id");
	}, 
      function(){  
	  	mainMenuStatus=0; 
		  var t=setTimeout("hideMenu('"+lastMenuOpen+"')",2000)
		}
    );	
	
    $(".newsBot").hover(function(){ 
		$(this).removeClass().addClass("newsBotHover");
	}, 
      function(){ 
		$(this).removeClass().addClass("newsBot");	  
	   }
    );	
	
    $(".bottomBanner").hover(function(){ 
		$(this).animate({marginTop: "-15px" }, 200, 'easeInOutBounce');
	}, 
      function(){ 
		$(this).animate({marginTop: "0px" }, 200, 'easeInOutBounce');
	   }
    );		
	
	
	
	var consultationNudge = setInterval("nudgeDiv('#web-design-consultation');", 4000);
	var consultationDivStatus = "0";
 	$("#web-design-consultation").click(function () { 
		clearInterval(consultationNudge);  
		if(consultationDivStatus == "0"){
			$("#consultationButton").animate({marginLeft: "0px" }, 700, 'easeInOutQuint'); 
			$("#consultationListDiv").fadeIn(1500); 
			consultationDivStatus = "1";
		}else{
			$("#consultationButton").animate({marginLeft: "268px" }, 700, 'easeInOutQuint'); 
			$("#consultationListDiv").fadeOut(); 
			consultationDivStatus = "0";		
		}
	});	
	
	
    $("#corporate-identity").hover(function () { nudgeDiv (this); }, function () { });   
	
		
 
	$("#homeCaseStudyImage").hover(function () { $(".homeCastStudyContents").addClass("hover"); }, function () {$(".homeCastStudyContents").removeClass("hover"); });   
	
	$(".headerBrown").after("<div class='headerBrownArrow'></div>");
	$(".headerPink").after("<div class='headerPinkArrow'></div>");	
	

	/* Homepage Header Tabs 	 */
	 $('#homepageHeader ul li:first').addClass('current'); 
	 $('#homepageHeader ul li a').click(function(){ 
		var currentTab = "#homepageHeaderImage";											 
		var currentHref = $(this).attr('rel'); 											 

		$('#homepageHeader ul li').removeClass('current'); 
		$(this).parents('li:eq(0)').addClass('current'); 
		$('#homepageHeader div').animate({opacity:0.2}, 600); 
		$(currentTab).html("<div align='center' style='margin-top:120px;'><img src='images/ajax-loader-white1.gif' vspace='' hspace='' alt='Loading Data...' /></div>");
		 $.ajax({
		   type: "POST",
		   url: siteURL+"ajax/homepage-headers.php",
		   cache: false,
		   data: "currentHref="+currentHref,
		   success: function(msg){
				$(currentTab).html(msg);
				$(currentTab).animate({opacity:1}, 600); // Show div with id equal to variable currentTab
		   }
		 });												 

			return false;
	}); 	

	

	$(".portfolioExpandIcon").click(function () { 
		var projectID = $(this).attr("rel");
		var target = $(this).attr("target");
		var obj = this;
		loadProjectDetail(projectID, target, obj);
		return false;
	});

	$(".homeProjectsHref").click(function () { 
		var sliderIconXPos="";
	 	var id = $(this).attr("id");
		if(id == "p1") { sliderIconXPos=0; } else if (id=="p2"){ sliderIconXPos=272;} else { sliderIconXPos=560;} 
		catID = $(this).attr("rel");
		loadAjaxData('homepage-projects.php', '.homepageProjectsList', "catID="+catID, "pink"); 
		$("#sliderIcon").animate({left:sliderIconXPos},1000);
		return false;
	});

	$("#homeNewsPrevNext a").click(function () { 
		var page =  ($(this).attr('title') == 'Previous')?homepageNewsCurrentPage-2:homepageNewsCurrentPage;
		loadAjaxData('homepage-news.php', '#homepageNewsContainer', "limit=3&page="+page, "brown"); 
		return false;
	});	

	$("#homeCaseStudyPrevNext a").click(function () { 
		var page =  ($(this).attr('title') == 'Previous')?homepageCaseStudiesCurrentPage-2:homepageCaseStudiesCurrentPage;
		loadAjaxData('homepage-featured-case-study.php', '#featuredCaseStudy', "page="+page, "brown"); 
		return false;
	});	
	
}); 

function loadAjaxData(fileName, target, variables, image){
	
	$(target).html("<img src='"+siteURL+"images/ajax-loader-"+image+".gif' />");
	 $.ajax({
	   type: "POST",
	   url: siteURL+"ajax/"+fileName,
	   cache: false,
	   data: variables,
	   success: function(msg){
			$(target).html(msg);
	   }
	 });
}


var reviewListAdd = function(o){

	$.post(siteURL+'ajax/review-list-add.php',{id:o.id, title:o.title}, function(r){
		if(r.success){
			mytitle = o.title;
			if(mytitle.length > 20) { newTitle = mytitle.substring(0,20)+"...";}else {newTitle=mytitle;}
			$('#footerReviewsList').append('<li id="review_list_'+o.id+'"><input type="checkbox" value="'+o.id+'" class="compareList" /> '+newTitle+'<div class="footerReviewsListFloater"><a href="javascript:;" onclick="reviewListRemove('+o.id+')">X</a></div></li>');
			$('#catfish').catfish();
		}
	},'json');
}
	
var reviewListRemove = function(i){
	$.post(siteURL+'ajax/review-list-remove.php',{id:i}, function(r){
		if(r.success){
			$('#review_list_'+i).remove();
			if(r.count == 0){
				$('#catfish').hide();
			}
		}
	},'json');
}

$(document).ready(
	function(){
		$('#compare-list').click(function(){
			
			var checks = new Array();
			var maxAllow = 4;
			$('.compareList:checked').each(function(){
				checks.push($(this).val());
			});
			if(checks.length < 2){
				alert('Please select atleast 2 projects to compare');
			}else if(checks.length > maxAllow){
				alert('You cannot select more then '+maxAllow+' projects');
			}else{
				/*$.nyroModalManual({
					url: '/aan-01/aanv5-1/ajax/compare.php?c='+checks.join()
				});*/
				
				$.post(siteURL+'ajax/compare.php',{c:checks.join()},function(response){
					$('#compare-list-div-contents').html(response);
					$('#compare-list-div').show().animate({top:"50%"}, 1000, "easeOutElastic");
				})
			}
			
			$('#compare-list-close').click(function(){
				 $('#compare-list-div').animate({top:"-50%"}, 1000, "easeInElastic");
			})			
		});
	
	
		$('#print_this_page').click(function(e){
			e.preventDefault();
			var bc = $('#breadcrumbs').html();
			var tbnid = $(this).attr('rel');
			
			var url = siteURL+'ajax/print.php?t='+tbnid+'&breadcrumbs='+bc;
			$('#iframe-print').attr('src',url);
			var posY = $('#printing-div').css("top");
			if(posY=="50%") { 	$('#printing-div').animate({top:"-50%"}, 1000, "easeInElastic"); }
			else { $('#printing-div').show().animate({top:"50%"}, 1000, "easeOutElastic");}
			
			$('#btn-print-close').click(function(){
				var posY = $('#printing-div').css("top");
				if(posY=="50%") { 	$('#printing-div').animate({top:"-50%"}, 1000, "easeInElastic"); }
				else { $('#printing-div').show().animate({top:"50%"}, 1000, "easeOutElastic");}
			})
			/*$.post(siteURL+'ajax/print.php', {breadcrumbs:bc, t:tbnid}, function(content){
				$.nyroModalManual({
				  content: content
				});																 
			});*/
			return false;

		});
		
		$('.btn_email_to_friend').click(function(){
			var posY = $('#div_email_to_friend').css("top");
			if(posY=="0px") { $('#div_email_to_friend').animate({top:"-800px"},1500, "easeInElastic"); }
			else {$('#div_email_to_friend').animate({top:"0px"},1500, "easeOutElastic");}										 
		});
		
		$('#frm_email_to_friend').validate({
			rules: {
				senderName: 	{required:true},
				senderEmail:	{required:true,	email:true},
				"friend[]": 		{required:true},
				"email[]":			{required:true,	email:true}
			}
			,submitHandler:function(form){
				$('#email_to_friend_link').val(location.href);
				$(form).ajaxSubmit({
					 dataType:'json'
					,success:function(o){
						if(o.success){
							$('#frm_email_to_friend').html(o.message);
							setTimeout('$("#div_email_to_friend").animate({top:"-800px"},1500, "easeInElastic");', 4000);
						}
					}
				})
			}
		});
		
		$('#add_email_to_friend_row').click(function(){
			$('#tbl_email_to_friend tbody').append('<tr align="center"><td><input name="friend[]" type="text" size="38" maxlength="50" /></td><td><input name="email[]" type="text" size="38" maxlength="50" /></td><td><a href="javascript:;" onclick="remove(this)" title="Remove"><img src="../images/icon-delete.png" alt="Remove" width="16" height="16" border="0"  /></a></td></tr>');
		});
		
		$('#search_btn').click(function(){
			var keyword = $('#search_keyword').val();
			if($.trim(keyword).length > 0){
				location.href = siteURL+'search/'+keyword;	
			}
		});
	}
);

var remove = function(f){
	$(f).parent().parent().remove();
}

