$(document).ready(function(){
	initHover();
	initTabs();
	initToggle();
	initActive();
	initNewsSlider();
	initArtSlider();
	initPager();
	initExpand();
	initScroll();
	initTeam();
	$("div.bg-inp select").styleSelect({styleClass: "selectstyle",optionsTop: "23px",speed: "200"});
	initCheckboxValidate();
	initValidate();	
});


$(window).load(function(){
	initLogosSlider();
});

function initHover(){
	$("#menu > li").hover(
		function(){$(this).addClass('hover');},
		function(){$(this).removeClass('hover');}
	)
};

function initToggle(){
	$("ul.col-exp li img").click(function(){
		$(this).parent().find("img").toggle();
		$(this).parent().find("ul.exp").slideToggle("slow");
		return false;
	})
};

function initActive(){
	
	$('#header div.social-bar ul.profile li').hover(
		function(){$(this).addClass('active');},
		function() {$(this).removeClass('active');}
	);
	
	$('#rotating-area div.tabs ul li a').click(
		function(){$(this).addClass('active');}	
	);

};

function initLogosSlider(){
	if ($('#logos-slider div.slider').length == 0) return false;
	
	$('#logos-slider div.slider a.next, #logos-slider div.slider a.prev').click(function(){return false;});
	$('#logos-slider div.slider').scrollable({
		items: 'ul.logos',
		item : 'li',
		size : 9,
		clickable:false,
		prev : 'a.prev',
		next : 'a.next',
		loop : true
	}).circular().autoscroll(4000);
	
};

function initNewsSlider(){
	if ($('#text-slider div.slider').length == 0) return false;
	
	$('#text-slider div.slider a.next, #text-slider div.slider a.prev').click(function(){return false;});
	$('#text-slider div.slider').scrollable({
		items: 'ul',
		item : 'li',
		size : 1,
		clickable:false,
		prev : 'a.prev',
		next : 'a.next',
		loop : true
	}).circular();
	
};

function initTabs(){
	$('#rotating-area div.tabs ul').tabs('#rotating-area div.bg-banner div.tabs-content');
	
	$('#rotating-area div.home-slider a.right-btn, #text-slider div.home-slider a.left-btn').click(function(){return false;});
	$('#rotating-area div.home-slider').scrollable({
		items: 'ul',
		item : 'li',
		size : 1,
		clickable:false,
		prev : 'a.left-btn',
		next : 'a.right-btn',
		loop : false
	}).circular().autoscroll(7000);
};

function initArtSlider(){
	if ($('#article-slider div.middle-block').length == 0) return false;
	
	$('#article-slider div.middle-block a.slid-right, #article-slider div.middle-block a.slid-left').click(function(){return false;});
	$('#article-slider div.block').scrollable({
		items: 'div.inner',
		item : 'div.slide-boxes',
		size : 1,
		clickable:false,
		prev : 'a.slid-left',
		next : 'a.slid-right',
		loop : true
	}).circular();
};


// initialize scrollable and return the programming API
$(document).ready(function(){

	
	// a dedicated click event for the intro thumbnail
	$("#t0").click(function() {

		// seek to the beginning (the hidden first item)
		$(".block-post-bottom").scrollable().begin();

	});
	
})

function initScroll(){
var offset = {};
var topPadding = 15;
var document_size = $('#page').height()-$('#sidebar').height()-$('#header').height()- topPadding - 300;
$(function() {
	offset = $("#scrollable_sidebar").offset();
}); 
$(window).scroll(function() {
	if ($(window).scrollTop() > offset.top) {
		var margin_Top = $(window).scrollTop() - offset.top + topPadding;		
		if (margin_Top<document_size) {
			$("#scrollable_sidebar").stop().animate({			
				marginTop: margin_Top
			});
		}
	} else {
		$("#scrollable_sidebar").stop().animate({
			marginTop: 0
		});
	};
});
}

