
var startfav=0;
var favlen=7;
var onMouseOutOpacity = .6;
		  
		   
$(document).ready(function(){
	
	// check if screen width > 1200 we make favoritedContainer larger
	if (screen.width > 1200) {
		$('#favoritedContainer').css('width','1000px');
		$('#favoritedContent').children().remove();
		for(i=0;i<9;i++) {
			$('#favoritedContent').append('<div class="favoritedThumb emptyslot" id="thumb_'+i+'"></div>');
		}
		$('#favoritedContent').append('<div class="clear"></div>');
		favlen=9;
	}
	/*
	$('#escortListResults').find('img').hover(
				  function () {
					$(this).css('border-color','#ffc600');
				  }, 
				  function () {
					$(this).css('border-color','#fff');
				  }
				); */
	$('.paginateButton').css('cursor','pointer').click(function(){
		var sStart=0;
		if ($(this).html()=='prev') {
			if ( parseInt( $('#start').val() ) > 0 )
				sStart = parseInt( $('#start').val() )-1;
			else
				return false ;
		} else if ($(this).html()=='next') {
			var tpage = $('#px98756').val( ).split('/');
			if ( parseInt ( $('#start').val() ) < parseInt( tpage[0] ) -1 )
				sStart = parseInt( $('#start').val() ) +1;
			else
				return false ;
		}
		processdata(sStart)	;
	});
	/*
	$('#gotopagesearch').change(function() {
		var sStart = parseInt($(this).val())-1;
		processdata(sStart)	
	}); */
	$('.paginateNumber').click(function() {
		var sStart = parseInt($(this).html())-1;
		processdata(sStart)
	});
	
	
	$('#header').css('cursor','pointer').click(function() {
		top.document.location.href = '/';
	});
	
	var wheight = $(window).height();
	var w = $(window).width();
	//$('#srcresultbox,#pagebox').css('height',(wheight-$('#favorited').height())+'px');
	$('#srcresultbox').css('height',(wheight)+'px');
	var diffpad = 0;
	if ($.browser.msie && $.browser.msie < 6.0) {
		diffpad=2;
	}
	$('#pagebox').css('width',( w-$('#srcresultbox').width()-diffpad )+'px');
	pag_top_H = $('#paginatetop').height();
	pag_btm_H = $('#paginatebottom').height();
	$('#escortListResults').css('height',wheight-(pag_top_H+pag_btm_H+12)+'px');
	//fav_w = $('#favorited').css('width');
	$('#favorited').css('width',(w-$('#srcresultbox').width()-2)+'px');
	
	$(window).resize(function(){
		var wheight = $(window).height();
		var w = $(window).width();
		//$('#srcresultbox,#pagebox').css('height',(wheight-$('#favorited').height())+'px');
		$('#srcresultbox').css('height',(wheight)+'px');
		pag_top_H = $('#paginatetop').height();
		pag_btm_H = $('#paginatebottom').height();
		$('#escortListResults').css('height',wheight-(pag_top_H+pag_btm_H+12)+'px');
		$('#favoritedContent').css('width','auto');
		$('#favorited').css('width',(w-$('#srcresultbox').width()-2)+'px');
	});
	//favorited
	$('.savefavorite').click(function() {
		var adstgt = $('#adsID').val();
		adata = { 
			action : 'setFavorite',
			adsID : $('#adsID').val(),
			adsName : $('#adname_contactinfo').html().replace('VIP ','')
		};
		var ele = $('#favoritedContent .emptyslot:first');
		$(ele).html('processing').addClass('processing');
		$.ajax({ url: "/html/ajax.handler.php",type:'POST', data: adata , success: function(msg){
					
						$(ele).html('').removeClass('processing');
						if( msg != '0') {
							
							$(ele).css('background','url(../include/image.php?'+$('#adsID').val()+') no-repeat center center');
							$(ele).removeClass('emptyslot').addClass('fillslot').attr('id',adata.adsID);
							$(ele).click(function(){
								$("#escortlistbox").load("/html/ajax.handler.php", { 'action':'getSearchDetails','item': $(ele).attr('id') } , function(msg){
									$('#pagebox').scrollTop(0);
									$('.fav_adname').html( $('#adname_contactinfo').html() )
								});
							});
							$(ele).append('<img style="padding-top:34px;padding-right:3px;visibility:hidden" src="/images/circle_x_red.gif" alt=""/>');
							$(ele).hover(
								  function () {
									$(this).css('border-color','#ff0000');
									$(this).find('img').css('visibility','visible')
								  }, 
								  function () {
									$(this).css('border-color','#171717');
									$(this).find('img').css('visibility','hidden');
								  }
								); 
							$(ele).find('img').css('cursor','pointer').css('cursor','hand').bind('click',function() {
								adata = { 
									action : 'removeFavorite',
									adsID : $(this).parent().attr('id')
								};
								$.ajax({ url: "/html/ajax.handler.php",type:'POST', data: adata , success: function(msg){
													resetFavorite();
											}
								});
								return false;
							});
							
							if (startfav<=0) {
								$('#prevFav').removeClass('enabled')
								$('#prevFav').removeClass('useopacity');
							} else {
								$('#prevFav').addClass('enabled');
								$('#prevFav').addClass('useopacity');
							}
							if (parseInt(msg)<(favlen+startfav)){
								$('#nextFav').removeClass('enabled')
								$('#nextFav').removeClass('useopacity');
							} else {
								$('#nextFav').addClass('enabled');
								$('#nextFav').addClass('useopacity');
							}
							
							$('.useopacity').opacityrollover({
								mouseOutOpacity:   onMouseOutOpacity,
								mouseOverOpacity:  1,
								fadeSpeed:         'fast',
								exemptionSelector: '.selected'
							  });
							
						} else {
							// display the message the the ad is already become user's favorites.
							if ( $('#favoritedMainContainer').hasClass('expanded')) {
								//$('#fav_message').html( $('.fav_adname').eq(0).html() +' already in / favorites' ); 
								
								adname = $('.fav_adname').eq(0).html();
								/*
								chtml = $('#fav_name_cont').html();
								$('#fav_name_cont').html( adname +' already in / favorites' ); 
								$('#fav_name_cont').css('visibility','visible');
								setTimeout("$('#fav_name_cont').html('"+chtml+"').css('visibility','hidden')",2000);
								*/
								$('#fav_name_cont').find('a').css('display','none');
								$('#fav_name_cont').append('<span id="tempspan">'+adname +' already in / favorites</span>')
								$('#fav_name_cont').css('visibility','visible');
								setTimeout("$('#fav_name_cont').css('visibility','hidden');$('#tempspan').remove();$('#fav_name_cont').find('a').css('display','inline');",2000);
							} else {
								$('#fav_message').html( $('.fav_adname').eq(0).html() +' already in / favorites' ); 
								setTimeout("$('#fav_message').html('')",2000);
							}
						}
					}
		});
		
		return false;
	});
	$('#nextFav').bind('click',function() {
		if ($(this).hasClass('enabled')) {
			startfav  = startfav+favlen;
			resetFavorite(startfav , favlen) ;
			//displayFavorite(startfav , favlen);
		}
		
	});
	$('#prevFav').bind('click',function() {
		if ($(this).hasClass('enabled')) {
			startfav  = startfav-favlen;
			resetFavorite(startfav , favlen) ;
			//displayFavorite(startfav , favlen);
		}
	});
	
	$('.favoritedTopright').css({'cursor':'hand','cursor':'pointer'}).bind('click',function() {
		if ( $('#favoritedMainContainer').hasClass('expanded') ) {
			$('#favoritedMainContainer').removeClass('expanded').addClass('colapsed');
			$('#favorited').css({'height':'13px'});
			$(this).html(' + Show');
			$('#fav_name_cont').css('visibility','visible');
		} else {
			$('#favoritedMainContainer').removeClass('colapsed').addClass('expanded');
			$('#favorited').css({'height':'75px'});
			$(this).html(' - Hide');
			$('#fav_name_cont').css('visibility','hidden');
		}		
		/*
		var wheight = $(window).height();
		$('#srcresultbox,#pagebox').css('height',(wheight-$('#favorited').height())+'px');
		pag_top_H = $('#paginatetop').height();
		pag_btm_H = $('#paginatebottom').height();
		$('#escortListResults').css('height',wheight-(pag_top_H+pag_btm_H+12)+'px');
		$('#favoritedContent').css('width','auto');  */
	});
	//display set favorite name
	$('.fav_adname').html( $('#adname_contactinfo').html() )
	
	$('.searchEscort').each(function() {
		$(this).hover(
				  function () {
					$(this).find('a').css({'text-decoration':'underline' });
					$(this).find('img').css({'border-color':'#ffc821' });
				  }, 
				  function () {
					$(this).find('a').css({'text-decoration':'none' });
					$(this).find('img').css({'border-color':'#fff' });
				  }
				); 
	}); 
	displayFavorite(startfav,favlen);
	makelink();
	
	
});
function resetFavorite(start,length) {
	$('#favoritedContent').find('img').unbind();
	$('#favoritedContent').find('div').unbind();
	$('#favoritedContent .favoritedThumb').each(function() {
		$(this).removeClass('fillslot').addClass('emptyslot').css('background','');
		$(this).hover(
											  function () {
												$(this).css('border-color','#171717');
											  }, 
											  function () {
												$(this).css('border-color','#171717');
											  }
											);
		$(this).find('div').remove(); 
		$(this).find('img').remove(); 
		
	});
	start = (start!==undefined) ? start : startfav;
	length = (length!==undefined) ? length : favlen;
	setTimeout('displayFavorite('+start+','+length+')',100);						
}
function displayFavorite(start,length) {
	
	start = (start!==undefined) ? start : startfav;
	length = (length!==undefined) ? length : favlen;
	$.getJSON("/html/ajax.handler.php", {action:'getFavorites',initialStart:start,Clength:length,randNum:Math.floor(Math.random()*11)},function(json){
			if (json.data.length==0 && start!=0) {
				startfav = startfav-(favlen);
				displayFavorite(startfav,length)
			}
			
			$.each(json.data, function(i,item){
				//if(item.adsID!=''){
				ele = $('#favoritedContent .emptyslot:first');
				$(ele).css('background','url(../include/image.php?'+item.adsID+') no-repeat center center');
				$(ele).removeClass('emptyslot').addClass('fillslot').attr('id',item.adsID);
				if ( item.available == '1' ) {
					$(ele).click(function(){
						$("#escortlistbox").load("/html/ajax.handler.php", { 'action':'getSearchDetails','item': item.adsID } , function(msg){
							//$('#pagebox').scrollTop(0);
							$(document).scrollTop(0);
							$('.fav_adname').html( $('#adname_contactinfo').html() )
						});
					});
					$(ele).append('<img style="margin-top:35px;padding-right:3px;visibility:hidden" src="/images/circle_x_red.gif"  alt=""/>');
				} else {
					// if user has paused or removed the ads
					$(ele).append('<div style="padding-top:14px;padding-right:3px;font-size:10px;color:#ff0000"> This Ad has removed <br /><img class="show" src="/images/circle_x_red.gif"  alt=""/></div>');
				}
				//}
			});
			
			if (start<=0) {
				$('#prevFav').removeClass('enabled')
				$('#prevFav').removeClass('useopacity');
			} else {
				$('#prevFav').addClass('enabled');
				$('#prevFav').addClass('useopacity');
			}
			if ((parseInt(json.totalrecords) -1 )<(favlen+start)){
				$('#nextFav').removeClass('enabled')
				$('#nextFav').removeClass('useopacity');
			} else {
				$('#nextFav').addClass('enabled');
				$('#nextFav').addClass('useopacity');
			}
			
			$('.useopacity').opacityrollover({
				mouseOutOpacity:   onMouseOutOpacity,
				mouseOverOpacity:  1,
				fadeSpeed:         'fast',
				exemptionSelector: '.selected'
			  });
			rebuildFavorite();
	});
}
function resetimg() {
	$('.fillslot').find('img').unbind();
	$('.fillslot').find('img').css('cursor','pointer').bind('click',function() {
						parentID = ($(this).hasClass('show') ) ? $(this).parent().parent().attr('id') : $(this).parent().attr('id');
						adata = { 
							action : 'removeFavorite',
							adsID : parentID
						};
						$.ajax({ url: "/html/ajax.handler.php",type:'POST', data: adata , success: function(msg){
											resetFavorite();
									}
						});
						return false;
					});
}
function rebuildFavorite() {
	$('.fillslot').each(function(){
		var ele = $(this);
		var theID = ele.attr('id');
		$(this).hover(
				  function () {
					$(this).css('border-color','#ff0000');
					img = $(this).find('img');
					if ($(img).hasClass('show')) {
					// do nothing
					} else {
						$(img).css('visibility','visible')
					}
				  }, 
				  function () {
					$(this).css('border-color','#171717');
					img = $(this).find('img');
					if ($(img).hasClass('show')) {
					// do nothing
					} else {
						$(img).css('visibility','hidden')
					}
				  }
				); 
		resetimg()
	});
}
function processdata(sStart) {
	adata = { 
		action : 'gotopage',
		lStart : sStart,
		gender : $('#srcgender').val(),
		region : $('#srcRegion').val(),
		lookfor : $('#srclookfor').val(),
		sLimit : $('#paginatesearch').val()
	};
	$('.escortsSearchL').unbind('click');	
	$.ajax({ url: "/html/ajax.handler.php",type:'POST', data: adata , success: function(msg){
					$('#escortListResults').html(msg);
					$('#start').val(sStart);
					$('.paginateNumber').removeClass('current').each(function() {
						if ( parseInt($(this).html()) == sStart+1) {
							$(this).addClass('current');
						}
					});
					//$('#gotopagesearch').val(sStart+1);
					$('.searchEscort').each(function() {
						$(this).hover(
								  function () {
									$(this).find('a').css({'text-decoration':'underline' });
									$(this).find('img').css({'border-color':'#ffc821' });
								  }, 
								  function () {
									$(this).find('a').css({'text-decoration':'none' });
									$(this).find('img').css({'border-color':'#fff' });
								  }
								); 
					}); 
					makelink();
					$('#escortListResults').scrollTop(0);
				}
	});
}

