/**
 * @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
		};
	
	    return this.each(function() {        
	      
			// If options exist, lets merge them with our default settings
			if ( options ) { 
				$.extend( settings, options );
			}
			
			// Wrap the content
			var elContent = $(this);
			elContent.wrap('<div class="mainFrame" />').wrap('<div class="contentFrame" />')
						
			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 + '">' + i + '</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');
					
			    } );
			}

	    });

	};
  
	// 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;
	};
	
})( jQuery );


/**
 * Copyright (C) 2009 Jonathan Azoff <jon@azoffdesign.com>
 */
(function($){
	$.extend({"log":function(){ 
		if(arguments.length > 0) {
			var args = (arguments.length > 1) ? Array.prototype.join.call(arguments, " ") : arguments[0];
			try { 
				console.log(args);
				return true;
			} catch(e) {		
				try { 
					opera.postError(args); 
					return true;
				} catch(e) { }
			}
			alert(args);
			return false;
		}
	}});
})(jQuery);



/*
 * Run Forrest Run ...
 */

$(document).ready( function(){
	//openPopup();
	
	$("#contentWrapper").paginate({
		height: 380
	});
});

