globalJSObject = {
	// SHOW the LOGIN FORM + HIDE the REST of ALERTS BAR ----->

	login: function() {
		
		if ( $("#login_bar").length ) {
			var alerts = $("#alerts");
		
			$(".showLoginForm").click(function (){
				alerts.animate({top:'31px',opacity:'0'});
				$(".username").focus();
				return false;
			});

			$(".hideLoginForm").click(function () {
				alerts.animate({'top':'0',opacity:'1'});
				return false;
			});
			
			/* return state if master navbar is clicked */
			$("#nav a, #nav input").click(function(){
				alerts.animate({'top':'0',opacity:'1'},150);
			});
		}
	},

	// DROPNAVS ----->

	dropNavs: function(){
	
		$('#nav a.toggleDropNav').bind('click', function(e) {
		
			var master = $(this).closest("li");	
			var href = $(this);
			var target = $(this).parent().siblings("ul");
									
			href.addClass('active');
			
			target.slideToggle('fast');
							
			e.preventDefault();
				
			master.mouseleave(function() {
				setTimeout(function() {
					target.slideUp(200);
					href.removeClass('active');
				}, 300);
			});
		});
	},
	
	// HOME PAGE GRID TABS
	
	gridTabs: function(){
	
		$("#home #galleries.grid").hide();
	
		var stories_href = $("#page-nav .next a").attr("href");
		var stories_text = $("#page-nav .next a").text();
		
		var host = top.location.host;
		if ( host === "www.jaunted.com" ) { host = '/travel-photos' }
		else if ( host === "www.hotelchatter.com" ) { host = '/hotel-photos' }
		else if ( host === "www.vegaschatter.com" ) { host = '/vegas-photos' }

		$(".gridheader .nav a").click(function(){
		
			var index = $(".nav a").index(this),
				nav = $(".gridheader li"),
				thisNav = $(this).parent(),
				gridView = $(".gridcontainer > ul");
			
			if( !thisNav.data("active") )
			{
				nav.removeClass("active");
				$(this).parent().addClass("active");
				
				gridView
					.css('display','none')
					.eq(index).css('display','block');
			}

			if( $(this).parent().attr("id") === "image-galleries" ) {
				$("#page-nav .next a")
					.attr({ href: host })
					.text("More Galleries")
			} else if( $(this).parent().attr("id") === "more-recent-stories" ) {
				$("#page-nav .next a").attr("href", stories_href).text(stories_text);
			}

			return false;
		});
	},

	// DISALLOW NAVBAR SEARCH CLICK THROUGH WITHOUT FIRST ENTERING SEARCH TERM

	searchTermIsEntered: function(){
		$('#nav form').submit(function(){
				
				var navInput = $('#nav input[type="text"]');
				
	        	if ( navInput.val() === '') {
	        		 navInput.focus();
	        		 return false;
	       		}
	    });
	},
	
	// HIDE REF_TAG BAR ( incoming from sibling ) ----->
	
	refTag: function(){
		if ( $("#ref_tag").length ) {
			$("#ref_tag .close a").click(function () {
				$("#ref_tag").animate({'height':'0',opacity:'0'});
				return false;
			});
		}
	},
	
	// FOCUS on COMMENT FORM after CLICKING 'POST A COMMENT' ----->
	
	focusCommentInput: function(){
			$("#post-a-comment").click(function () {
				$("#standard-login-box input").focus();
			});
	},
	
	// MAKE GRID STORIES FULL ROLLOVERS ----->
	
	gridHover: function(){
		
		$(".grid a.more em").hide();
	
		$(".grid li.hentry").hover(function() {
				$(this).addClass('hover');
			}, function() { 
				$(this).removeClass('hover');
		});
	},

	// FIND RATIO of IMAGES and IMPROVE VIEW
	homeGalleryResize: function(){
	
		var img = $('#home #galleries .crop img');
		
		img.each(function(){
		
			var img_width = $(this).width();
			var img_height = $(this).height();
			var width_ratio = (333 / img_width);
			var height_ratio = (200 / img_height);
			var scale_to_width;
	
			if ( width_ratio > height_ratio ) {
				scale_to_width = img_width * width_ratio; 
			} else {
				scale_to_width = img_width * height_ratio;
			}
			
			img.width(scale_to_width);
		
		});
	},
	
	// ADD #SECTION2 HOVER EFFECTS FOR IE6. THIS IS ONLY REQUIRED FOR IE6. OTHER BROWSERS HAVE FULL CSS SUPPORT ----->
	
	sidebarHoverIE: function(){
		if($.browser.msie && /6.0/ || /7.0/.test(navigator.userAgent)){
			
			var sidebarHover = $("#section2 li.hentry");
			
			sidebarHover.css('margin-bottom',1);
			sidebarHover.hover(function() {
					$(this).addClass('ie6-hover');
				}, function() {
					$(this).removeClass('ie6-hover');
			});
		}
	},
	
	hotelDetailSlug: function(){
		$('.book-now, .map-nav a, .desc-nav a').click(function(){
			var link = $(this),
				parent_li = link.closest('li'),
				parent_li_class = link.closest('li').attr('class'),
				parent_lis = parent_li.siblings(),
				parent_ul = $(this).closest('ul'),
				active_li = $(this).closest('.js-hook'),
				book_now = $('.book-now', active_li),
				booking_href = book_now.attr('href'),
				my_box = active_li.find('.hotel-booking-box'),
				text = book_now.text();
			
				function navControls() {
					if(parent_li_class === 'map-nav' || parent_li_class === 'desc-nav') {
						parent_lis.removeClass('active');
						parent_li.addClass('active');
						var control_class = 'control-' + parent_li_class;
						active_li.removeClass('control-map-nav control-desc-nav').addClass(control_class);	
						}		
					}
								
			if( active_li.hasClass('active') && ( (parent_li_class === 'map-nav') || (parent_li_class === 'desc-nav') )) {
					navControls();
					return false;	
			} else {
				if ( !(active_li.hasClass('loaded'))) {
					$.ajax({
					  url: booking_href,
					  success: function(html){
					    $('.slug', active_li).after(html);
					    active_li.addClass('loaded');
					  }
					});
				}
	
				book_now.text( (text == "Check Rates") ? "Close" : "Check Rates");
				my_box.slideToggle('fast');
				navControls();
				active_li.toggleClass('active');
				
				$('table#fc').hide();
				
				return false;
			}
		});
	}
};

$(document).ready(function(){	

	globalJSObject.login();
	globalJSObject.dropNavs();
	globalJSObject.gridTabs();
	globalJSObject.gridHover();
	globalJSObject.sidebarHoverIE();
	globalJSObject.refTag();
	globalJSObject.focusCommentInput();
	globalJSObject.searchTermIsEntered();
	globalJSObject.homeGalleryResize();
	globalJSObject.hotelDetailSlug();
	
	$('#galleries.grid li:lt(2)').wrapAll('<ul class="pair" />');

});
