//detect IE
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) //test for MSIE x.x;
var ieversion=new Number(RegExp.$1); // capture x.x portion and store as a number

if(ieversion < 7)
	window.location = 'index-ie.html';

$(document).ready(function(){
	
	//turn off autocomplete
	$('input').attr("autocomplete", "off");
	
	//preload images
	$.preloadImages("bg-menu.html", "clients.html","forms/bg-input-clogin.html", "forms/bg-input-error-clogin.html",
					"forms/bg-input-error-quote.html","forms/bg-input-quote-focus.html","forms/bg-input-quote.html",
					"nav-left-over.html","nav-left-disabled.html","nav-right-disabled.html", "nav-right-over.html",
					"nav-right.html", "nav-left-over-projects.html","nav-left-disabled-projects.html",
					"nav-right-disabled-projects.html", "nav-right-over-projects.html", "nav-right-projects.html",
					"bg-menu.html", "menu/home.html", "menu/home-over.html", "menu/about.html",
					"menu/about-over.html", "menu/services.html", "menu/services-over.html", "menu/quote-over.html",
					"menu/contact-over.html", "c-close.html", "services/menu/blogs-over.html",
					"services/menu/creative-web-design.html", "services/menu/custom-development-over.html",
					"services/menu/seo-over.html", "services/menu/web-standard-over.html");
	
	//show carousel
	$("#carousel").css('display', 'block');
	
	//add datePicker
/*	$('.date-pick').datePicker({
							   clickInput: true,
							   createButton: false
							   }); */

	
	$('#services-tabs > ol').tabs({ fx: { opacity: 'toggle', duration: 100 } });

	
	//add carousel action
	var carousel = $('#carousel').jcarousel({
					   scroll:1,
					   //auto: 20,
					   wrap: 'last',
					   animation: 500,
				       initCallback: mycarousel_initCallback,
					   itemVisibleInCallback: {
									onBeforeAnimation:  mycarousel_reloadCallback,
									onAfterAnimation:  check_for_contact
								}
					});
	
	var carousel_projects = $('#latestp-container').jcarousel({
					   			   scroll:1,
								   auto: 30,
								   wrap: 'last',
								   animation: 500
						    });
	
	//scroll bottom - this-form
	$('#contact-container #this-form a').click(function(){
		$('html, body').animate({scrollTop: 400}, 1000);
		return false;
													  });
	
	//show/hide menu
	$("#menu-opener").click(function(){
									 $("#menu").slideToggle("slow");
									 });
	//login clients ajax
	$('#clients-login').submit(function(){
									var error = false;
									$("#cusername").addClass('error');
									$("#cpass").addClass('error');
									
									if($("#cusername").val() == '' || $("#cusername").val() == 'client account name'){
										error = true;
										$("#cusername").addClass('error');
									}
									
									if($("#cpass").val() == '' || $("#cpass").val() == 'enter your password'){
										error = true;
										$("#cpass").addClass('error');
									}
							   return false;
							   });
	
	//contact form ajax -  trebuie modificata
	$('#quick-contact').submit(function(){
									var error = new Array();
									error['status']  = false;
									error['content'] = '';
									$("#error-container").removeClass();
									
									//form validation
									if(!$("#quick-contact p.name input").val()){
										error['status'] = true;
										error['content'] = 'Introduce tu nombre';
									}
									if(!validateEmail($("#quick-contact p.email input").val())){
										error['status'] = true;
										error['content'] = 'Introduce tu email';
									}
									if(!$("#quick-contact p.message textarea").val()){
										error['status'] = true;
										error['content'] = 'Introduce tu mensaje';
										
									}
									//
									
									if(error['status']){
										$("#error-container").addClass('error');
										$("#error-container").html(error['content']);
									    $("#error-container").animate({opacity:"show"}, "fast");
										
									}
									else{
										param = 'name='      + $("#quick-contact p.name input").val() +
										        '&email='    + $("#quick-contact p.email input").val() +
										        '&phone='    + $("#quick-contact p.phone input").val() +
												'&message='  + $("#quick-contact p.message textarea").val();
										
										//show loading
										$("#error-container").addClass('loading');
										$("#error-container").html("Enviando...");
									    $("#error-container").animate({opacity:"show"}, "fast");


									  $.ajax({
										  type: "POST",
										  url: 'requests/contacto.php',
										  cache: false,
										  data: param,
										  success: function(html){
											if(parseInt(html) == 1){
												$('#quick-contact input').attr("value", '');
												$('#quick-contact textarea').attr("value", '');
												$("#error-container").removeClass('loading');
												$("#error-container").addClass('success');
												$("#error-container").html("Hemos recibido tu mensaje.<br/>Gracias!");
											    $("#error-container").animate({opacity:"show"}, "fast");
												setTimeout('$("#error-container").animate({opacity:"hide"}, "slow")', 3000);
								}else{
												$("#error-container").addClass('error');
												$("#error-container").html("Introduce todos los campos!");
												$("#error-container").animate({opacity:"show"}, "fast");
												setTimeout('$("#error-container").animate({opacity:"hide"}, "slow")', 3000);
											}
										  },
										  error: function(){
											  alert('Vuelve a intentarlo');
										  }
										});
									}
							return false;
							});
	//quick quote form focus
	$('#quote-form input, #quote-form textarea').focus(function(){
												$(this).css('background','url(../images/forms/bg-input-quote-focus.jpg) repeat-x');
																});
	$('#quote-form input, #quote-form textarea').blur(function(){
												$(this).css('background','url(../images/forms/bg-input-quote.jpg) repeat-x');
																});
	//quick quote send hover
	$('#quote-form #request-quote').mouseover(function(){
												$(this).attr('src','images/forms/request-quote-hover.png');
																});

	$('#quote-form #request-quote').mouseout(function(){
												$(this).attr('src','images/forms/request-quote.png');
																});
	
	
	//quick quote form ajax
	$('#quote-form').submit(function(){
									var error = false;
									$("#quote-form p").removeClass("error");
									
									//form validation
									if(!$("#quote-form p.name input").val()){
										error = true;
										$("#quote-form p.name").addClass("error");
									}
									if(!validateEmail($("#quote-form p.email input").val())){
										error = true;
										$("#quote-form p.email").addClass("error");
									}
									if(!$("#quote-form p.phone input").val()){
										error = true;
										$("#quote-form p.phone").addClass("error");
									}
									if(!$("#quote-form p.pname input").val()){
										error = true;
										$("#quote-form p.pname").addClass("error");
									}
									if(!$("#quote-form p.pdesc textarea").val()){
										error = true;
										$("#quote-form p.pdesc").addClass("error");
									}
									if($("#quote-form p.budget input").val() == '$400-'){
										error = true;
										$("#quote-form p.budget").addClass("error");
									}
									if(!$("#quote-form p.deadline input").val()){
										error = true;
										$("#quote-form p.deadline").addClass("error");
									}
									
									if(!error){
										param = 'name='      + $("#quote-form p.name input").val() +
										        '&email='    + $("#quote-form p.email input").val() +
												'&phone='    + $("#quote-form p.phone input").val() +
												'&pname='    + $("#quote-form p.pname input").val() +
												'&pdesc='    + $("#quote-form p.pdesc textarea").val() +
												'&budget='   + $("#quote-form p.budget input").val() +
												'&deadline=' + $("#quote-form p.deadline input").val();
												
										//show loading
										$('#quote-c').block({ message: '<h1>Please wait...</h1>',
															  css: { 
																border: '1px solid #FFF', 
																padding: '5px', 
																backgroundColor: '#124B78', 
																color: '#fff' 
															  }
										});
										
										$.ajax({
										  type: "POST",
										  url: 'requests/quote.php',
										  cache: false,
										  data: param,
										  success: function(html){
											if(parseInt(html) == 1){
												$('#quote-c').html('<div id="thank-you"></div>');
												$('#quote-c').unblock();
											}
											else{
											  $('#quote-c').html(html);
											  $('#quote-c').unblock();
											}
										  },
										  error: function(){
											  alert('Please retry');
										  }
										});
									}
									//
	return false;
	});
	
	//quick quote tips
	$('#left-column input').focus(function(){
									  $('#left-column .tip').css("display","block");
									  
									  $('#center-column .tip').css("display","none");
									  $('#right-column .tip').css("display","none");
									  });
	$('#center-column input, #center-column textarea').focus(function(){
									  $('#center-column .tip').css("display","block");
									  
									  $('#left-column .tip').css("display","none");
									  $('#right-column .tip').css("display","none");
									  });
	$('#right-column input').focus(function(){
									  $('#right-column .tip').css("display","block");
									  
									  $('#center-column .tip').css("display","none");
									  $('#left-column .tip').css("display","none");
									  });

	//set cookie if carousel is clicked
	$(".jcarousel-next").click(function(){
								if(!$.cookie('carousel_clicked')){
								    $("#menu").slideDown("slow");
									setTimeout("hide_nav_popup("+ieversion+")", 1500);
								}
								$.cookie('carousel_clicked', 'true');
								});
	
	
	//popup
	if(!$.cookie('carousel_clicked'))
    	setTimeout("show_nav_popup("+ieversion+")", 2000);
		
	//clients login
	$('#c-login').click(function(){
				$('#clients').css('display', 'block');
								 
				 $('#c-close').click(function(){
												$('#clients').css('display', 'none');
							 					$("#clients-login input").removeClass('error');
															  });
				$('#cusername').focus(function(){
												($(this).val()=='client account name')?$(this).val(''):'';
																});
				$('#cusername').blur(function(){
												$(this).val()?'':$(this).val('client account name');
																});
				$('#cpass').focus(function(){
												($(this).val()=='enter your password')?$(this).val(''):'';
																});
					$('#cpass').blur(function(){
												$(this).val()?'':$(this).val('enter your password');
																});
							 	 
					return false;
								 });
	//lightbox
	$('#warmyth li a').lightBox();
});