function initPager(){
	if( !$('#pager-blocks').length ) return;
	
	var blockHeight = $("#pager-blocks div.block").height() + 13;
	$("#pager-blocks").css({
		position : "relative",
		height : blockHeight + "px"
	});

	var $blocks = $("#pager-blocks div.block");
	$blocks
		.css({
			position : "absolute",
			top : 0,
			left : 0,
			height: blockHeight + "px",
			opacity : 0
		})
		.hide();
	$($blocks.get(0)).show().css("opacity",1).addClass("active");
	$("#pager-post a:first").addClass("active");
	window.animatePager = false;

	setInterval(function(){
		if (!window.animatePager){
		  //window.animatePager = true;
			return;
		}
		var $blocks = $("#pager-blocks div.block");
		var $active = $("#pager-blocks div.active");
		var index = $blocks.index($active.get(0));
		$next = $active.next();
		if ($next.size() == 0){
			index = -1;
			$next = $("#pager-blocks div.block:first");
		}
		$active.animate({opacity:0},500,function(){
			$active.removeClass("active").hide();
			$next.show().css('opacity', 0);
			$next.animate({opacity:1},500,function(){
				$next.addClass("active");
				$("#pager-post a").removeClass("active");
				$("#pager-post a:eq("+ ++index +")").addClass("active");
			});
		});
	},3000);

	window.animation = false;

	$("#pager-post a").click(function(e){
		e.preventDefault();
		
		if (window.animation){
			return;
		}
		window.animation = true;
		window.animatePager = false;
		var index = $("#pager-post a").index(this);
		var $active = $("#pager-blocks div.active");
		var $next = $("#pager-blocks div.block:eq("+index+")"); //$active.next();
		if ($next.size() == 0){
			$next = $("#pager-blocks div.block:first");
			index = -1;
		}
		if ($next.hasClass("active")){
			window.animation = false;
			return;
		}
		$("#pager-post a").removeClass("active");
		$(this).addClass("active");
		$active.animate({opacity:0},500,'',function(){
			$active.removeClass("active");
			$active.hide();
			$next.show().css('opacity', 0);
			$next.animate({opacity:1},500,'',function(){
				$next.addClass("active");
				window.animation = false;
			});
		});
	});
};

function initExpand(){
	$('#text-slider a.more').click(function(){
		$(this).parent().next().slideDown();
		$(this).hide();
		return false;
	});
	$('#text-slider a.less').click(function(){
		$(this).parent().slideUp();
		$(this).parent().prev().find('a.more').show();
		return false;
	});
	$('#text-slider a.prev,#text-slider a.next').click(function(){
		$(this).parent().find('p.expand').slideUp();
		$(this).parent().find('a.more').show();
	});
	$('#content div.middle-block a.more').click(function(){
		$(this).parent().next().slideDown();
		$(this).hide();
		return false;
	});
	$('#content div.middle-block a.less').click(function(){
		$(this).parent().slideUp();
		$(this).parent().prev().find('a.more').show();
		return false;
	});
};

function initTeam(){
	$('a.team').addClass('active-team');
	$('.middle-block-bottom1 table a').click(function(){
		$('table a').removeClass('active-team');
		$('.middle-block-bottom1 table a').removeClass('active-tab');
		var link_id = $(this).attr('id')
		if (link_id) {
			$(this).addClass('active-tab');
			$('ul#meet-the-team').show();
			$('ul#meet-the-team').removeClass('active');
			$('#meet-the-team li').each(function(){
				if ($(this).hasClass(link_id)) {
					$(this).parent().parent().parent().find('a.team').addClass('active-team');
					$(this).parent().parent().parent().find('a.team img.normal').show();
					$(this).parent().addClass('active');
				}
			});

			$('ul.active').show();
			return false;
		}			
	})
};

