//AD MODALS		
	
function pbModalClose() {
    $('.adOverlay, #exposeMask').fadeOut('slow');
    $('#tpl_topmost_ad, .gbl_adstruct, .adstruct, .ad').css({'position':'relative','margin-left':'auto'}); //SHOW ADS
};

function pbModalPop(type,timer,target,link,creative,width,height) { //setup for variables coming from ad server script

	var date = new Date();
	date.setTime(date.getTime() + (4 * 60 * 60 * 1000)); //SETTING EXPIRATION TIME FOR COOKIES, in hours*minutes*seconds*milliseconds */
	
	var timer = setTimeout(function(){
	
		var checkOverlay = $("#exposeMask").css("display"); //CHECK TO SEE IF AN OVERLAY IS ALREADY OPEN
		var checkColorbox = $("#cboxOverlay").css("display"); //CHECK TO SEE IF A COLORBOX IS ALREADY OPEN
		var checkCookie = $.cookie("pbModal"); //CHECK TO SEE IF EMAILGATE HAS FIRED ALREADY
		//var checkRegistered = $.cookie("u_profile"); //CHECK TO SEE IF USER IS LOGGED IN
				 
		if ( checkOverlay == 'block' || checkColorbox == 'block' ) {
		    return false;
		} 
		else {
		
			$('.adOverlay').remove();//REMOVES ANY EXISTING AD OVERLAY
			
			if (type.indexOf("emailgate") > -1) { //if the "type" contains "iframe", create and iFrame in the modal
				$('body').append('<div class="adOverlay emailgate" id="' + type + '"><iframe src="' + link + '" name="pbModaliFrame_Email" width="' + width + '" marginwidth="0" height="' + height + '" marginheight="0" align="top" scrolling="no" frameborder="0" hspace="0" vspace="0"></iframe><img src="' + creative + '" class="hidden"/><a class="adOverlayClose" href="#" onClick="pbModalClose();">CLOSE WINDOW</a></div>'); //CREATE HTML AD
			}
			else {
				$('body').append('<div class="adOverlay" id="'+ type +'"><a href="' + link + '" target="' + target + '"><img src="' + creative + '" width="950" /></a></div>'); //CREATE IMAGE AD
			}
			$(".adOverlay").overlay({ // overlay settings
			    left: 'center',
			    expose: {
			        color: '#000', loadSpeed: 0, opacity: 0.8, loadSpeed: 'slow'
			    },
			    absolute: true, oneInstance: true, fixed: true,
			    onBeforeLoad: function(){
			     	$('#tpl_topmost_ad, .gbl_adstruct, .adstruct, .ad').css({'position':'absolute','margin-left':'-100000px'}); //HIDE ADS
					var value = $.cookie("pbModal"); //CHECK VALUE OF pbModal
					if (value == "" || value == null) { //if pbModal does not exist or if it's an iFrame, create it and add type to value
			    		$.cookie("pbModal", type , { path: "/", domain: "playboy.com"}); //creates cookie called pbModal, sets value to type
					} 
					else if (value.indexOf(type) > -1) { //if value already exists in pbModal, do not duplicate
						return true;
					} 
					else {
						$.cookie("pbModal", value + "&" + type , { path: "/", domain: "playboy.com", expires: date}); //if pbModal does exist, add new value with delimiter
					}
			       },
			    onClose: function(){
			        $('#tpl_topmost_ad, .gbl_adstruct, .adstruct, .ad').css({'position':'relative','margin-left':'auto'}); //SHOW ADS
			        $('.adOverlay, #exposeMask').css({"display":"none"});
			    },
			    closeOnClick: true, closeOnEsc: true, api: true
			}).load();	//load overlay
		}
		
	}, timer );	//set the timer, variables coming from ad script

}


var defaultSearchInputText = 'Search';

