﻿jQuery.ajaxSetup({contentType: "application/x-www-form-urlencoded; charset=utf-8"});

document.cookie = 'resolution=' + screen.width;

var nl_name_default = "";
var nl_mail_default = "";

$(document).ready(function(){
	// gallery
	$("div.gallery a").fancybox({ 'hideOnContentClick': true, 'overlayShow': false });
	
	// slideshow
	setInterval( "slideSwitch()", 3000 );
	
	// newsletter
	nl_name_default = $('#nl_name').val();
	nl_mail_default = $('#nl_mail').val();
	
	$('#nl_add').click(function(){ nlRegister(); })
	$('#nl_name, #nl_mail').click(function(){ clearForm(this); })
	$('#nl_name, #nl_mail').focus(function(){ clearForm(this); })
	$('#nl_name, #nl_mail').blur(function(){ fillForm(this); })
});

/* all functions */

function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

/*
$(function() {
    
});
*/

var regEmail = /^.+\@.+\..+$/;

function encodeString(text) {
	encoded = text.replace("///g","%2F");
	encoded = encoded.replace("/?/g","%3F");
	encoded = encoded.replace("/=/g","%3D");
	encoded = encoded.replace("/&/g","%26");
	encoded = encoded.replace("/#/g","%23");
	encoded = encoded.replace("/@/g","%40");
	encoded = encoded.replace("/r/g","");
	encoded = encoded.replace("/n/g","%0A");
	
	return encodeURIComponent(encoded);
}

function nlRegister() {
	var nl_name = $('#nl_name').val();
	var nl_mail = $('#nl_mail').val();
	
	$('#nl_load').hide();
	$('#nl_load').html('<img src="/data/img/loader_mini.gif" alt="" style="vertical-align: middle" />');
	$('#nl_load').fadeIn('fast');
	
	if(nl_name == '') {
		$('#nl_load').html( $('#nl_error_name').val() );
		return false;
	}
	
	if(nl_mail == '' || !regEmail.test(nl_mail)) {
		$('#nl_load').html( $('#nl_error_mail').val() );
		return false;
	}
	
	$('#nl_name, #nl_mail').attr("disabled");

	$.ajax({
		 type: "POST",
		 url: "/inc/ajax_register_newsletter.php",
		 data: "nl_name="+nl_name+"&nl_mail="+nl_mail+"&lang="+$('#lang').val(),
		 success: function(msg){
		 	$('#nl_load').html( msg );
			$('#nl_name, #nl_mail').removeAttr("disabled");
		 }
	});
}

function clearForm(el){
	if(el.id == "nl_name" && el.value == nl_name_default) $('#nl_name').val("");
	if(el.id == "nl_mail" && el.value == nl_mail_default) $('#nl_mail').val("");
}

function fillForm(el){
	if(el.id == "nl_name" && el.value == "") $('#nl_name').val( nl_name_default );
	if(el.id == "nl_mail" && el.value == "") $('#nl_mail').val( nl_mail_default );
}
