var content_id = '#content';

function aniSelector(el) {
	if (el) {
		var element = '#'+el;
		var offset = $(element).offset();
		var left = (offset) ? offset.left + ($(element).width() / 2) - ($('#selector').width() / 2) : false;
	}
	var selWidth = $('#selector').width();
	$('#selector').animate({
        'left': (left) ? left : '-'+selWidth+'px'
    },{
        duration: 500,
        queue: false
    });
}

function page_load(hash) {
    $('#content').hide("blind", function() {
        $('#content').empty();
        if (hash) {
            $.get('http://nodesign.in/' + hash, function(data) {
                var target = hash.split('.html');
                /* parsing target */
				aniSelector(target[0]);
				switch(hash) {
					case 'portfolio.html':
						$.getScript('/js/jquery.tools.min.js', function(){
							$('.scrollable').scrollable({
								size: 3,
								next: '.right', 
								prev: '.left'
							});
							$('.left,.right,h3').mouseover(function(){
								$(this).css('background-color','#ee0000');
							});
							$('.left,.right,h3').mouseout(function(){
								$(this).css('background-color','#333');
							});
							$('img').mouseover(function(){
								$(this).parent().parent().find('h3').css('background-color','#ee0000');
							});
							$('img').mouseout(function(){
								$(this).parent().parent().find('h3').css('background-color','#333');
							});
							document.title = "Портфолио «Nodesign»";
						});  
						break;    
					case 'about_us.html':
					  	document.title = "О «Nodesign»";
					  	break;
					case 'services.html':
					  	document.title = "Услуги «Nodesign»";
						break;
					case 'contacts.html':
					  	document.title = "Контакты «Nodesign»";
						$('input[type="button"]').click(function(){
							var name = $('input[name="name"]').val();
							var mail = $('input[name="mail"]').val();
							var subject = $('input[name="subject"]').val();
							var message = $('textarea[name="message"]').val();
							$.post("mail.php", { name: name, mail: mail, subject: subject, message: message }, function(data){
								switch (data) {
									case 'ok':
									$('#notify').css('background-color','#65DC3C').text('Ваше сообщение отправлено! Скоро мы его прочтем.').show('highlight');
									break;
									case 'nok':
									$('#notify').css('background-color','#f11').text('Произошла неведомая фигня и ваше сообщение не отправилось.').show('highlight');
									break;
									case 'poo':
									$('#notify').css('background-color','#f11').text('Пожалуйста, заполните все поля.').show('highlight');
									break;
								}
						  	});
						});
					  	break;
					default:
						aniSelector();
						document.title = "«Nodesign»";
						break;
				
				}
                /* end */
				$('#logo a').attr('href','#');
				$('#content').html(data);
				$('#content').show("blind");
            });

        }
        else {
			$.get('welcome.html', function(data) {
				aniSelector(false);
				document.title = "«Nodesign»";
				$('#logo a').attr('href','http://nodesign.in');
				$('#content').html(data);
				$('#content').show("blind");
			});
        }
    });
}

$(function() {
    var http_regex = /^(http:\/\/)+/;
    var sharp_regex = /#(.*)/;
    $.historyInit(page_load);
    page_load();
    $('a').live("click", function() {
        if (http_regex.exec($(this).attr('href'))) {
            window.location = $(this).attr('href');
            return false;
        }
        var hash;
        hash = sharp_regex.exec(window.location);
        if (($(this).attr('href') == '#') ||
        (hash && (hash[1] == $(this).attr('href')))) {
            window.location = '#';
            return false;
        }
        window.location = '#' + $(this).attr('href');
        hash = sharp_regex.exec(window.location);
        $.historyLoad(hash[1]);
        return false;
    });
});