var ptbox = {
	
    slideshowOn     : 0,        /* if set on 1 automaticly show next content */
    delay           : 3000,     /* slideshow delay */
    
    slideshowStatus : 1,        /* slideshow stopped */
    contentIndex    : 0,
    
    offset          : { 'x' : 50, 'y' : 75 },
	root            : { 'w' : $(window).width(), 'h' : $(window).height() },
	
	slideshow : function(group, remeberPosition) {
	    if (typeof(remeberPosition)=='undefined') remeberPosition = 0;
	    if (!remeberPosition) ptbox.contentIndex = 0;
	    
	    ptbox.slideshowOn = 1;
	    ptbox.group = $('.ptbox[rel="'+group+'"]');
	    ptbox.show(ptbox.group[0].url, ptbox.group[0].title,'');
	},
	
	runSlideshow : function() {
	    ptbox.slideshowOn = 1;
	    ptbox.next();
	},
	
	init : function(element) {
	    ptbox.root = { 'w' : $(window).width(), 'h' : $(window).height() }
	    
	    url    = $(element).attr('href');
	    title  = $(element).attr('title');
	    group  = $(element).attr('rel');
	    type   = '';
	    
	    ptbox.slideshowOn = 0;
	    ptbox.contentIndex = 0;
	    ptbox.group = $('.ptbox[rel="'+group+'"]');
	    $(ptbox.group).each(function(i, elm){         
	        if ($(elm).attr('href') == url) ptbox.contentIndex = i;
	    });
	    if (ptbox.slideshowOn == 1) ptbox.contentIndex = 0;
	    
	    ptbox.show(url, title, type);
	    
	},
	
	insertImage : function(url) {
	    img = new Image();
		img.onload = function() { 
		    img.onload = null;
			img.w = img.width;
			img.h = img.height;

			var x = ptbox.root.w - ptbox.offset.x;
			var y = ptbox.root.h - ptbox.offset.y;
			
			if (img.w > x) {        img.w = x; img.h = img.h * (x / img.w);
				if (img.h > y) {    img.w = img.w * (y / img.h); img.h = y; }
			} else if (img.h > y) { img.w = img.w * (y / img.h); img.h = y;
				if (img.w > x) {    img.w = x;	img.h = img.h * (x / img.w); }
			}

			$('#ptb_main')
				.css('width', img.w + 'px')
				.html('<img src="'+url+'" alt="x" width="'+img.w+'" height="'+img.h+'" />')
				.fadeIn('slow')
				.click(function() { 
				    ptbox.slideshowStatus ? ptbox.stop() : ptbox.next();
				    ptbox.slideshowStatus = ptbox.slideshowStatus ? 0 : 1;
				    
				    return false; 
				});
			
        	var pos = $("#ptb_main").position();
        	
        	if ($.browser.msie && $.browser.version < 8 && $.browser.version > 6) { /* fix IE7 */
        	    $("#ptb_navigation").css({ marginLeft: -pos.left })
        	}
        	
			if (ptbox.slideshowOn == 1) { ptbox.intUpdate = setTimeout('ptbox.next();', ptbox.delay); }
		};
		img.src = url;
		
		return false;
	},

	insertContent : function(url, ajax) {

	    if (typeof(ajax)=='undefined') ajax = 0;
	    
	    var w = ptbox.root.w - ptbox.offset.x;
	    var h = ptbox.root.h - ptbox.offset.y;
	    
	    $('#ptb_main')
    		.css({ width: w + 'px', height: h + 'px' })
    		.show()
    		.addClass('ajaxContent')
    		.click(function() { /*return false;*/ });
    		
	    if (ajax) {
	        $.ajax({
				url: url,
				data: 'ajax',
				cache: false,
				success: function(html){ $('#ptb_main').append(html); }
            });
	    } else {
	        $('#ptb_main').append('<iframe src="'+url+'" frameborder="0" width="'+w+'" height="'+h+'"> </iframe>');
	    }
	    
	    if ($(ptbox.group).length <= 1)
		$('#ptb_root').prepend('<div id="ptb_close" style="padding-left:25px;color:#FFF;line-height:21px; width:50px;background-color:#F36D45">CLOSE</div>');
	    	$('#ptb_close').click(function() { ptbox.close(); });
    			
    	var pos = $("#ptb_main").position();
    	
    	if ($.browser.msie && $.browser.version < 8 && $.browser.version > 6) { /* fix IE7 */
    	    $("#ptb_navigation").css({ marginLeft: -pos.left })
    	}
    	
		if (ptbox.slideshowOn == 1) { ptbox.intUpdate = setTimeout('ptbox.next();', ptbox.delay); }
		
		return false;
	},
	
	navigation : function(url, title, picklist) {
        
        if (typeof(picklist)=='undefined') picklist = '';
        
       	return '<div id="ptb_nav"><div id="ptb_close"></div><div id="ptb_next"></div><div id="ptb_cnt">'+(ptbox.contentIndex+1)+'/'+$(ptbox.group).length+'</div><div id="ptb_prev"></div><div id="ptb_slideshow"'+(ptbox.slideshowOn ? ' class="on"':'')+'></div><div id="ptb_picklist">'+ title +''+ picklist +'</div></div>';
	},
	
	show : function() {
	    
	    el = $(ptbox.group[ptbox.contentIndex]);
	    
	    url    = $(el).attr('href');
	    title  = $(el).attr('title');
	    
	    $('body').append('<div id="ptb_root"></div>');
	    
	    if ($(ptbox.group).length >= 2)
	    	$('#ptb_root').append('<div id="ptb_navigation">'+ ptbox.navigation(url, title) +'</div>');
	    
	    
	    $('#ptb_root')
    	    .css({ width: ptbox.root.w + 'px', height: ptbox.root.h + 'px' })
    	    .append('<div id="ptb_load"></div><div id="ptb_main"></div>')
    	    .click(function() {  });
        $('#ptb_main').hide();
        $('#ptb_prev').click(function() { ptbox.slideshowOn = 0; ptbox.prev(); });
        $('#ptb_next').click(function() { ptbox.slideshowOn = 0; ptbox.next(); });
        $('#ptb_close').click(function() { ptbox.close(); });
        $('#ptb_slideshow').click(function() { ptbox.runSlideshow(); });
    	$('#ptb_load').css({ position: 'absolute', top: '50%', left: '50%' });
    	$('#ptb_navigation').css({ width: (ptbox.root.w + 'px'), zIndex: 999 });
    	
    	$("#ptb_picklist").click(function() { return false });
    	
    	var baseURL;
    	if (url.indexOf("?")!==-1){ //ff there is a query string involved
    	    baseURL = url.substr(0, url.indexOf("?"));
    	} else {
    	    baseURL = url;
    	}

    	var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
    	var urlType = baseURL.toLowerCase().match(urlString);
    	
    	if (urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp') {
    	    ptbox.insertImage(url);
    	} else if ($(el).hasClass('iframe')) {
    	    ptbox.insertContent(url);
    	} else {
    	    ptbox.insertContent(url, 1);
    	}
		$('#ptb_load').hide();
		
		return false;
	},
	
	reloadContent : function() {
	    ptbox.root = { 'w' : $(window).width(), 'h' : $(window).height() }
	    ptbox.close(); 
	    ptbox.show();
	},
		
	stop : function() {
	    clearTimeout(ptbox.intUpdate);
	    return false;
	},

	prev : function() {
	    if (ptbox.contentIndex <= 0)  ptbox.contentIndex = $(ptbox.group).length - 1;
	    else ptbox.contentIndex --;

	    ptbox.reloadContent();
	    return false;
	},

	next : function() {
	    if (ptbox.contentIndex+1 >= $(ptbox.group).length)  ptbox.contentIndex = 0;
	    else ptbox.contentIndex ++;

	    ptbox.reloadContent();
	    return false;
	},

	close : function() {
	    clearTimeout(ptbox.intUpdate);
	    $('#ptb_root').remove();
	    
	    return false;
	}
}

$(document).ready(function() {
	$('.ptbox').click(function() {
	    ptbox.init($(this));
		return false;
	});
	
	/* outside */
	$('#ptbox_slideshow').click(function() {
	    ptbox.slideshow('pixs');
	});
	$('#ptbox_slideshowbypass').click(function() {
	    ptbox.slideshow('slideshow');
	});
	
	$(window).resize(function(){
		var popup 	= { 'w' : $('#ptb_main').width(), 	'h' : $('#ptb_main').height() }
        var h = $(window).height();
        var w = $(window).width();
        //alert(w+"-"+popup.w+"-"+Math.round((w - popup.w) / 2));
        var l = Math.round((w - popup.w) / 2);
        $('#ptb_root').css({'width': w, 'height': h});
        $('#ptb_navigation').css({'right': 0});
        $('#ptb_title').css({'right': 0});
        $('#ptb_picklist').css({'width': '100%' });
        /*$('#ptb_main').css({'left': l});*/
    });
	
}).keyup(function(event){ if (event.keyCode == 27) { ptbox.close(); }});