$(document).ready(function() {
	
 	$('.popupLink, a[rel~=popup]').live("click", function(event) {
		var url 	= event.target.href;
		var width 	= getURLParameter('w', url);
		var height 	= getURLParameter('h', url);
		var scroll 	= getURLParameter('s', url);
		
		openPopup(url, url, width, height, scroll);
		
		return false;
	});
	
	$('a[rel~=new]').live("click", function() {
		window.open($(this).attr('href'));
        return false;
	});
	
	/**
     * Skinned file inputs
     */
    
    $("div.skinned-file input")
    	.change(function(e) {
			var filename = $(this).val().replace(/^.*\\/, '');
			
			$(this).siblings('p.filename').text(filename);
		})
		.bind('mouseenter mouseleave', function() {
			$(this).siblings('a').toggleClass('active');
		});
	
    
	/**
	 * Skinned checkboxes
	 */
    
	$('.skinned-checkbox input')
		.change(function(e) {
			$(this).parent().toggleClass('checked');
		})
		.filter(':checked') // if checked by default
		.parent()
		.addClass('checked');
	
});

function openPopup(mypage,myname,w,h,scroll)
{
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
	
	var realname = myname.substring(1,4);
	
	window.open(mypage, realname, settings);
	return false;
}

function getURLParameter(name, url)
{
	url = (url != null)?url:window.location.href;
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	if( results == null )
		return "";
	else
		return results[1];
}

