jQuery.fn.print = function(){
	if (this.size() > 1){
		this.eq( 0 ).print();
		return;
	} else if (!this.size()){
		return;
	}
	var strFrameName = ("printer-" + (new Date()).getTime());
	var jFrame = $( "<iframe name='" + strFrameName + "'>" );
	jFrame
		.css( "width", "1px" )
		.css( "height", "1px" )
		.css( "position", "absolute" )
		.css( "left", "-9999px" )
		.appendTo( $( "body:first" ) )
	;
	var objFrame = window.frames[ strFrameName ];
	var objDoc = objFrame.document;
	objDoc.open();
	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	objDoc.write( "<html>" );
	objDoc.write( "<body>" );
	objDoc.write( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( "</title>" );
	objDoc.write( "</head><style type='text/css'>body{font:12px Helvetica,Arial;width:720px;margin:0 auto;}.box-title{font-size:16px;font-weight:bold;}.box-content{margin-bottom:20px}.box-footer{width:100%;border-top:1px dotted #CCCCCC;}.box-footer a{display:block;font-align:center;text-decoration:none;font-color:#CCCCCC;margin:5px auto;width:100px;}</style>" );
	objDoc.write( this.html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();
	objFrame.focus();
	objFrame.print();
	setTimeout(function(){jFrame.remove();},(60*1000));
}
function aofiPrintBox(btn){
	$('.aofi-share-box').html('<div class="aofi-control"><a class="aofi-print-button" onclick="$(\'.print-box\').print();">'+btn+'</a></div><div class="print-box"><div class="box-title">'+$('.sadrzaj h2').html()+'</div><div class="box-content">'+$('.post-content').html()+'</div><div class="box-footer"><a href="http://www.aofi.rs">www.aofi.rs</a></div></div>').addClass('box-loaded').fadeIn(150).center();
}
function aofiShareBoxMessage(html){
	$('.aofi-share-box').html(html).removeClass('box-loaded').center();
}
function aofiShareBoxError(id,val){
	$('#'+id).addClass('incomplete').val(val);
}
function aofiShareBoxErrorReset(names){
	for(var i in names){
		$('#'+names[i]).removeClass('incomplete');
	}
}
function aofiResetContactForm(){
	$('.contact-form').html('<div class="send-complete">Ваша порука је прослеђена.</div>');
}
function aofiShareBox(id,message){
	$('.aofi-share-box').removeClass('box-loaded').html(message).fadeIn(150).center();
	$.ajax({
		url : getBaseURL()+'~axiomgrp/temp/aofi-share-box='+id,
		success : function(html) {
			$('.aofi-share-box').html(html).addClass('box-loaded').center();
		}
	});
}
function getBaseURL(){
    var url = location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));
    if (baseURL.indexOf('http://localhost') != -1) {
        var url = location.href;
        var pathname = location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);   
        return baseLocalUrl + "/";
    }else return baseURL + "/";
}
jQuery.fn.center = function () {
    this.css('position','absolute');
    if($(window).height() > this.outerHeight()){
    	this.css('top', (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + 'px');
    }else{
    	this.css('top', '40px');
    }
    this.css('left', (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + 'px');
    return this;
}
$(document).ready(function(){
    
    $(document).click(function(){
        $('.aofi-share-box').fadeOut(200).html();
    });
    
    $(document).keydown(function(e){
        if (e.keyCode == 27) {
            $('input#friend_name').blur();
            $('body').focus().click();
        } 
    })
    $('.aofi-share-box').add('.aofi-share-box input, h2').click( function(e) {
        e.stopPropagation();
    });
});
