function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function currentPage() {
     return window.location.pathname.replace('/workInProgress/sito-newvision-ottobre2011', '').indexOf('en') == 1 ? 'en' : 'it';
}

function otherPage() {
    return window.location.pathname.replace('/workInProgress/sito-newvision-ottobre2011', '').indexOf('en') == 1 ? 'it' : 'en';
}

function userLang() {
 return navigator.language.indexOf('en') == 0 ? 'en' : 'it'
}

function redirect() {
    var url = window.location.pathname;
    var isProduction = url.toLowerCase().indexOf('workinprogress') == -1;
    url = url.replace('/workInProgress/sito-newvision-ottobre2011', '');
    var itToEn = {
        '/chi-siamo.html': '/about-us.html',
        '/prodotti.html': '/products.html',
        '/campagne-sms.html': '/sms.html',
        '/msc-crociere.html': '/msc-cruises.html',
        '/contatti.html': '/contact-us.html',
    };
    var enToIt = {
        '/about-us.html': '/chi-siamo.html',
        '/products.html': '/prodotti.html',
        '/sms.html': '/campagne-sms.html',
        '/msc-cruises.html': '/msc-crociere.html',
        '/contact-us.html': '/contatti.html'
    };
    
    if (currentPage() == 'it') {
        url = url.replace('/prodotti/', '/products/');
    }
    else {
        url = url.replace('/products/', '/prodotti/');
    }
    
    if (currentPage() == 'it') {
        for (key in itToEn) {
            url = url.replace(key, itToEn[key]);
        }
    }
    else {
        for (key in enToIt) {
            url = url.replace(key, enToIt[key]);
        }
    }
    if (currentPage() == 'it') {
        url = '/en' + url;
    }
    else {
        url = url.replace(/^\/en/, '');
    }
    if (!isProduction) {
        url = '/workInProgress/sito-newvision-ottobre2011' + url;
    }
    window.location = window.location.protocol + '//' + window.location.hostname + url;
    return;
}

(function() {
    if (readCookie('last_visit') == null) {
        if (currentPage() == 'it' && userLang() == 'en') {
            createCookie('last_visit', 'en', 360);
            redirect();
            return;
        }
        if (currentPage() == 'en' && userLang() == 'it') {
            createCookie('last_visit', 'it', 360);
            redirect();
            return;
        }
    }
    else {
        if (readCookie('last_visit') == 'en' && currentPage() != 'en') {
            createCookie('last_visit', 'en', 360);
            redirect();
            return;
        }
        if (readCookie('last_visit') == 'it' && currentPage() != 'it') {
            createCookie('last_visit', 'it', 360);
            redirect();
            return;
        }
    }
    createCookie('last_visit', currentPage(), 360);
}());

$('.language.button > a').click(function(event) {
    createCookie('last_visit', otherPage(), 360);
    return true;
});

$(document).ready(function() {
	if($.browser.msie && parseInt($.browser.version, 10) == 7){
		$('.on-top>.container>div').css({'position':'relative', 'top':'60px'});
		$('.on-top>.container>.video>.close').css({'top':'-50px'});
		$('.header .menu').css('margin-bottom', '-5px');
	}
    var $usernameLabel = $('<label for="username">Username</label>').css('margin-left', '20px');
    var $username = $('<input type="text" id="username" />');
    
    var $passwordLabel = $('<label for="password">Password</label>');
    var $password = $('<input type="password" id="password" />').css('margin-right', 0);
    
    var $form = $('<form class="client-login"></form>');
    $form.append($usernameLabel).append($username).append($passwordLabel).append($password);
    
    var $error = $('<span class="error">Username e password non corrette.</span>');
    
    var $wrapper = $('<div class="wrapper"></div>').css('right', $('.clients-area').width());
    $wrapper.append($form).append($error);
    
    $('.first.line').append($wrapper);
    
    var $accedi = $('<span class="accedi">Accedi</span>');
    var $close = $('<span class="close"></span>');
    
    $('.first.line').append($close).append($accedi);
    
    $('.clients-area').click(function(event) {
        event.preventDefault();
        $wrapper.animate({width: '390px'}, 400, function() {
            $('.clients-area').hide();
            $close.show();
            $accedi.show()
        });
    });
    
    $close.click(function(event) {
        event.preventDefault();
        if ($form.is(':animated')) {
            return;
        };
        $wrapper.animate({width: '0'}, 400, function() {
            $('.clients-area').show();
            $close.hide();
            $accedi.hide();
            $accedi.text('Accedi');
            $form.css('left', 0);
        })
    });
    
    $accedi.click(function(event) {
        event.preventDefault();
        if ($form.is(':animated')) {
            return;
        };
        if ($(this).text() == "Accedi") {
            $(this).text('Riprova');
            $form.animate({left: '-500px'}, 400)
        }
        else {
            $(this).text('Accedi');
            $form.animate({left: '0'}, 400)
        }
    });
    
});
