$(document).ready(function () {
    var $panels = $('#slider .scrollContainer > div');
    var $container = $('#slider .scrollContainer');
    var horizontal = true;
    if (horizontal) {
        $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }
    var $scroll = $('#slider .scroll').css('overflow', 'hidden');
    function selectNav() {
        $(this)
            .parents('ul:first')
                .find('a')
                    .removeClass('selected')
                .end()
            .end()
            .addClass('selected');
    }
    $('#slider .navigation').find('a').click(function(){
		if($(this).hasClass('no-slide')) { 
			window.location = $(this).attr('href').val();
		} else {
			selectNav;
		}			
	});
    /*
    function trigger(data) {
        var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
        selectNav.call(el);
    }*/
    
    function trigger(data) {
	    var div = '#' + data.id; 
	    var divHeight = $(div).height()+24;
	    $('#slider .scroll').animate({ height: divHeight },100);
	    var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
	    selectNav.call(el);
    }
    
    if (window.location.hash) {
        trigger({ id : window.location.hash.substr(1) });
    } else {
        $('ul.navigation a:first').click();
    }
    var offset = parseInt((horizontal ? 
        $container.css('paddingTop') : 
        $container.css('paddingLeft')) 
        || 0) * -1;

    var scrollOptions = {
        target: $scroll,
        items: $panels,
        navigation: '.navigation a',
        axis: 'xy',
        onAfter: trigger, 
        offset: offset,
        duration: 500,
        hash:true
    };
    $('#slider').serialScroll(scrollOptions);
    $.localScroll(scrollOptions);
    scrollOptions.duration = 1;
    $.localScroll.hash(scrollOptions);
    
	$("#vslider").wslide({
		width: 666,
		height: 420,
		autolink: 'vnav'
	});
	
	Clients();
	
	$("#openClient").click(function(){
		$("#openClient").animate({ opacity: "0" },{queue: false, duration:1000})
			.animate({height: "0px", width: "0px" }, 500);
		$("#openClient").html("");
	});
	
	contactValidate();
});

this.Clients = function(){
	$("ul li div.client").click(function(){
		clientContent = $(this).html();
        $("#openClient").append(clientContent);
		$("#openClient").animate({ opacity: "100%" },{queue: false, duration:500})
			.animate({ width: "600px" }, { queue: false, duration:500 })
			.animate({ height: "250px" },{ queue: false, duration:1000 });
		$("#openClient").css("display","inline");
	});	
};
this.contactValidate = function(){
	$("#contact_submit").click(function(){
		var errors = "";
		if($("#company").val() == ""){errors += "<li>Company Name</li>\n";}
		if($("#name").val() == ""){errors += "<li>Your Name</li>\n";}
		if($("#title").val() == ""){errors += "<li>Your Title</li>\n";}
		if($("#email").val() == ""){errors += "<li>Your Email Address</li>\n";}
		if($("#phone").val() == ""){errors += "<li>Your Phone Number</li>\n";}
		if($("#message").val() == ""){errors += "<li>Your Message</li>\n";}
		if(errors){
			$("#output").html('<p><strong>Please fill in the following required fields:</strong></p><ul>'+errors+'</ul><br /><p align="center"><a class="close-button"><span>close</span></a></p>\n');
			contactOutput();
		}else {
			$.post("/contact.php", { company: $("#company").val(), name: $("#name").val(), title: $("#title").val(), phone: $("#phone").val(), email: $("#email").val(), message: $("#message").val() });
			$("#output").html('<p align="center"><strong>Thank you for contacting RMP Media Analysis.</strong></p><p align="center">Your message has been received and we will be contacting you shortly.</p><br /><p align="center"><a class="close-button"><span>close</span></a></p>\n');
			contactOutput();
			$("#company").val("");$("#name").val("");$("#title").val("");$("#email").val("");$("#phone").val("");$("#message").val("");
		}
	});
	$("#output").click(function(){
		$("#output").animate({ opacity: "0" },{queue: false, duration:1000})
			.animate({height: "0px", width: "0px" }, 500);
		$("#output").html("");
	});
};
this.contactOutput = function(){
	$("#output").animate({ opacity: "100%" },{queue: false, duration:500})
		.animate({ width: "300px" }, { queue: false, duration:500 })
		.animate({ height: "250px" },{ queue: false, duration:1000 });
	$("#output").css("display","inline");
};

function getClients(page) {
	$.getJSON('/dataproviders/clients.php', {page: page}, function (results) {
		$('#clients-container').html(results.clients);
		$('#client-pagination').html(results.navigation);
		Clients();
	});
}