// JavaScript Document

function outputlink( str1, str2, str3, str4 ) {
	document.write("<a href=" + "mai" + "lto:" + str1 + str2 + str3 + ">" + str4 + "</a>");
}

function show_popup( content, title, callback ) {
	
//	if( $('#popup_content') ) { 
//		$('#dialog').dialog('destroy');
//		$('#dialog').remove();
//	}
//	
//	$("<div>").attr('id','dialog').appendTo($(document));
//	$("<div>").attr('id','popup_content').appendTo( $('#dialog') ).html( content );
	
	var btns;
	if (callback != 'NONE') { 
		if (callback != 'NULL') { 
			btns = { 
				"Ok": function() { 
					eval( callback ); 
					$(this).dialog('destroy'); 
					$('#dialog').remove(); 
				},  
				"Cancel" : function() { 
					$(this).dialog('destroy'); 
					$('#dialog').remove(); 
				} 
			};
		} else {
			btns = { 
				"Cancel" : function() { 
					$(this).dialog('destroy'); 
					$('#dialog').remove(); 
				} 
			};
		}
	}
	$("#dialog").dialog({
		bgiframe: true,
		width: 670,
		height: 550,
		modal: true,
		title: title,
		buttons: btns,
		close: function() {
			$(this).dialog('destroy');
			$('#dialog').remove();
		}
	});
}



function validateUser() {
	var ret = true;
	var returnstr = '';
	$("input[required='true']").each( function(el) {
		if ( $(this).val() == '' ) { 
			ret = false;
			returnstr += "\n\rPlease fill in "+this.name;
		}
	} );
	if ( ret ) $('#userForm').submit();
	else alert( returnstr );
}
function validateMedia() {	$("input:file[value='']").attr('disabled',true);
	$("#editmedia").submit();
}
function delUser( id ) {
	if ( confirm( "Permanently Delete this User. Confirm?" ) ) {
		window.location.href = 'adminuser.php?action=deluser&id='+id;
	}
}

function checkUsername() {
	if ( $('#username_field').val() != '' ) {
		$('#checkText').text("Checking ... ");
		$.ajax( {
			type: 'post',
			url: 'ajax_fns.php',
			data: "action=usernameCheck&username="+$('#username_field').val(),
			success: function(msg) {
				$('#checkBtn').removeClass('ui-icon-help');
				$('#checkBtn').removeClass('ui-icon-check');
				$('#checkBtn').removeClass('ui-icon-notice');
				if ( msg == '0' ) {
					$('#checkBtn').addClass('ui-icon-notice');
				} else {
					$('#checkBtn').addClass('ui-icon-check');
				}
				$('#checkText').text("Check Username");
			} 
		} );
	}
}

function checkPageName( id ) {
	if ( $('#section_name_field').val() != '' ) {
		$('#checkText').text("Checking ... ");
		$.ajax( {
			type: 'post',
			url: 'ajax_fns.php',
			data: "action=sectionnameCheck&id="+id+"&name="+$('#section_name_field').val(),
			success: function(msg) {
				$('#checkBtn').removeClass('ui-icon-help');
				$('#checkBtn').removeClass('ui-icon-check');
				$('#checkBtn').removeClass('ui-icon-notice');
				if ( msg == 'bad' ) {
					$('#checkBtn').addClass('ui-icon-notice');
				} else {
					$('#checkBtn').addClass('ui-icon-check');
				}
				$('#checkText').text("Check Username");
			} 
		} );
	}
}

function validateSection() {
	$("#editSection").submit();
}

function removeFile( id ) {
	if ( confirm( "Delete this file?" ) ) {
		$.ajax( {
			type: 'post',
			url: 'ajax_fns.php',
			data: "action=deleteFile&id="+id,
			success: function(msg) {
				if ( msg == '1' ) {
					$('#container_'+id).remove();
				}
			} 
		} );
	}
}

function editFile( id ) {
	$.ajax( {
		type: 'post',
		url: 'ajax_fns.php',
		data: "action=editFile&id="+id,
		success: function(msg) {
			show_popup( msg, "Media File", "" );
		} 
	} );
}