var escID;
function makelink() {
	$('.escortsSearchL').bind('click',function() {
		adata = $(this).attr('href').split('/details/');
		adata2 = adata[1].split('/');
		var escID = adata2[0];
			
			  $("#escortlistbox").load("/html/ajax.handler.php", { 'action':'getSearchDetails','item': escID } , function(msg){
					//display set favorite name
					$('.fav_adname').html( $('#adname_contactinfo').html() );
					$(document).scrollTop(0);
				});
			
		
		return false;
		
	});
}

function makegal() {
		
			$("#imagesmallList a").slimbox();
			$('#photo1').click(function(){
				$("#imagesmallList").find('a').eq(0).trigger('click');
			});
			$('#photo2').click(function(){
				//$("#imagesmallList a:(eq=2)").trigger('click');
				$("#imagesmallList").find('td').eq(1).find('a').trigger('click');
			});
			$('#photo3').click(function(){
				$("#imagesmallList").find('td').eq(2).find('a').trigger('click');
			});			
}
;(function($) {
	var defaults = {mouseOutOpacity:0.67,mouseOverOpacity:1.0,	fadeSpeed:'fast',exemptionSelector:'.selected'};

	$.fn.opacityrollover = function(settings) {
		$.extend(this, defaults, settings);
		var config = this;
		function fadeTo(element, opacity) {
			var $target = $(element);
			if (config.exemptionSelector) {
				$target = $target.not(config.exemptionSelector);	
			}
			$target.fadeTo(config.fadeSpeed, opacity);
		}

		this.css('opacity', this.mouseOutOpacity)
			.hover(
				function () {
					fadeTo(this, config.mouseOverOpacity);
				},
				function () {
					fadeTo(this, config.mouseOutOpacity);
				});

		return this;
	};
})(jQuery);
