var slider, curSlide, totalSlides, trasDuration, animating, imgLoaded;
$(document).ready(function() {
	
	
	if ($('#testimonials #slider1').length > 0 && $('#wrapper').hasClass('home_page')) {
		$('#slider1').bxSlider();							   
	}
	
	if ($('#featured .slides').length > 0 && $('#wrapper').hasClass('home_page')) {
		$('#main_content .info .process_content .content').hide();
		totalSlides = $('#featured .slides img').length;
		animating = false;
		curSlide = 0;
		imgLoaded = 0;
		trasDuration = 5000;
		for (var i = 0; i < totalSlides; i++) {
			$('#featured .slides .nav').append('<a href="#" />');
			if (i == 0) $('#featured .slides .nav > a').addClass('active');
			
			img = new Image();
			img.src = $('#featured .slides img').eq(i).attr('src');
			jQuery(img).load(function() {
				imgLoaded++;
				if (imgLoaded == totalSlides) {
					slider = setTimeout(changeSlide, trasDuration);
				}
			});
		}
		
		$('#featured .slides .nav a').live('click', function() {
			if (!$(this).hasClass('active')) {
				clearTimeout(slider);
				var newIndex = $(this).index();
				$('#featured .slides img:not(:nth-child(' + (newIndex + 1) + '))').animate({ opacity:'0.0' }, trasDuration);
				$('#featured .slides img:nth-child(' + (newIndex + 1) + ')').fadeIn();
				$('#featured .slides .nav a').removeClass('active');
				$(this).addClass('active');
				updateMenu(newIndex + 1);
				$('#featured .slides img:nth-child(' + (newIndex + 1) + ')').animate({ opacity:'1.0' }, trasDuration, function() {
					curSlide = newIndex;
					clearTimeout(slider);
					slider = setTimeout(changeSlide, trasDuration);
				});
			}
			return false;
		});
		
		$('#main_content .info .process > a.title').bind('click', function() {
			if ($(this).parent().children('.process_content').children('.content').css('display') == 'none') {
				$(this).addClass('active');
			}
			else {
				$(this).removeClass('active');
			}
			$(this).parent().children('.process_content').children('.content').slideToggle();
			return false;
		});
		
		$('#main_menu .container > div a:first-child').bind('click', function() {
			clearTimeout(slider);
			newIndex = $(this).parent().index()/2;
			$('#featured .slides img:not(:nth-child(' + (newIndex + 1) + '))').animate({ opacity:'0.0' }, trasDuration);
			$('#featured .slides img:nth-child(' + (newIndex + 1) + ')').fadeIn();
			$('#featured .slides .nav a').removeClass('active');
			$('#featured .slides .nav a:nth-child(' + (newIndex + 1) + ')').addClass('active');
			updateMenu(newIndex + 1);
			$('#featured .slides img:nth-child(' + (newIndex + 1) + ')').animate({ opacity:'1.0' }, trasDuration, function() {
				curSlide = newIndex;
				clearTimeout(slider);
				slider = setTimeout(changeSlide, trasDuration);
			});
			return false;
		});
	}
	
	if ($('#contact_form').length > 0) {
		$('#contact_form form ul.services input[type=checkbox]').each(function() {
			$(this).parent().prepend('<span />');
			$(this).hide();
		});
		$('#contact_form form ul.services input[type=checkbox]').bind('click', function() {
			if ($(this).attr('checked') == true)
				$(this).parent().children('span').addClass('selected');
			else
				$(this).parent().children('span').removeClass('selected');
		});
		$('#contact_form form ul.services li > span').bind('click', function() {
			$(this).parent().children('input').attr('checked', !$(this).parent().children('input').attr('checked'));
			if ($(this).parent().children('input').attr('checked') == true)
				$(this).addClass('selected');
			else
				$(this).removeClass('selected');
		});
		$('#contact_form form').bind('submit', function() {
			var param = $(this).serialize();
			submitContact(param);
			return false;
		});
	}
		if ($('#frmPricing').length > 0) {
			
		$('.priceoutput').hide();
		$('.pricing_form form ul.services input[type=radio]').each(function() {
			$(this).parent().prepend('<span />');
			$(this).hide();
		});
		$('.pricing_form form ul.services input[type=radio]').bind('click', function() {
																					 
			$('.pricing_form form ul.services span').removeClass('selected');
			if ($(this).attr('checked') == true)
				$(this).parent().children('span').addClass('selected');

		});
		$('.pricing_form form ul.services li > span').bind('click', function() {
			$(this).parent().children('input').attr('checked', !$(this).parent().children('input').attr('checked'));
			
			$('.pricing_form form ul.services span').removeClass('selected');
			if ($(this).parent().children('input').attr('checked') == true)
				$(this).addClass('selected');

		});
			
			$('.pricing_form form').bind('submit', function() {
				var param = $(this).serialize();
				submitPricing(param);
				return false;
			});

	}
	$("#various3").fancybox({
		'width'				: '70%',
		'height'			: '100%',
		'autoScale'			: false,
		'autoDimensions'    : true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
	
	/*var fancybox_iframe = $('.fancybox-iframe');
	if (fancybox_iframe.length > 0){
		fancybox_iframe.each(function(index){
			// Inline frame width param
			if( $(this).attr('href').match(/width=[0-9]+/i) ){
				var fWidth = parseInt($(this).attr('href').match(/width=[0-9]+/i)[0].replace('width=',''));
			} else {
				var fWidth = '90%';
			}
			// Inline frame height param
			if( $(this).attr('href').match(/height=[0-9]+/i) ){
				var fHeight = parseInt($(this).attr('href').match(/height=[0-9]+/i)[0].replace('height=',''));
			} else {
				var fHeight = '90%';
			}
			if(window.console&&window.console.log) { 
				console.log('fWidth #'+index+': '+fWidth); 
				console.log('fHeight #'+index+': '+fHeight); 
			}
			$(this).fancybox({
				'titlePosition' : 'inside',
				'type'              : 'iframe',
				'autoScale'         : true,
				'padding'           : '10',
				'width'             : fWidth,
				'height'                : fHeight,
				'centerOnScroll'    : true
			});
		});
	}*/
	
});
function closeME() {
	event.preventDefault();
	parent.$.fancybox.close();
}
function changeSlide() {
	var nextIndex;
	if (curSlide == totalSlides - 1)
		nextIndex = 1;
	else
		nextIndex = curSlide + 2;
	updateMenu(nextIndex);
	$('#featured .slides img:not(:nth-child(' + nextIndex + '))').animate({ opacity:'0.0' }, trasDuration);
	$('#featured .slides img:nth-child(' + nextIndex + ')').fadeIn();
	$('#featured .slides .nav a').removeClass('active');
	$('#featured .slides .nav a:nth-child(' + (nextIndex) + ')').addClass('active');
	finishTransition($('#featured .slides img:nth-child(' + nextIndex + ')'));
}

function finishTransition(nextObj) {
	trasDuration=2000;
	if (animating) {
		clearTimeout(slider);
		slider = setTimeout(changeSlide, trasDuration);
	}
	animating = true;
	nextObj.animate({ opacity:'1.0' }, trasDuration, function() {
		curSlide++;
		if (curSlide == totalSlides || curSlide > totalSlides-1) {
			curSlide = 0;
		}
		clearTimeout(slider);
		animating = false;
		slider = setTimeout(changeSlide, trasDuration);
	});
}

function updateMenu(index) {
	$('#main_menu .container > div').removeClass('active');
	$('#main_menu .container > div:nth-child(' + ((index * 2) - 1) + ')').addClass('active');
}

function submitContact(param) {
	if ($('ul.services input[type=checkbox]:checked').length < 1) {
		alert('Please select a service');
		return false;
	}
	if ($.trim($('#txtName').val()).length < 1) {
		alert('Please enter your name');
		$('#txtName').focus();
		return false;
	}
	if ($.trim($('#txtEmail').val()).length < 1) {
		alert('Please enter your e-mail address');
		$('#txtEmail').focus();
		return false;
	}
	
	var x = $.trim($('#txtEmail').val());
	var atpos = x.indexOf("@");
	var dotpos = x.lastIndexOf(".");
	if (atpos<1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
		alert("Not a valid e-mail address");
		$('#txtEmail').focus();
		return false;
  	}
	if ($.trim($('#txtPhone').val()).length < 1) {
		alert('Please enter your phone number');
		$('#txtPhone').focus();
		return false;
	}
	$.ajax({
		type: "POST",
		url: "contact_form.php",
		cache: false,
		data: param,
		success: function(msg) {
			
			//var contactMsg = msg.match("<STATUS>(.*)</STATUS>");
			
			if(msg == 'success' ) {
/*				$('#contact_form').hide();
				$('#main_content > .content').html('<div class="container"><h1>Thank You</h1><h2>Thanks for contacting us.<br />Your comments have been received, and we will be in touch with you shortly.</h2></div>');
*/			location.href="thanku.php?msg=1";	
			}
			else {
				$('#contact_form .errorMsg').html(msg).fadeIn('slow');
				$('#contact_form .errorMsg').delay(2000).fadeOut('slow');
			}
		}
	});
}



function submitPricing(param) {
	

	if($('input:radio[name=rdComplexity]:checked').val()!='easy' && $('input:radio[name=rdComplexity]:checked').val()!='medium' && $('input:radio[name=rdComplexity]:checked').val()!='complex'){
		alert("Please select complexity");
		$("#rdComplexity").focus();
		return false;
	}
	
	if($("#txtNoOfPages").val()==''){
		alert("Please enter no of pages");
		$("#txtNoOfPages").focus();
		return false
	}

	$.ajax({
			type: "POST",
			url: "pricing_form.php",
			cache: false,
			data: param,
			success: function(msg) {
				$('.priceoutput').show();
				$('.priceoutput').html(msg).fadeIn('slow');;
				$('.priceoutput ').delay(5000).fadeOut('slow');
			}
		});
}


