// ED Photo Viewer
(function($) {
  $.fn.edPhoto = function(options) {
  	var settings = {
			'ad_free':'yes',
			'min_height':500,
			'min_width':960,
			'max_height':720,
			//'banner_1':'<iframe  allowTransparency="true" style="background-color:#000000" bgcolor="#000000" id="ab8d69ed" name="ab8d69ed" src="http://escortclicks.com/www/delivery/afr.php?zoneid=20&amp;cb={rand}" frameborder="0" scrolling="no" width="600" height="18"><a href="http://escortclicks.com/www/delivery/ck.php?n=aacc46de&amp;cb={rand}" target="_blank"><img src="http://escortclicks.com/www/delivery/avw.php?zoneid=20&amp;cb={rand}&amp;n=aacc46de" border="0" alt="" /></a></iframe>',
			//'banner_2':'<iframe allowTransparency="true" style="background-color:#000000" bgcolor="#000000" id="af53a189" name="af53a189" src="http://escortclicks.com/www/delivery/afr.php?zoneid=33&amp;cb={rand}" frameborder="0" scrolling="no" width="468" height="60"><a href="http://escortclicks.com/www/delivery/ck.php?n=aa2911e9&amp;cb={rand}" target="_blank"><img src="http://escortclicks.com/www/delivery/avw.php?zoneid=33&amp;cb={rand}&amp;n=aa2911e9" border="0" alt="" /></a></iframe>'
			//'banner_1' : "<a href='http://escortclicks.com/www/delivery/ck.php?n=a931fe53&amp;cb={rand}' target='_blank'><img src='http://escortclicks.com/www/delivery/avw.php?zoneid=34&amp;cb={rand}&amp;n=a931fe53' border='0' alt='' /></a>",
			//'banner_2' : "<a href='http://escortclicks.com/www/delivery/ck.php?n=afa5d028&amp;cb={rand}' target='_blank'><img src='http://escortclicks.com/www/delivery/avw.php?zoneid=35&amp;cb={rand}&amp;n=afa5d028' border='0' alt='' /></a>" 
			'banner_1':'<iframe allowTransparency="true" id="a931fe53" name="a931fe53" bgcolor="#FFF" src="http://escortclicks.com/www/delivery/afr.php?zoneid=34&amp;cb={rand}" frameborder="0" scrolling="no" width="600" height="15"></iframe>',
			'banner_2':'<iframe allowTransparency="true" id="afa5d028" name="afa5d028" src="http://escortclicks.com/www/delivery/afr.php?zoneid=35&amp;cb={rand}" frameborder="0" scrolling="no" width="600" height="92"></iframe>'
		};
		
		var next_link = 0;
		var prev_link = 0;
		var popup_open = 0;
	
		if (options) { 
			$.extend(settings, options);
		}

		//bindings	
		$(window).resize(function(e) {
			resize_stage();
		});	
	
		$('#popup').click(function(e) {
			if(e.target === this) { 
				photo_close();
			}
		});	
	
		$('a.p_close').click(function(e) {
			e.preventDefault();
			photo_close();
		});
	
		$('a.prev').click(function(e) {
			e.preventDefault();		
			photo_open(prev_link);
		});
	
		$('a.next').click(function(e) {
			e.preventDefault();
			photo_open(next_link);
		});	
	
		//keys left, right for prev and next navigation
		$(document).keydown(function (e) {
			if (popup_open != 0) {
				var key_code = e.keyCode || e.which;
				e.preventDefault();

				switch (key_code) {
					case 37: //left
						photo_open(prev_link);
					break;
					case 39: //right
						photo_open(next_link);
					break;
					case 27: //escape
						photo_close();
					break;
				}
			}
		});
	
		$('img.p_img').live({
			click: function(e) {
				if (e.target === this) photo_open(next_link);
			},
			mouseover: function(e) {
				if (e.target === this) $('a.next').addClass('hover');
			},
			mouseout: function(e) {
				if (e.target === this) $('a.next').removeClass('hover');
			}
    });
	
		//functions
		function resize_stage() {
			var h = $('div.stage_bckg').height();

			if (h == 0) h = $(window).height() - 200;
			h = h - 150;
			
			if (settings.ad_free == 'yes') h = h + 100;
			
			if (h > settings.max_height) h = settings.max_height;
			
			$('div.stage', $('#popup')).css('height', h + 'px').css('line-height', h + 'px');
			
			if ($(window).width()-40 < settings.min_width) {
				$('#popup').addClass('tiny_screen');
			} else {
				$('#popup').removeClass('tiny_screen');
			}
		} 
		
		function photo_open(obj) {
			if(!$.browser.msie)	$('body').css('overflow-y', 'hidden');

			if(all_items.length > 1) {
				$('.next', $('#popup')).show();
				$('.prev', $('#popup')).show();
			}
			
			//updating banners
			if(settings.ad_free != 'yes') {
				//$(".banner_1", $('#popup')).show(0).html(settings.banner_1.replace("{rand}", Math.round(Math.random()*1000000000)));
				$("#popup div.banner_1").show()
				$("#popup div.banner_1_inner").html(settings.banner_1.replace("{rand}", Math.round(Math.random()*1000000000)));

				$("#popup div.banner_2").show();
				$("#popup div.banner_2_inner").html(settings.banner_2.replace("{rand}", Math.round(Math.random()*1000000000)));
			}

			var $obj = $('a[data-edphoto_id="' + obj + '"]');		
			
			resize_stage();
			$('img.p_img', $('#popup')).remove();
			$('#photo_title').html('Photo ' + (obj+1) + ' of ' + all_items.length);
			$('div.stage', $('#popup')).html('<img src="' + $obj.attr('href') + '" class="p_img" /> ');		
			
			//updating prev and next links
			var curr_link = obj;
			next_link = curr_link + 1;
			if(next_link > (all_items.length-1)) next_link = 0;
			prev_link = curr_link - 1;
			if(prev_link < 0)  prev_link = all_items.length-1;
			
			popup_open = 1;
		}
	
		function photo_close() {
			$('#popup').hide(0);
			$('div.stage', $('#popup')).html('');		
			if(!$.browser.msie) $('body').css('overflow-y', 'auto');
			popup_open = 0;
		}

		var all_items = [];
		var c = 0;

		return this.each(function() {		
			$(this).attr('data-edphoto_id', c);

			all_items[c] = c;
			c++;
			
			$(this).click(function(e){
				e.preventDefault();
				$('#popup').show(0);
				photo_open(parseFloat($(this).attr('data-edphoto_id')));
			});
		});
	};
})(jQuery);

