/* Popin on form next */
var popinOpen = false;
var alreadyOpened = false;

function openPopinSelector(selector){
    initializePopin();
    $('#popin .content').html($(selector).html());
    openPopin();
}

function openPopinIframe(url,width,height,name){
    initializePopin();
    if(typeof url == 'object')
        url = $(url).attr('href');
    if(url.length > 0){
        $('#popin .content').html('<iframe src="'+url+'" '+((width)?'width="'+width+'"':'')+' '+((width)?'height="'+height+'"':'')+' '+((width)?'name="'+name+'"':'')+'  frameborder="0"></iframe>');
        if(height)
            $('#popin .content').css('height',height);        
        openPopin();
    }else
        alert('Unsupported type for iframe source');
        
}
function openPopin(){
    initializePopin();
	popinOpen = true;
    centerPopin(true);
}	
function closePopin(){
	$('#modal').css('display','none');
	$('#popin').css('display','none');
	popinOpen = false;
}
function centerPopin(open){
	if($('#popin') && popinOpen){
        topOffset = Math.ceil(($(window).height()-$('#popin').outerHeight())/2);
        leftOffset = Math.ceil(($(window).width()-$('#popin').outerWidth())/2);
		$('#popin').css('top',((topOffset<0)?0:topOffset).toString()+"px");
		$('#popin').css('left',((leftOffset<0)?0:leftOffset).toString()+"px");
		if(open){
			$('#popin').css('display','block');
			$('#modal').css('display','block');
        }
	}
}
function initializePopin(){
    if(!alreadyOpened){
        $('body').append('<div id="modal"></div>');
        $('body').append('<div id="popin"><div class="content"></div><a href="#" id="popinClose">Close</a></div>');

        $(window).resize(function(){
            centerPopin();
        });
        $('#popinClose').click(function(){
            closePopin();
            return false;
        });
        alreadyOpened = true;
    }

}
