$(document).ready(function(){
	
	//------------------------------Home page showcase
	$("#showcaseContent").scrollable({
		size: 1, 
		vertical: true,
		clickable: false,
		items: ".gallery_items"
	}).navigator({ 
        navi: "#showcaseButtons", 
        naviItem: 'a', 
        activeClass: 'current' 
    }); 
	$('#showcaseButtons li a').mouseover(function(){
		$(this).trigger('click');
		return false;
	});
	$('#showcaseButtons li a span').click(function() {
		var link = $(this).attr('title');
		location.href= link;										   
	});

	//----------------------------- Upcoming Events
	$("div.scrollable_events").scrollable({
		size: 3, 
		clickable: false,
		items: ".event_items",
		nextPage: ".next",
		prevPage: ".prev",
		easing: "custom"
	}).mousewheel();
	
	$('div.event_items div').hover(function () {
		$(this).find('div.moreInfo').show();
	}, function() {
		$(this).find('div.moreInfo').hide();
	});


	//------------------------------- Fancybox 
	
	$('a.popup').attr('name','gallery').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'titlePosition'	: 'over',
		'overlayColor' : '#6c98b9'
	});
	
    //------------------------------- Testimonials
	/*
	if ($("div.scrollable_testimonials").length) {
		var api = $("div.scrollable_testimonials").scrollable({
			api:true, 
			size: 1, 
			clickable: false,
			items: ".testimonial_items",
			loop: true,
			speed: 500
		});
		$("div.scrollable_testimonials").autoscroll({
			api: true,																							 
			autoplay: true,
			interval: 7000
		});
		api.onBeforeSeek(function(event,index){ 
			var element = jQuery("div.scrollable_testimonials"); 
			if (element.css('opacity') == .01) { 
				return true; 
			} 
			element.fadeTo("slow", .01, function(){ 
				api.seekTo(index); 
			}); 
			return false; 
		}).onSeek(function(){ 
			jQuery("div.scrollable_testimonials").fadeTo('slow', 1); 
		});
	}
	*/
	
	$('.testimonial_items').cycle({ 
		fx:     'fade', 
		speed:   2000, 
		timeout: 7000, 
		cleartype:  1
	 });

	
	//----------------------------- Main Nav
	$("#mainNav ul li a").hoverIntent(function() { 
		$(this).parent().find("ul").animate({ height: "toggle", opacity: "toggle" }, 450, "easeOutExpo");
		$(this).parent().hover(function() {}, function(){ $(this).parent().find("ul").slideUp('slow'); });		 
	}, function() {});

	$('#mainNav ul li ul').hover(function() {
		$(this).parents("li").addClass('selected');
	},
	function() {
		$(this).parents("li").removeClass('selected');
	});
	
	//----------------------------- Sub Nav


	//Back to top

	$('#backToTop').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow'); 
		return false;
	});	

	//Folow Us hovers
	$('#followUs a img').hover(function() {
		var src = $(this).attr("src").match(/[^\.]+/) + "_hover.gif";
        $(this).attr("src", src);	
	},function() {
		var src = $(this).attr("src").replace("_hover", "");
		$(this).attr("src", src);
	});
	
	
	//----------------------------- Subscribe form 
	$("#subscribeForm").validate({
		 rules: { 
	      "cm-name": { 
			  required: true,
			  checkForDefaultVale: true 
	      },
		  "cm-iidykt-iidykt": {
			  required: true,
			  email: true
		  }
	    }, 
		messages: { 
 	      "cm-name": "Please enter a your full name",
		  "cm-iidykt-iidykt": "Please enter your email address"
 	    },
		submitHandler: function(form) { sumbitForm(form.id); }
	});
	
	
	//------------------------------- Search form validation
	$("form#ajaxSearch_form").submit(function() {
		if ( ($("input#ajaxSearch_input").val() == "") || ($("input#ajaxSearch_input").val() == "Search...")){
			alert('At least 3 characters are required for your search term');
			return false;
		}
    });
	
	//------------------------------- Callback form
	
	$("a[rel]").overlay({ 
        expose: '#6c98b9', 
        opacity: '0.4',    
		closeOnClick: false,
        onBeforeLoad: function() { 
            var wrap = this.getContent().find(".request").empty();
            wrap.load(this.getTrigger().attr("href")); 
        } 
    }); 
	
	
	
	$('#callback_form').live('submit',function() {
		data = $(this).serialize();
		url = 'index.php?id=174';
		$.ajax({
		  type: "POST",    
		  url: url,
		  data: data,
		  success: function(msg){
			$('.request').html(msg);
		   }
		});   
		return false;										   
	});
	
	//---------------------------------- Layout related
	$('#teamBlog ul li:last').css('border-bottom','none');
	
	if (!$("#rightChunks").length) {
		$('#content').css({
			'marginRight' : '0',
			'width' : '645px'
		});	
	}

	$('#content a img').parent().css('border-bottom','none');
	
	$('.events_snapshot p').each(function() {
		var target = $(this).parent().parent().find('span.date');
		if (target.length) {
			$(this).css('padding-top','10px');
		}
		
	});
	

	
	//---------------------Loading message
	
	$("#loading").bind("ajaxSend", function(){    
        $(this).show();
     }).bind("ajaxComplete", function(){
        $(this).hide();
     });

	$('#loading').show();
	
	
}); //Close document.ready