$(document).ready(function() { 

			var u_profile = $.cookie("u_profile");
			if (!u_profile) {
				$('#tpl_nav_elements .gbl_simplestruct_box2').css({"display":"block"});
			} else {
				$('#tpl_nav_elements .gbl_simplestruct_box3').css({"display":"block"});
			}		
	
			$('#girl_meta_data h3.sidebar_toggle_trigger').each(function(i){
					$(this).attr('id', Math.pow(2,i));
			});	
			
			
			
			//Insert Tooltip div after the wrapper
			$('<div id="gbl_pbr_girlName_tooltip"></div>').insertAfter($('#tpl_wrap_outer'));
			
			//Tooltip
			$("#tpl_pb_recommends img[title]").tooltip({
				tip: '#gbl_pbr_girlName_tooltip',
				effect: 'toggle',
				position: "top center",
				offset: [5, 0]	
			});
	
			//Browse Tag Opener - used on index, browse, search results
			
			$(".open_browse_tags").click(function(e) {          
				e.preventDefault();
		        $("ul#tpl_tag_list").toggle();
				$(".open_browse_tags").toggleClass("menu-open");
		    });
	
			$("ul#tpl_tag_list").mouseup(function() {
				return false
			});
			
			$(document).mouseup(function(e) {
				if($(e.target).parent("a.open_browse_tags").length==0) {
					$(".open_browse_tags").removeClass("menu-open");
					$("ul#tpl_tag_list").hide();
				}
			});	

			//Add a class for the hover on the search button

			$('#tpl_gbl_sb_submit').hover(function() {
				  $(this).addClass('tpl_hover');
				  }, function() {
				  $(this).removeClass('tpl_hover');
			 });


			$("#SearchString").val(defaultSearchInputText); 
			//Search String Focus
			$("#SearchString").focus(function() {
			$("#SearchString").val('');
				if (this.value == this.defaultValue){  
					 this.value = '';  
					}  
				if(this.value != this.defaultValue){  
					this.select();  
					}  
				$("#search_box").addClass("curFocus");
			});

			//Search string Blur
			$("#SearchString").blur(function() {
				 if ($.trim(this.value) == ''){  
					        this.value = (this.defaultValue ? this.defaultValue : '');  
					        $(this).val(defaultSearchInputText);
				 			}    
				 $("#search_box").removeClass("curFocus");
			});

		
			$('#tpl_gbl_sb_submit').click(	
				function(){ 
					$('#SearchForm').submit(); 
				} 
			);

			$('#SearchForm').submit(
				function(event){
					var isValid = $('#SearchString').validate();
					if ( isValid ){			
						$('#SearchString').val( stripSpecialChars( $('#SearchString').val() ) );
					}
					
					return isValid;
				}
			);	

			$('#SearchString').one('click', function(){$('#SearchString').val('');});		

			$('#SearchString').autocomplete('/services/autocomplete', 
				{	
					minChars:3,
					width:255, 
					cacheLength:0,    																										  
					resultsClass:'gbl_head_autocomplete_results',
					//loadingClass:'gbl_head_autocomplete_loading',
					resultsMouseOverClass:'gbl_head_autocomplete_results_mouseover',
					//lineSeparator:'|',
					onItemSelect:function(item){
						window.location = $(item).attr( "url" );
			 		},
					extraParams:{ 
						'jsonp':'true',
						'profiles':'girlsdir_search' 
						//'profiles':'freesite_search'
					},
					formatItem:function(row, i, num){
						var html = '<span class="gbl_autocomplete_thumb"><img src="' + row.thumbnailURL + '"/></span>';
						html = html + '<span class="gbl_autocomplete_textname">' + row.term + '</span>';
						return html;
					}			
					} 
				);


function stripSpecialChars( value ){
	if ( value == null || value == undefined ){
		return '';
		}
	return $.trim( value.replace(/[^a-zA-Z0-9]+/g, " ") );
	}

			
	//$(".sidebar_toggle_simplestruct").hide();
	//$("h3.sidebar_toggle_trigger").addClass("active");

	$("h3.sidebar_toggle_trigger").click(function(){	
		var triggerId = parseInt($(this).attr('id'));		
		var triggerContent = $(this).next();
		
		//TODO:  get the value from the cookie
		//hard-coding to 15 for now indicating that all are expanded
		//(this should probably the default, anyway, if the cookie doesn't exist)
		var aggregatedPreferencesOpenBitCookieValue = $.cookie('aggregatedPreferencesOpenBit');
		if ( !aggregatedPreferencesOpenBitCookieValue ){
			aggregatedPreferencesOpenBitCookieValue = 15;
		}
		
		var aggregatedPreferencesOpenBit = new BitWriter( aggregatedPreferencesOpenBitCookieValue );  
		if ( aggregatedPreferencesOpenBit.hasBitmask(triggerId) > 0 ){ //trigger content is already open... 
			//hide it
			triggerContent.hide();
			
			//remove it from the bit
			aggregatedPreferencesOpenBit.removeBitmask(triggerId);
		}
		else{ //trigger content is not open...
			//show it
			triggerContent.show();

			//add it to the bit
			aggregatedPreferencesOpenBit.addBitmask(triggerId);			
		}

		//store the bit value back into the cookie to be used later 
		$.cookie( 'aggregatedPreferencesOpenBit', aggregatedPreferencesOpenBit.getValue() );
		
		return false;
	});
	

	
/*
	$('#toggler0').click(function(){
		
	
	})
*/
	
	//$(element).attr('id', 'newID');

	// this code uses "toggle_sidebar_toggle_trigger" instead of just "sidebar_toggle_trigger"
	// functions to allow open all sidebar_toggle_simplestructs
	// and activate all toggle_sidebar_toggle_triggers
	
	function toggle_all_on() {
	$(".sidebar_toggle_simplestruct").show();
	$(".toggle_sidebar_toggle_trigger").not('.closed').addClass("closed").toggle(function(){
	$(this).addClass("closed");
	}, function () {
	$(this).removeClass("closed");
	});
	}
	
	function toggle_all_off() {
	$(".sidebar_toggle_simplestruct").hide();
	$(".toggle_sidebar_toggle_trigger.closed").removeClass("closed").toggle(function(){
	$(this).addClass("closed");
	}, function () {
	$(this).removeClass("closed");
	});
	}

	$('#allGirlsButton a').click(function(){
		showAllGirls(); 
		return false;
	});

 	function showAllGirls(){
 		$('#tpl_browse_girls_listing').show();
		$('#allGirlsButton').hide();
		$.cookie('see_more', 'true');
 	}
 
 	($.cookie('see_more')) ? showAllGirls() : null; 	
			




}); // end ready


