var panels = new Object();
var _autoScroll = false;

$(document).ready(function() {
	if ($('body').hasClass('front')) {
		$('.scroll_link').click(function(){
			DiscoveryFrontXScroll($(this).attr('href'));
			return false;
		});	
		
		// Initialize panels Object to efficiently track panel sizes and position
		var _i = 0;
		var id, $panel;

		window.onscroll = function() {
	    if(!_autoScroll && window.XMLHttpRequest) {
					DiscoveryCheckScrollPosition();
	    }
		};
		
		// Reset the panel margins based on the viewport size.
		var resizeTimer;
		$(window).resize(function() {
		    clearTimeout(resizeTimer);
		    resizeTimer = setTimeout(DiscoveryContentMargins, 100);
		});

		$("div.states a").click(function(){
			$("div.markets").hide();
			var target = "#" + $(this).attr("rel");
			$(target).show();
			return false;
		});

		$('.overlay').qtip({
		   	content: {
		   		title: {
		   			text: "<h1>Destinations</h1>",
		   			button: "Close"
		   		},
		      	text: $("#destinations")
		   	},
		   	position: {
		      	my: 'center center',
		      	at: 'center center',
		      	target: $(window)
		   	},
		   	show: {
			  	event: 'click',
		      	modal: true
		   	},
			hide: {
				event: 'unfocus'
			},
			style: {
				classes: "ui-tooltip-dmi ui-tooltip-rounded",
				tip:{
					corner: "center center"
				}
			}
		});

		$('.buy_overlay').qtip({
		   	content: {
		   		title: {
		   			text: "<h1>Destinations</h1>",
		   			button: "Close"
		   		},
		      	text: $("#buy_destinations")
		   	},
		   	position: {
		      	my: 'center center',
		      	at: 'center center',
		      	target: $(window)
		   	},
		   	show: {
			  	event: 'click',
			  	modal:true
		   	},
			hide: {
				event: 'unfocus'
			},
			style: {
				classes: "ui-tooltip-dmi ui-tooltip-rounded",
				tip:{
					corner: "center center"
				}
			}
		});

		$('.contact_overlay').qtip({
		   	content: {
		   		title: {
		   			text: "<h1>Contact Our Local Publishers</h1>",
		   			button: "Close"
		   		},
		      	text: $("#contact_destinations")
		   	},
		   	position: {
		      	my: 'center center',
		      	at: 'center center',
		      	target: $(window)
		   	},
		   	show: {
			  	event: 'click',
			  	modal:true
		   	},
			hide: {
				event: 'unfocus'
			},
			style: {
				classes: "ui-tooltip-dmi ui-tooltip-rounded",
				tip:{
					corner: "center center"
				}
			}
		});
		
		$("#contact_destinations div.markets ul li a").click(function(){
			$(".contact_info").hide();
			var target = "#" + $(this).attr("rel");
			console.log(target);
			$(target).show();
			return false;
		});
		
		DiscoveryContentMargins();
	}
	
	if ($('body').hasClass('page')) {
		DiscoveryContentMargins();
	}
});


function DiscoveryCheckScrollPosition() {
	var y = $(window).scrollTop();
	var wh = $(window).height();
	var $div, $li;
	$("div.panel").each(function(){
		var pan_y = $(this).offset().top;
		if (pan_y > y && pan_y < y+(wh/3)) {
			$li = $('div#header ul#nav li a[href$='+$(this).attr('id')+']').parent();
			if (!$li.hasClass('active')) {
				$('div#header ul#nav li.active').removeClass('active');
				$li.addClass('active');
			}
			return;
		}
	});
}

function DiscoveryContentMargins() {
	var wh = $(window).height();
	var hh = $("div#header").height();
	$("div.panel").each(function(){
		var ph = $(this).height();
		if($(this).attr("id") != "contact" && !$(this).hasClass("top")){
			var dif = (wh - hh) - ph;
			var marg = dif/2;
			if (marg > 0){
				$(this).css({"margin-top" : marg+"px", "margin-bottom" : marg+"px"});
			}
		} else if($(this).attr("id") == "contact") {
			var fh = $("div#footer").outerHeight(true);			
			var dif = ((wh - fh) - hh) - ph;
			var marg = dif/2;
			if (marg > 0) {
				$(this).css({"margin-top" : marg+"px", "margin-bottom" : marg+"px"});
			}
		} else if($(this).hasClass("top")){
			var dif = (wh - hh) - ph;
			var marg = dif/2;
			if (marg > 0){
				$(this).css({"margin-bottom" : marg+"px"});
				if ($('body').hasClass('front') && marg <= 15) {
					marg += 25;
					$(".top_spacer").height(marg+"px");
				} else {
					$(".top_spacer").height(marg+"px");
				}
			}
			if ($('body').hasClass('front') && marg <= 0) {
				marg = 40;
				$(".top_spacer").height(marg+"px");
			}
		}
	});
}


function DiscoveryFrontXScroll(hash){
	_autoScroll = true;
		//we are using human readable hashes to both prevent default hash behavior and create better links. Adds some code but will make for better bookmarks, urls etc.
	switch(hash)
	{
	case "#welcome":
	  window.location.hash = "#home";
	  break;
	case "#explore":
	  window.location.hash = "#explore_our_destinations";
	  break;
	case "#use":
	  window.location.hash = "#use_our_maps";
	  break;
	case "#map":
	  window.location.hash = "#get_a_map";
	  break;
	case "#advertise":
	  window.location.hash = "#advertise_with_us";
	  break;
	case "#join":
	  window.location.hash = "#join_our_family";
	  break;
	case "#illustration":
	  window.location.hash = "#create_your_own";
	  break;
	case "#contact":
	  window.location.hash = "#contact_us";
	  break;
	case "#see":
	  window.location.hash = "#see_how_they_are_used";
	  break;
	}
	$('div#header ul#nav li.active').removeClass('active').parent().find('a[href="'+hash+'"]').parent().addClass('active');
	var parts = hash.split("#");
    var top = $("#"+parts[1]).offset().top;
		// calculate height diff to center viewport unless contact page.
		if (parts[1] != "contact"){
			if ($(window).height() < $('div#'+parts[1]).outerHeight()){
				var h = ($('div#'+parts[1]).height()/2) - $(window).height();
			} else {
				var h = ($(window).height() - $('div#'+parts[1]).height())/2;	
			}
			if (h>0) {
				top -= h-70;
			}
			if (h<0 && parts[1] == "welcome") {
				$('html, body').animate({scrollTop:'0'}, 500, function() {
						_autoScroll = false;
				});
				return;
			}
		} else {
			top -= 20;
		}
		if (parts[1] == "explore" && typeof(viewer) == "object"){
			viewer.viewport.goHome();
		}
    $('html, body').animate({scrollTop:top}, 500, function() {
			_autoScroll = false;
	});
}


function DiscoveryFrontScroll(e) {
	_autoScroll = true;
	var href = $(this).attr('href');
	$('div#header ul#nav li.active').removeClass('active').parent().find('a[href="'+href+'"]').parent().addClass('active');

    e.preventDefault();
    var parts = this.href.split("#");
    var top = $("#"+parts[1]).offset().top;
		// calculate height diff to center viewport unless contact page.
		if (parts[1] != "contact"){
			var h = ($(window).height() - $('div#'+parts[1]).height())/2;
			if (h>0) {
				top -= h-70;
			}
		} else {
			top -= 20;
		}
		if (parts[1] == "explore"){
			viewer.viewport.goHome();
		}
    $('html, body').animate({scrollTop:top}, 500, function() {
			_autoScroll = false;
		});	
}




