$(document).ready(function(){

	if($.browser.msie) {
		$('#nav li').click(function(e) {
			$(this).addClass('active').siblings().removeClass('active');
		});
		$('#nav li').eq(0).addClass('active');
		//alert("Internet Explorer");
	} 
	
	else {
		$.getScript("js/jquery/jquery.lavalamp.1.3.3.js", function(){
			$('#nav').lavaLamp({
				fx: 'easeOutBack',
				speed: 700,
				autoReturn: true
			});
		})
	}
	
	$('#nav a').click(function(e) {
		if($(this).text() !== "BLOG") {
	 		e.preventDefault();
			document.location.hash=$(this).attr("href");
		}
	});
	
	$('#home-slider').scrollable({
		items: '.home-container',
		easing: 'linear',
		item: '.section',
		size: 1,
		speed: 350,
		prev: '.homeprev',
		next: '.homenext',
		keyboardSteps: 1
	}).navigator({
		navi: "#tabs",
		naviItem: 'li',
		activeClass: "active"
	});
	
	$('#homediv').nivoSlider({
		directionNav:false, //Next & Prev
		controlNav:false,
		pauseTime: 5000
	});
	
	$('.home-container .scrollable').scrollable({
		items: '.container',
		circular: true,
		size: 1,
		easing: 'linear'
	}).navigator();
	
	$('#can-switcher div').click(function() {
		var relId = $(this).attr('rel');
		//console.log(relId);
		$('#can #'+relId).fadeIn().siblings().fadeOut();
	});
	
	$('#aboutdiv a').click(function(e) {
		var rel = $(this).attr('rel');
		$(this).siblings().removeClass('active');
		$(this).addClass('active');
		if(rel == 'murielwiener') {
			$('div#michaelblake').fadeOut();
			$('div#murielwiener').fadeIn();
			
		} else if(rel == 'michaelblake') {
			$('div#murielwiener').fadeOut();
			$('div#michaelblake').fadeIn();
		}
		e.preventDefault();
	});
	
	$('#search form').submit(function(e) {
		//console.log($(this).serialize());
		$.post('newsletter.php', $(this).serialize()+'&submit=1', function(result) {
			var res = $.parseJSON(result);
			//console.debug(res);
			if(res['status'] === "success") {
				alert("Thank you! You have successfully added to our newsletter");
			} else {
				alert("Please enter a valid email");
			}
		});
		e.preventDefault();
	});
	$('#form form').submit(function(e) {
		$("#form form label .error").slideUp().remove();
		$('#form form input[type=submit]').attr("disabled", "disabled");
		$.post('mail.php', $(this).serialize()+'&submit=1', function(result) {
			var res = $.parseJSON(result);
			if(res['status'] === "success") {
				$('#form').prepend(res['confirm']);
				$('#form form').fadeOut();
				$('#confirm').delay(5000).fadeOut('normal', function() {
					$('#form form').fadeIn();
				});
				$('#form form input[type=submit]').removeAttr('disabled');
			} else {
				var errors = res['errors'];
				for(var field in errors) {
					//console.log(field);
					$('#form form label[for='+field+']').append('<span style="display: none;" class="error">'+errors[field]+'</span>');
				}
				$("#form form label .error").slideDown();
				$('#form form input[type=submit]').removeAttr('disabled');
			}
		});
		e.preventDefault();
		
	});
	// Document title
    var title = document.title;

    
    // Default tracker mock
    var track = function() {
        log('track: ' + arguments[0]);
    };

    // Serialization utility
    var serialize = function(obj, re) {
        var result = [];
        $.each(obj, function(i, val) {
            if ((re && re.test(i)) || !re)
                result.push(i + ': ' + (typeof val == 'object' ? val.join 
                    ? '\'' + val.join(', ') + '\'' : serialize(val) : '\'' + val + '\''));
        });
        return '{' + result.join(', ') + '}';
    };
	var loadSection = function(value) {
		switch(value) {
			case '/':
			break;
			case 'blog':
				window.location = "http://blog.designerliners.com/";
			break;
			default:
				if($.browser.msie) {
					$('#section-container > div').fadeOut();
					$('#section-container #'+value+'div').fadeIn().addClass('active');
				} else {
					$('#section-container > div').fadeOut();
					//$('#section-container #'+value+'div').fadeIn().siblings().fadeOut();
					$('#section-container #'+value+'div').fadeIn().addClass('active');
				}
			break;
		}
	};
	
	
	
	$.address.init(function(event) {
		/*
        console.debug('init: ' + serialize({
            value: $.address.value(), 
            path: $.address.path(),
            pathNames: $.address.pathNames(),
            parameterNames: $.address.parameterNames(),
            queryString: $.address.queryString()
        }))
        */
    
        loadSection($.address.value());
    }).change(function(event) {
        //console.debug('change: ' + serialize(event, /parameters|parametersNames|path|pathNames|queryString|value/));
        var names = $.map(event.pathNames, function(n) {
            return n.substr(0, 1).toUpperCase() + n.substr(1);
        }).concat(event.parameters.id ? event.parameters.id.split('.') : []);
        var links = names.slice();
        var match = links.length ? links.shift() + ' ' + links.join('.') : 'Home';
        $('a').each(function() {
            $(this).toggleClass('selected', $(this).text() == match);
        });
        $.address.title([title].concat(names).join(' | '));
        loadSection($.address.value());
    });
    
 
});


