(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);
this.imagePreview = function(){ 
	/* CONFIG */
	hh = ($(window).height() / 2);
	ww = $(window).width() / 2 ;
	xOffset = -20;
	yOffset = 30;
	dette = false;
	/* END CONFIG */
	
	var mobile = (/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent.toLowerCase()));
	
	$(window).resize(function() {
		hh = ($(window).height() / 2);
		ww = $(window).width() / 2 ;
	}); 	

	function swapp(streng){
		if(streng.indexOf("/tmb/l_pimage1.jpg")>0){
			var xx=streng.substring(0,streng.indexOf("/tmb/l_pimage1.jpg"))+"/pimage1.jpg";
		}
		else{xx=streng;};
		return xx;
	}	

	$("html").click(function(e){
		if ($("#preview").length > 0) { $("#preview").remove(); }	
	});	
	
	$("ul.thumb").find("li").hoverIntent(function(e){
	    if (mobile) return (true);
		if ($("#preview").length > 0) { $("#preview").remove(); };
		var tiss=$(this);
		var tass=tiss.find('img');	
		var nyimg = swapp(tass.attr('src'));
		this.t = tass.attr('title');
		var perfid=(this.t).split(" ");
		var klikke=tiss.find("a").attr('href');
		//var klikke="/freechat.php?performerid="+perfid[0];
		var c = (this.t != "") ? "<br/>" + this.t : "";
//		$("body").append("<p id='preview'>"+"<img src='"+ nyimg +"' height='305' width='405'/>"+ c +"</p>");     
		$("body").append("<p id='preview'></p>");                                                                                           		                                                                                      		

		xOffset = ( (e.pageY  - $(window).scrollTop() ) > hh ) ? 325 : -20;
		yOffset = ( e.pageX + $(window).scrollLeft() > ww ) ? -455 : 30;                     
		
		//$("#preview").append("<img src='"+ nyimg +"' height='305' width='405'/>"+ c);
		$("#preview").append("<a rel='nofollow' href='" + klikke + "'><img src='"+ nyimg +"' height='305' width='405' /></a>"+ c);		
		
		dette = true;
		
		$("#preview")
		.fadeIn(0)
		.css("top",(e.pageY - xOffset) + "px")
		.css("left",(e.pageX + yOffset) + "px")
		;                                                             
	},

	function(){
		$("#preview").stop().remove();
		dette = false;

	});     
	
	$("ul.thumb li a").mousedown(function(e) {
	    url = "/";
		path = "freechat.php";
		xx = this.href;
		xx = xx.replace("&c=1","");
		zz = xx.indexOf("#");
		$("#preview").remove();
		if (zz > 0) {
			yy = url + path + "?performerid=" + xx.substr(zz + 1);
			this.href=yy;
			}
		else {
			this.href=xx;
			}
		return(true);
	});

	$("ul.thumb").find("li").mousemove(function(e){
	    if (mobile) return (true);
		// 1 gang for hvert nytt bilde:
		if (dette == false) {	
			xOffset = ( (e.pageY - $(window).scrollTop() ) > hh ) ? 355 : -20;
			yOffset = ( e.pageX + $(window).scrollLeft() > ww ) ? -455 : 30;                     
			;}
		$("#preview")
		.css("top",(e.pageY - xOffset) + "px")
		.css("left",(e.pageX + yOffset) + "px");
	});   

    // $("ul.thumb li a").live('touchend', function(e) {		
		// var el = $(this);
		// $("#preview").stop().remove();		
		// var link = el.attr('href').replace("&c=1","");
		// window.location.href = '';
		// return false;
	// });   	
	
//	$("#preview a").live('touchend', function(e) {
//		var el = $(this);
//		var link = el.attr('href').replace("&c=1","");
//		$("#preview").stop().remove();		
//		window.location = link;
//	});	
	
};

var curblokk = 0;

this.addmore = function(){ 
	removebutton();
	if(curblokk <= blokk.length - 1) {
		$("ul.thumb").append(blokk[curblokk]);
		imagePreview();
		curblokk = curblokk + 1;
		if(curblokk <= blokk.length - 1) {
			addbutton();
			};
		};

};

this.breakout_of_frame = function(){ 
	if (top.location != location) { 
		top.location.href = document.location.href ; 
	}
};

this.addbutton = function(){ 
	if (!(typeof(blokk) == 'undefined')) {
		var knapp = '<div class="button"><center><input id="smb" onclick="addmore();" style="margin-bottom: 20px; height: 30px; width:320px !important;background-repeat:repeat-x;padding:0" type="button" value="  Show more thumbnails  "></center></div>';	
		$(".thumb").after(knapp);
	}
};

this.removebutton = function () {
	$(".button").remove();
};

$(document).ready(function(){
//    breakout_of_frame();
	imagePreview();
});

$(window).load(function(){
	addbutton();
});
