$(function(){
	init();
	rollover("sf-image","gif");
});
init=function(){
	$("#wrapper").cornerz({radius:10,corners:"tr tl br bl",background:"#f5f5f5"});

	$(".sf-menu").superfish({
		delay:       1000,
		animation:   {opacity:'show',height:'show'},
		speed:       'fast',
		autoArrows:  false,
		dropShadows: false
	});
	$(".lightbox").nyroModal({
		minWidth: 20,
		minHeight: 20,
		closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close"></a>',
		padding: 0
	});
	$(".tooltip[title],.tooltipB[title]").qtip({
		content: {
			text: $(this).attr("title")
		},
		style: {
			tip:true,
			border:{
				width:1,
				radius:1,
				color:'#F2DA00'
			},
			background:'#FFFACD',
			color:'#000000',
			width:{
				min:0,
				max:500
			}
		}
	});
}
rollover=function(clas,ext){
	//Preload all rollovers
	$("."+clas+" img").each(function() {
		// Set the original src
		rollsrc = $(this).attr("src");
		if(ext=="gif"){
			rollON = rollsrc.replace(/.gif$/ig,"2."+ext);
		}else{
			rollON = rollsrc.replace(/.png$/ig,"2."+ext);
		}
		$("<img>").attr("src", rollON);
	});
	//Navigation rollovers
	$("."+clas+" a").mouseover(function(){
		imgsrc = $(this).children("img").attr("src");
		matches = imgsrc.match(/_over/);
		//Don't do the rollover if state is already ON
		if (!matches) {
			if(ext=="gif"){
				imgsrcON = imgsrc.replace(/.gif$/ig,"2."+ext);
			}else{
				imgsrcON = imgsrc.replace(/.png$/ig,"2."+ext);
			}
			//Strip off extension
			$(this).children("img").attr("src", imgsrcON);
		}
	});
	$("."+clas+" a").mouseout(function(){
		$(this).children("img").attr("src", imgsrc);
	});
}
function sTab(tab,showonly,parent){
	$("."+parent).hide();
	$("#"+showonly).show();
	$(tab).parent().parent("div.tab_header").children("div.tab_child,div.tab_child_sel").removeClass("tab_child").addClass("tab_child_sel");
	$(tab).parent().removeClass("tab_child_sel").addClass("tab_child");
}
function sTabB(tab,parent,showonly){
	$("."+parent).hide();
	$("#"+showonly).show();
}
tinyMCE_init=function(){
	tinyMCE.init({
		// General options
		mode : "exact",
		elements : "editor",
		theme : "advanced",
		skin : "o2k7",
		skin_variant : "silver",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",
		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		// Example content CSS (should be your site CSS)
		content_css : "css_mce.css",
		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",
		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
}
var lastpage;
var lastobj;
var load_html="";
prepares=function(url, obj, fn, fx){
	prepare(url, obj, fn, fx, load_html);
}
prepare=function(url, obj, fn, fx, loader){
	lastpage=url;
	if(!obj){obj="content"};
	lastobj=obj;
	if(!loader){
		load_gif_=load_html;
	}else{
		load_gif_=loader;
	}
	if(fx!=null){
		$.get(url,function(res_data){
			$("#"+obj).html(res_data);
			init();
			if(fn!=null){fn();}
		})
	}else{
		if($("#"+obj).html()==""){
			$("#"+obj).html(load_gif_).fadeIn("normal",function(){
				$.get(url,function(res_data){
					//$("#"+obj).fadeOut("fast",function(){
						$("#"+obj).html(res_data).fadeIn("normal",function(){
							init();
							if(fn!=null){fn();}
						});
					//});
				});
			});
		}else{
			$("#"+obj).fadeOut("fast",function(){
				$("#"+obj).html("");
				$("#"+obj).html(load_gif_).fadeIn("normal",function(){
					$.get(url,function(res_data){
						//$("#"+obj).fadeOut("fast",function(){
							$("#"+obj).html(res_data).fadeIn("normal",function(){
								init();
								if(fn!=null){fn();}
							});
						//});
					});
				});
			});
		}
	}
}
openDialog=function(url,div,width,height,fn,param){
	if(param=="unmodal"){
		$.get(url,function(res){
			$("#"+div).remove();
			$("#dialog_pane").html(res);
			$("#"+div).dialog({
				width:width+"px",
				height:height+"px",
				resizable: false,
				overlay:{opacity:0.1,background:"#000000"}
			});
			if(fn!=null){fn();}
		});
	}else{
		$.get(url,function(res){
			$("#"+div).remove();
			$("#dialog_pane").html(res);
			$("#"+div).dialog({
				width:width+"px",
				height:height+"px",
				resizable: false,
				modal:true,
				overlay:{opacity:0.1,background:"#000000"}
			});
			if(fn!=null){fn();}
		});
	}
}
closeDialog=function(obj){
	if(obj==null){obj="dummy";}
	$("#"+obj).animate({opacity:1.0},1500,function(){
		$.nyroModalRemove();
	});
}
setForm=function(form,target,fn,loadfx){
	if(loadfx!=null){
		loader=loadfx;
	}else{
		loader=load_html;
	}
	$("#"+form).ajaxForm({
		target:"#"+target,
		beforeSubmit:function(){
			$("#"+target).html(loader);
		},
		success:function(){
			$("#"+target).fadeIn("slow");
			init();
			if(fn!=null){fn();}
		}
	});
}
