jQuery(document).ready(function($){
	/* Toggle tabs & tab planes */
	$("#profile_tabs li").click(function() {
		/* tabs */
		$("#profile_tabs li.current").removeClass("current");
		$(this).addClass("current");
		
		/* planes */
		$(".tab_plane.current").removeClass("current");
		$("#" + $(this).attr("id") + "_plane").addClass("current");
		if ($(this).attr("id") != "tab_2") {
			$("#sections").show();
		}
		else {
			$("#sections").hide();
		}
	});
	
	/* toggle side sections */
	$("#sections .section span.toggle, #sections .section h3.clickable").click(function() {
		if ($(this).parent().parent().hasClass("closed"))
			$(this).parent().parent().removeClass("closed");
		else
			$(this).parent().parent().addClass("closed");
	});

	/* special offers */
        $(".specialoffer_box a").click(function(evt) {
		evt.preventDefault();
		if ($(".specialoffer .offer_link").length > 0) {
			$(".specialoffer .offer_link a").eq(0).click();
		}
	});
	
	$(".specialoffer_box a").click(function() {
		$(".section_icon.specialoffer_icon h3.clickable").click();
	});
	
	$(".specialoffer .offer_link a").nyroModal({minWidth: 580, minHeight: 270});

	$("a.show_img_tab").click(function() {
		$("#sections").hide();
		$("ul#profile_tabs li#tab_2").click();
	});

	/* image gallery popups */
	$("a.gallery1").nyroModal({type: 'image'});
	$("a.gallery2").nyroModal({type: 'image'});
	
	/* set the file type for gallery */
	$.fn.nyroModal.settings.processHandler = function(settings) {
		var from = settings.from;
		if (!settings.type && from && from.href) {
			var arr = from.href.split('?');
			//if carrying extra stuff
			if(arr.length>1){
				var newArr=arr[0].split('.');
				var newType=settings.type;
				switch(newArr[newArr.length-1].toUpperCase()){
					case 'IMG':
					case 'JPG':
					case 'JPEG':
					case 'PNG':
					case 'GIF':
						newType='image';
						break;
					case 'PHP':
						newType='image';
						break;
					default:
						newType=settings.type;
				}
				$.nyroModalSettings({
					type: newType//,
				});
			}
		}
	};
	
	/* youtube links */
	$("a.youtube_links").nyroModal();

	/* google map popup */
	$.nyroModalSettings({beforeHideContent: function(modal, settings, callback) {
        $('iframe', modal.content).css({display: "block"});
        callback();
	}})
    $("a.map_enlarged").nyroModal({width: 640, height: 490});
	
	/* show the search bar link */
	$("#sidebar").css("margin-top", "0px");
	$("#sidebar").prepend('<div class="search_again"><span><a href="#" class="show_search_section">Search Again?</a></span></div>');
	
	/* show search bar 	*/
	$("a.show_search_section").click(function(evt) {
		$(this).parents(".search_again").hide(100);
		$("#sidebar").css("margin-top", "30px");
		evt.preventDefault();
		
		// show/hide the top banner 
		$("#header .banner-728").hide(100);

		// top search box html
		var search_box_html = '<div id="header_search_box">';
		search_box_html += $("#overview_top_search").html();
		search_box_html += '</div>';
		
		// show the top search bar
		var hsb_top = 0;
		if ($('#header-logo-link a').length > 0) {
			var header_offset = $('#header-logo-link a').offset();
			hsb_top = header_offset.top;
		}
		else if ($('.logo-title a').length > 0) {
			var header_offset = $('.logo-title a').offset();
			hsb_top = header_offset.top;			
		}
		else {
			hsb_top = $("#header .banner-728").css("margin-top");
		}
		$("#header .banner-728").after(search_box_html);
		$("#header #header_search_box").css("float", "right");
		$("#header #header_search_box").css("margin-top", hsb_top);
		$("#header #header_search_box").css("margin-right", $("#header .banner-728").css("margin-right"));

		$(":input[class*=predictive_category]").omgsuggestion({
			url: '/static/predictive/category.php',
			dataType: 'json',
			text: 'Type to search for category'
		});
		
		$(":input[class*=predictive_location]").omgsuggestion({
			url: '/static/predictive/search.php',
			dataType: 'json',
			text: 'Type to search for location'
		});
		
	});

	/* anchor hooks */
	if ((document.location.hash != "") && (document.location.hash.length > 1)) {
		var anchorStr = document.location.hash.substring(1, document.location.hash.length);
		switch (anchorStr) {
			case 'gallery':
			case '_tab_2': 
				// top of gallery
				if ($("ul#profile_tabs li#tab_2").length > 0) {
					$("#sections").hide();
					$("ul#profile_tabs li#tab_2").trigger('click');
				}
				break;
			default:
				if (anchorStr.charAt(0) == '_') {
					anchorStr = anchorStr.substring(1, anchorStr.length);
				}
				if ($("ul#profile_tabs li#" + anchorStr).length > 0) {
					$("ul#profile_tabs li#" + anchorStr).trigger('click');
				}
				break;
		}
	}
	
	/* show additional locations */
	$('.show_locations').click(function(evt) {
		evt.preventDefault();
		$(this).hide();
		$('.additional_locations').show("fast");
	});
});