/*FUNCTIONS*/
function show_nav_popup(ieversion){
	if (ieversion){
		$("#popup").css("display", "block");
		$("#popup").animate({bottom: "25"}, "350");
	}
	else{
		$("#popup").animate({opacity:"show", bottom: "25"}, "350");
	}
}

function hide_nav_popup(ieversion){
	if (ieversion)
		$("#popup").css("display", "none");
	else
		$("#popup").animate({opacity: "hide", bottom: "50"}, "slow");
}


function mycarousel_initCallback(carousel)
{
	$('#menu ul li a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
		setTimeout("hide_nav_popup("+ieversion+")", 1500);
        return false;
    });
};

function mycarousel_reloadCallback(carousel, item, idx, state){
	$('#menu ul li a').removeClass('selected');
	$('#menu ul li a[rel='+idx+']').addClass('selected');
	
	if(idx != 4)
		$('#this-form').hide();

}

function check_for_contact(carousel, item, idx, state){
	if(idx == 4){
		if (ieversion)
			$('#this-form').show();
		else
			$('#this-form').fadeIn();
	}
}

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", "images/"+arguments[i]);
	}
}

function validateEmail(email) {
	var at = email.lastIndexOf("@");

	// Make sure the at (@) sybmol exists and  
	// it is not the first or last character
	if (at < 1 || (at + 1) === email.length)
		return false;

	// Make sure there aren't multiple periods together
	if (/(\.{2,})/.test(email))
		return false;

	// Break up the local and domain portions
	var local = email.substring(0, at);
	var domain = email.substring(at + 1);

	// Check lengths
	if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
		return false;

	// Make sure local and domain don't start with or end with a period
	if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
		return false;

	// Check for quoted-string addresses
	// Since almost anything is allowed in a quoted-string address,
	// we're just going to let them go through
	if (!/^"(.+)"$/.test(local)) {
		// It's a dot-string address...check for valid characters
		if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
			return false;
	}

	// Make sure domain contains only valid characters and at least one period
	if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
		return false;	

	return true;
}
