$(document).ready(function(){

	$("[placeholder]").textPlaceholder();


	if($('#mytz').length){
		var tz = jstz.determine_timezone();
		var yourtime = '<strong>Your time ('+tz.name()+'):</strong> ';
		
		if (gmt1) {
			yourtime += gmt1.toLocaleTimeString().substr(0, 5) + ' - ' + gmt2.toLocaleTimeString().substr(0, 5);
			if (gmt3)
				yourtime += ' / ' + gmt3.toLocaleTimeString().substr(0, 5) + ' - ' + gmt4.toLocaleTimeString().substr(0, 5);

			$('#mytz').html(yourtime);
		}
	}

	//Home sidebars
	$("#bluesidebar, #greensidebar").easySlider({
		auto: true,
		controlsShow: false,
		vertical: true,
		speed: 500, 
		pause: 8000,
		continuous: true
	});
	
	//Loader logo rotation
	$(".footer").mouseenter(rotateLoaderLogo);
	
	//SQLBI Logo transform
	/*$(window).scroll(function(e) {
		var s=$(this).scrollTop();
		var tf=(s>=85);
		$("#logo").toggleClass("smalllogo", tf);
		$(".navdecol").toggle(!tf);
		//log(s);
	});*/
	
	//TOC controls
	var parent_toggle = $(".current-article").closest("ul.toc2");
	if (typeof(parent_toggle.attr("ID")) == 'undefined') parent_toggle = $(".current-article").children("ul.toc2");
	parent_toggle.toggle();
	parent_toggle.parent().find(".toctoggle").html('-');
	
	$(".toctoggle").click(function(e){
			e.preventDefault();
	}).mouseup(function(){
		
		var source = $(this);
		var target = $(this).parent().find("ul");
		$("ul.toc2").each(function(){
			
			if ($(this).attr("ID") == target.attr("ID")) {
				$(this).toggle("fast");
				source.html() == '+' ? source.html('-') : source.html('+');
			} else {
				$(this).toggle(false);
				$(this).parent().find(".toctoggle").html('+');
			}
		});
		
		
	});
	
	//Newsletter
	$("#nlemail").keypress(function(event){
		if (event.which == 13 && ($("#nlemail").val() != "" && $("#nlemail").val() != $("#nlemail").attr("placeholder"))) {
			event.preventDefault();
			myNewsletterSubscribe();
		} 
	});

	$("#nlsubmit").click(function(event){
		if ($("#nlemail").val() != "" && $("#nlemail").val() != $("#nlemail").attr("placeholder")) {
			event.preventDefault();
			myNewsletterSubscribe();
		}
	});
	
	$("#nlnl").click(function(){
		$("#nlemail").toggle("fast");
		$("#nlfeedback").html('');
	});
	//$("#nlemail").toggle($("#nlnl").is(":checked"));
	
	$("#nldownload").click(function(event){
		if ($("#nlnl").is(":checked")){
			event.preventDefault();
			myNewsletterSubscribe($(this).attr('href'));
		}
	});
	
	//Consulting
	
	$(".presel").click(function(event){
		event.preventDefault();
		var lnk = $(this).attr("href").split(",");
		$("#askproduct").val(lnk[1]);
		$("#askname").focus();
		document.location.href=lnk[0];
	});
	
	
	//Contact
	$("#askform").validate({
		rules: {
				askname: "required",
				askemail: "required",
				askcompany: "required"
		},
		submitHandler: function(form) {
			$.ajax({
				type: "POST",
				url: "/wp-admin/admin-ajax.php",
				data: $("#askform").serialize(),
				dataType: "text",
				success: function(response) {$("#formfeedback").html('<strong>Thank you!</strong><br>We\'ll contact you very soon.'); },
				error: function(response) {$("#formfeedback").html('<span class="err"><strong>Opsss!</strong><br>There was a problem with the form.<br>Please contact us to <a href="mailto:info@sqlbi.com">info@sqlbi.com</a></span>');}
			});
			$("#askform").hide('slow');
			return false;
		}
	});

});

var logo_can_rotate=true;
function rotateLoaderLogo(){
	if (logo_can_rotate) {
		logo_can_rotate=false;
		$(".loadingwheel").rotate(0);
		$(".loadingwheel").rotate({animateTo:360, callback:function(){logo_can_rotate=true;}});
	}
}

function myNewsletterSubscribe(callbackUrl){
  
	var lang;
	if (navigator.userLanguage)
		lang = navigator.userLanguage.substring(0,2).toLowerCase();
	else
		lang = navigator.language.substring(0,2).toLowerCase();
  	
	$('#nlfeedback').html("...");
		
	$("#nlform").hide("slow");
	var args = { 
		action : "check_email_and_subscribe", //"alo_em_pubblic_form_check",
		alo_em_opt_name : "user", 
		alo_em_opt_email : $('#nlemail').val(),
		alo_em_form_lists : $('#nllist').val(),
		alo_em_lang_code : lang,
		alo_em_error_email_incorrect : "Warning: The email address is not correct!",
		alo_em_error_on_sending : "Error during sending: please try again.",
		alo_em_txt_ok : "ok"
	};
	
	$.post('/wp-admin/admin-ajax.php', args, function(data) {
		
		if (data == "0") { 
			data = "Error: try to logout first...";
		} else if (data == "invalid" || data == "invalid-1") { 
			data = "The email is not valid!";
		} else if (data == "ok") { 
			data = "Subscription successful! <br>You'll receive an email with an activation link, check your inbox."; 
			// Your subscription was successfully activated.<br>Thank you!
			
			if (callbackUrl) 
				window.location.href = callbackUrl;
				
		} else { 
			$('#nlemail').focus();
			$("#nlform").show();
		}
		$('#nlfeedback').html(data);
		
		
	});

}


