/**
 * @author aprian
 */
jQuery.fn.center = function () {
    return this.each(function () {
        var t = jQuery(this);
		var tWidth = 0;
		
		t.parent().css({
			position: 'relative'
		});
		
		$.each(t.children('li'), function(){
			tWidth = tWidth + $(this).outerWidth();
		});
				
		t.css({
			position: 'absolute',
            zIndex: 100,
			left: '50%',
			marginLeft:    '-' + (tWidth / 2) + 'px'
        });
		
    });
};


/*
 * CLOSE BUTTON
 * Bind close function for element with ID btn_close (#btn_close)
 * ID : #btn_close
*/

function closePopup() {
	$("input#btn_close").each( function() {
		$(this).click(function() {
			self.close();
		});
	});
}

/*
 * POPUP WINDOW
 * Bind open new window function for element with ID openPopup (#openPopup)
 * Class: openPopup pop_[Width]x[Height]
 * To create popup 800x600, use class: popup_800x600
*/

function openPopup() {
	$("a[class^='popup']").each( function() {
		$(this).click(function() {
			
			var winpop;
			var xurl = $(this).attr('href');
			var xtarget = '_blank';
			var thisClass = $(this).attr('class').split(' ');
			var re = new RegExp('^(popup_)');
			for(i=0;i<thisClass.length;i++) {
				if(thisClass[i].match(re)) {
					popSizes = thisClass[i].split('_');
					popSize = popSizes[1].split('x');
					popWidth = popSize[0];
					popHeight = popSize[1];
				}
			}
			
			winpop = window.open(xurl, xtarget,'width=' + popWidth + ',height=' + popHeight + ',resizable=0,scrollbars=yes,menubar=no,status=no' );
			winpop.focus();
			return false;
		});
	});
}

/*
 * Paginate
 * 		HTML
 * 			- Content
 * 				<div class="mainFrame"><div class="contentFrame"><div id="content"></div></div></div>
 * 			- Paginator
 * 				<ul class="paginator"><li><a href="#n"></a></li></ul>
 * 		
 */
(function( $ ){

	$.fn.paginate = function( options ) {  

		var settings = {
			'height': 400
		};
	
		// If options exist, lets merge them with our default settings
		if ( options ) { 
			$.extend( settings, options );
		}
		// Wrap the content
		var elContent = $(this);
		
		if ( $(".mainFrame").length < 1 ) {
			elContent.wrap('<div class="mainFrame"><div class="contentFrame"></div></div>');
		}
			
		var elMainFrame = $(".mainFrame");
		var elContentFrame = $(".contentFrame");
		
					
		// Set contentFrame height
		elContentFrame.height( settings.height ).css({'overflow': 'hidden', 'position': 'relative'});
		
		//If content height > frame height then paginate the content.
		var contentFrameHeight = parseFloat( elContentFrame.outerHeight() );
		var contentHeight = parseFloat( elContent.outerHeight() );
		var numPages = Math.ceil(contentHeight/contentFrameHeight );
		 
		if( numPages > 1){
		    
			// Create paginator
		    var paginator = '<ul class="paginator">';
		    for(i = 1; i < numPages+1; i++){
		        paginator += '<li><a href="#' + i + '">' + '&nbsp;' + '</a></li>'
		    }
		    paginator = paginator + '</ul>';
			
			// Append to the main frame
		    elMainFrame.append( paginator );
			
		    $(".paginator a").live('click', function(e){
				
				e.preventDefault();
				
				var pattern = /#([0-9]+)/;
				var page = $(this).attr('href').match(pattern)[1];
				var target = ( ( parseFloat( page ) - 1) * settings.height ) - 15;
				elContentFrame.animate({scrollTop: target}, 1200, 'easeOutQuart');
				
		    } );
		}
		
		return this;
	};
  
})( jQuery );


// Easing equation, borrowed from jQuery easing plugin (http://gsgd.co.uk/sandbox/jquery/easing/)
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

/*
 * Run Forrest Run ...
 */

$(document).ready( function(){
	//openPopup();
	
	$("#contentWrapper").paginate({
		height: 380
	});
});