function initValidate(){
	$("#content").find("form.request-enquirytop").validate({
		'onkeyup': false,
		/*'onfocusout': true,
		'onclick' : false,
		'focusInvalid': false,*/
	    'focusCleanup': true,
	    'rules': {
	      'first-name': {
	      	'required': true,
	      	'minlength': 2,
	      	'maxlength': 12
	      },
		  'last-name': {
	      	'required': true,
	      	'minlength': 2,
	      	'maxlength': 20
	      },
		  'email': {
	      	'required': true,
	      	'email': true
	      },
		  'company': {
	      	'required': true,
	      	'minlength': 2,
	      	'maxlength': 20
	      },
		  'phone': {
	      	'required': true,
			'minlength': 5,
	      	'maxlength': 16,
	      	'digits': true
	      },
		  'website': {
	      	'required': true,
	      	'url': true
	      },
		  'description': {
	      	'required': true,
	      	'minlength': 5
	      },
		  'captcha': {
	      	'required': true
		  }
		},
		'messages': {
	      'first-name': {
	        'required': "Fill in your First Name",
			'minlength': "First Name - min. 2 characters.",
			'manlength': "First Name - max. 12 characters."
	      },
		  'last-name': {
	        'required': "Fill in your Last Name",
			'minlength': "Last Name - min. 2 characters.",
			'manlength': "Last Name - max. 20 characters."
	      },
		  'email': {
	        'required': "Fill in your e-mail address",
			'email': "Fill in a valid e-mail address"
	      },
		  'company': {
	        'required': "Fill in your Company Name",
			'minlength': "Company Name - min. 2 characters.",
			'manlength': "Company Name - max. 20 characters."
	      },
		  'phone': {
	        'required': "Fill in a contact number.",
			'minlength': "Contact number - min. 5 characters.",
			'manlength': "Contact number - max. 16 characters.",
			'digits': "Contact number - Numbers only."
	      },
		  'website': {
	        'required': "Fill in your Website URL.",
			'url': "Fill in a valid Website URL."
	      },
		  'description': {
	        'required': "Give a brief description of your website.",
			'minlength': "Give a brief description of your website - min. 5 characters."
	      },
		  'captcha': {
	        'required': "Fill in the below blue characters (case sensitive)"
	      }
		},
		'errorPlacement': function(error, element) {
	      error.insertAfter(element);
		}
	});
};

function initCheckboxValidate(){
	$("#content form.request-enquirytop #captcha").blur(function(){
		$("#content form.request-enquirytop label.captcha-error").hide();
		initCheckSecurityCode();
	})
	$("#content form.request-enquirytop").submit(function(){
		if(document.getElementById('seo').checked != true && document.getElementById('conversion').checked != true && document.getElementById('social-media').checked != true && document.getElementById('ppc').checked != true && document.getElementById('e-commerce').checked != true && document.getElementById('design-services').checked != true){
			$("#content form.request-enquirytop").find("label.service-error").css("display","block");
		}
	});
	$("#content form.request-enquirytop div.radio-box input").change(function(){
		$("#content form.request-enquirytop").find("label.service-error").hide();
		if(document.getElementById('seo').checked != true && document.getElementById('conversion').checked != true && document.getElementById('social-media').checked != true && document.getElementById('ppc').checked != true && document.getElementById('e-commerce').checked != true && document.getElementById('design-services').checked != true){
			$("#content form.request-enquirytop").find("label.service-error").css("display","block");
		}
	});	
}

function initCheckSecurityCode(){
	$.ajax({ url: 'verify-code.php', 
	     data: "security_code="+$("#captcha").val(),
	     type: 'post', 
	     datatype: 'html', 
	     success: function(outData) { 
   	          if(outData != 1) {
			  	$("#content form.request-enquirytop label.captcha-error").css("display","block");
   	          }
			  else{
			  	$("#content form.request-enquirytop div.spamholder label:first").html("<img src=../../images/tick.gif class=tick align=left><label>Verification completed successfully</label>").parents("div.spamholder").next().hide();
			  }
         }, 
     error: function(errorMsg) { alert('Error occured: ' + errorMsg); }});
}