function editAttachment( id ) {
	$.ajax( {
		type: 'post',
		url: 'ajax_fns.php',
		data: "action=editAttachment&id="+id,
		success: function(msg) {
			show_popup( msg, "Media Attachment", "" );
		} 
	} );
}
function addMediaFile() {
	$('#media_item_template').clone().removeClass('hidden').attr('id','').addClass('media_item').appendTo("#newFileContainer");
	
}

function rotateSecondMenu( id ) {
	$("#secondMenu").find(".secondMenuBtn").unbind("mouseenter");
	var curr = $('#secondMenuContent').find('div:visible').attr('index');
	if ( id == '' ) {
		var next = parseInt(curr) + 1;
		if ( next == 5 ) next = 1;
	} else {
		var next = id;	
	}
	if ( $("#brief"+next).hasClass('hidden') ) {
		$("#brief"+next).hide();
		$("#brief"+next).removeClass('hidden');
	}
	if ( curr != next ) {
		$("#brief"+curr+"btn").removeClass('on');
		$("#brief"+next+"btn").addClass('on');
		
		$("#brief"+curr).fadeOut('slow');
		$("#brief"+next).fadeIn('slow');
	}
	setTimeout("$('#secondMenu').find('.secondMenuBtn').bind('mouseenter', function() { rotateSecondMenu( $(this).attr('index') ); } ) ;", 650);
//	$('#secondMenu').find('.secondMenuBtn').bind('mouseenter', function() { rotateSecondMenu( $(this).attr('index') ); } ) ;
}
///////////////////////////////
// PERMISSION FUNCTIONS
///////////////////////////////
	function addPerm( type ) {
		if ( type == 'page' || type == 'attachement' ) {
			$("#permTemplate").clone().appendTo("#permContainer").css('display','block').attr('id','');
		} else {
			$("#permTemplateFile").clone().appendTo( $(type).parent().find('div.permFileContainer') ).css('display','block').attr('id','');
		}
	}
	
///////////////////////////////
// ONLONAD FUNCTIONS
///////////////////////////////
	$(function() { 
		$('.ui-button').live('mouseover', function () { $(this).toggleClass('ui-state-default'); $(this).toggleClass('ui-state-hover'); } );		   
		$('.ui-button').live('mouseout', function () { $(this).toggleClass('ui-state-default'); $(this).toggleClass('ui-state-hover'); } );		   
		$('.ui-button-sm').live('mouseover', function () { $(this).toggleClass('ui-state-default'); $(this).toggleClass('ui-state-hover'); } );		   
		$('.ui-button-sm').live('mouseout', function () { $(this).toggleClass('ui-state-default'); $(this).toggleClass('ui-state-hover'); } );		   
		$('#username_field').bind('change', function() { 
			$('#checkBtn').removeClass('ui-icon-check');
			$('#checkBtn').removeClass('ui-icon-notice');
			$('#checkBtn').addClass('ui-icon-help');
		} );
		
		$('img.rollover').live('mouseover',function(){ $(this).attr('src', $(this).attr('src').replace( ".", "_on." ) )});
		$('img.rollover').live('mouseout',function(){ $(this).attr('src', $(this).attr('src').replace( "_on.", "." ) )});
			
		$('span.rollover').live('mouseover',function(){ $(this).css('background-image', $(this).css('background-image').replace( ".png", "_on.png" ) ); $(this).css('color','#ffffff'); });
		$('span.rollover').live('mouseout',function(){ $(this).css('background-image', $(this).css('background-image').replace( "_on.png", ".png" ) ); $(this).css('color','#000000'); });
		
		$("#secondMenu").find(".secondMenuBtn").bind("mouseenter", function() { rotateSecondMenu( $(this).attr('index') ); } ) ;
		
		$("#loginbtn").bind('click',function() {
			if ( $('#container_login').is(":hidden") ) {
				$('#container_login').fadeIn();
			} else {
				$('#container_login').fadeOut();
			}
		} );
		$("#loginform input").keypress(function (e) {  
			if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {  
				$('#loginform').submit();
				return false;  
			} else {  
				return true;  
			}  
		});  
		
		$('a[rel*=lightbox]').lightBox({maxHeight: 300,maxWidth: 500});
		//setTimeout("rotateSecondMenu('')", 2000);
	 } );