$(window).bind("load", function() {								
	$('div.images,#showcaseContent,#showcaseButtons').show();
	$('#loading').fadeOut();
});

/*------- Validation additonal methods --------------------------------------------------*/
$.validator.addMethod( "checkForDefaultVale", function(value, element) { 
	data = (element.value == "Full name") ? false : true; 
	return data;
});
function sumbitForm(formId) {
	 data = $('#'+formId).serialize();
	 formAction = $('#'+formId).attr("action");
	 url = 'assets/snippets/campaignMonitor/proxy.php';
	 final = data + "&action=" + formAction;
     $.ajax({
      type: "POST",    
      url: url,
      data: final,
      success: function(msg){
        $('#newsletter_form').html("<span class='error'>Thank you for subscribing to the Tender Team newsletter.<br />You'll receive the first edition by email soon.</span>");
		$.get('ezine-conversion', function(data) {
			$('#newsletter_form').append(data);
		});	
      }
    });
    return false;
}
/*------- Equal hights ---------------------------------------------------------*/
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
/*------- Toggle animation (open - close) --------------------------------------*/
(function($) {
	$.fn.animPanel = function(options) {
		var opts = $.extend({}, $.fn.animPanel.defaults, options);
		return this.each(function() {
			$this = $(this);
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;				
			$this.toggle(function() {
				$(o.target).animate({ top: o.topStart }, 300, "easeOutBounce", function() { 
					$(o.target).find('#trigger').html(o.textOpen);
				});						   
			},function() {
				$(o.target).animate({ top: o.topEnd }, 300, "easeOutBounce", function() { 
					$('#topPanelOne ul li ul:visible').slideUp();	
					$(o.target).find('#trigger').html(o.textClose);
				});						   	
			})	
			
		});
	};
})(jQuery);
/*------ Easing animations -----------------------------------------------------*/
(function($) {
	$.easing.custom = function (x, t, b, c, d) { 
		var 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; 
	}
	$.easing.easeOutExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	};
	$.easing.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;
		}
	};
})(jQuery);
/*----- Custom alert popup -----------------------------------------------------*/
function my_alert(url) {
	$('body').append('<div id="alert"><span class="close">Close</span></div>');
	$("#alert").load(url);
	$("#alert").overlay({ 
		top: 200, 
		expose: { 
			color: '#6c98b9', 
			loadSpeed: 500, 
			opacity: 0.6,
			effect: 'apple'			
		}, 
		closeOnClick: false,
		api: true 
	}).load();
}
/*---- Logs -------------------------------------------------------------------*/
jQuery.fn.log = function (msg) {
		
      console.log("%s: %o", msg, this);
      return this;
  };
