$(document).ready(function(){
	if($.browser.msie&&$.browser.version<7){
		// $.ie6CssFix();
		$('#header img,#footer img').ifixpng();
		$('#header').css({background:'url(images/header.gif) no-repeat'});
		$('#middle').css({background:'url(images/middle.gif) repeat-y'});
		$('#bottom').css({background:'url(images/bottom.gif) no-repeat'});
	}
});
function goTo(str){$("#content").hide();url="load.php";data='p='+str;$.get(url,data,function(txt){$("#content").html(txt);$("#content").fadeIn("slow");});}
function goToPage(a,b){if(!b){b='_blank';}window.open(a,b);}
function fold(str){$("div#"+str).slideToggle();}
function showLogin(){$('#loginlink').click();$('#TB_window').css({background:'transparent',border:'none'});}
function showWrap(){tb_remove();$('div#wrap').show();$('div#content').show();}
function ext(a){return a.substr(a.lastIndexOf('.')+1);}
function getCookie(a){if(document.cookie.length>0){b=document.cookie.indexOf(a+"=");if(b!=-1){b+=a.length+1;c=document.cookie.indexOf(";",b);if(c==-1)c=document.cookie.length;return unescape(document.cookie.substring(b,c));}}return "0";}
function global(f,a,str,div){
	url="/scripts/actions.php";
	data="f="+f+"&a="+a;
	switch(f){
		case"gallery":
			url+="?page="+str;
			$.post(url,data,function(txt){$("#gallery").html(txt);$("#gallery").fadeIn("slow");if(!$.browser.msie&&!$.browser.version<7){$('#gallery .lightbox').lightbox();}});
		break;
		case"mail":
			data+="&title="+$("#title").val()+"&post="+$("#post").val();
			$.post(url,data,function(txt){$('#formid').html(txt).show();});
		break;
		case"search":
			if($('#searchinput'+str).val()!=''&&$('#searchinput'+str).val()!=' '){data+="&search="+$("#searchinput"+str).val();
			var xhr=$.post(url,data,function(txt){$("#BlogPosts").html(txt);$("#BlogPosts").fadeIn("slow");});
			$("#searchinput"+str).keypress(function(){xhr.abort();});}else{global("blog",a,0);}
		break;
		case"blog":
			data+="&blogpage="+str;
			$.post(url,data,function(txt){$("#blog").html(txt);$("#blog").fadeIn("slow");});
		break;
		case"login":
			$('#loginform').ajaxForm({target:'#TB_ajaxContent',url:'/scripts/actions.php',beforeSubmit:function(){if($('input[@name=username]').val()!=''&&$('input[@name=password]').val()!=''){$('#login-box').html("<center><img src='/images/loading.gif' alt='Loading...' /></center>").show();}else{return false;}},success:function(){$('#login-box').show();}});
		break;
	}
}
function admin(f,a,str,div,id,field){
	url="/scripts/actions.php";
	data="f="+f+"&a="+a;
	switch(f){
		case"image":
		switch(a){
			case"edit":
				data+="&id="+str;
				$.post(url,data,function(txt){$("#"+str).html(txt);$("#"+str).fadeIn("slow");});
			break;
			case"save":
				data+="&id="+str+"&name="+$("#input-name").val()+"&description="+$("#input-description").val();
				$.post(url,data,function(txt){$("#"+str).html(txt);$("#"+str).fadeIn("slow");});
			break;
			case"remove":
				data+="&file="+str+"&id="+div;
				$.post(url,data,function(txt){$("#"+div).html(txt);$("#"+div).fadeIn("slow");});
			break;
			case"thumb":
			case"rotate":
				data+="&file="+str+"&id="+div;
				$.post(url,data,function(txt){$("#preview").html(txt);$("#preview").fadeIn("slow");});
			break;
			case"upload":
				$('#upload-form').ajaxForm({url:url,beforeSubmit:function(){if(ext($('#upload-file').val().toLowerCase())=="png"||ext($('#upload-file').val().toLowerCase())=="gif"||ext($('#upload-file').val().toLowerCase())=="jpg"||ext($('#upload-file').val().toLowerCase())=="jpeg"){$('#preview').hide();$('#preview').after("<img src='/images/loading.gif' alt='Loading...' />").show();}else{return false;}},success:function(r){$('#preview').html(r);
				// goToPage("?p=imagegallery","_self");
				}});
			break;
		}
		break;
		case"users":
		switch(a){
			case"select":
			$("#l"+str).slideUp("slow",function(){
			var group=0;
			for(g=0;g<=getCookie('wr_group')&&g<=9;g++){group+="<option value='"+g+"' ";if(g==$('#g'+str+' div').html()){group+="selected='selected'";}group+=">"+g+"</option>";}
			$('#u'+str).html("<div><input type='text' id='ue"+$('#id'+str).html()+"' value='"+$('#u'+str+' div').html()+"' onKeyUp='if(event.keyCode==13||event.which==13){admin(\"users\",\"edit\",\""+$('#id'+str).html()+"\");}'/></div>");
			$('#k'+str).html("<div><input type='password' id='ke"+$('#id'+str).html()+"' value='' onKeyUp='if(event.keyCode==13||event.which==13){admin(\"users\",\"edit\",\""+$('#id'+str).html()+"\");}'/></div>");
			$('#g'+str).html("<div><select id='ge"+$('#id'+str).html()+"'>"+group+"</select></div>");
			$('#f'+str).html("<div class='functions'><img src='/images/delete.png' alt='' onClick='admin(\"users\",\"remove\",\""+str+"\");'/><img src='/images/save.png' alt='' onClick='admin(\"users\",\"edit\",\""+str+"\");'/></div>");
			$("#l"+str).slideDown("slow");});
			break;
			case"add":
			div='add';
			$.post(url,data,function(){setTimeout("goToPage('?p=users','_self');",500);});
			break;
			case"edit":
			div='l'+str;
			if($('#ke'+str).val()){
				$("#"+div).slideUp("slow",function(){
				$('#u'+str).html("<div onClick='admin(\"users\",\"select\",\""+str+"\");'>"+$('#ue'+str).val()+"</div>");
				$('#k'+str).html("<div onClick='admin(\"users\",\"select\",\""+str+"\");'>******</div>");
				$('#g'+str).html("<div onClick='admin(\"users\",\"select\",\""+str+"\");'>"+$('#ge'+str).val()+"</div>");
				$('#f'+str).html("<div class='functions'><img src='/images/delete.png' alt='' onClick='admin(\"users\",\"remove\",\""+str+"\");'/><img src='/images/edit.png' alt='' onClick='admin(\"users\",\"select\",\""+str+"\");'/></div>");});
				data+="&select="+str+"&name="+$('#ue'+str).val()+"&key="+$('#ke'+str).val()+"&group="+$('#ge'+str).val();
			}else{$('input[@value=""][@type=password]').animate({height:'hide'},function(){$('input[@value=""]').animate({height:'show'});});return false;}
			$.post(url,data,function(){$("#"+div).slideDown("slow");});
			break;
			case"reset":
			case"remove":
			data+="&select="+str;
			div='l'+str;
			$("#"+div).slideUp("fast");
			$.post(url,data,function(txt){$("#"+div).html(txt);$("#"+div).slideDown("slow");});
			break;
		}
		break;

		// admin(f,a,str,div,id,field){
		// 	url="/scripts/actions.php";
		// 	data="f="+f+"&a="+a;
		//		admin(\"pages\",\"save\",\"$row[weight]\",\"return\");

		case"pages":
		switch(a){
			case"edit":
			div="admin-NewPost";
			case"remove":
			data+="&weight="+str;
			$.post(url,data,function(txt){$("#"+div).html(txt);$("#"+div).fadeIn("slow");});
			break;
			case"add":
			case"save":
			if($("#admin-Title").val()!=''){var postbody=$(".jTagEditor-editor").val();if(postbody=='undefined'||postbody==''){postbody=$("admin-Body").val();}
			data+="&nweight="+str+"&weight="+$("#admin-Weight").val()+"&parent="+$("#admin-Parent").val()+"&title="+encodeURI($("#admin-Title").val())+"&post="+encodeURIComponent(postbody);
			$.post(url,data,function(){
				goToPage("?p=pages","_self")
			});
			}
			break;
		}
		break;
		case"blog":
		switch(a){
			case"add":
			if($("#admin-Title").val()!=''&&$("#admin-NewPost .jTagEditor-editor").val()!=''){
			cf=$("#admin-Cattext").val();
			if(cf==""){cf=$("#admin-Catsel").val();}var postbody=$(".jTagEditor-editor").val();if(postbody=='undefined'||postbody==''){postbody=$("admin-Body").val();}
			data+="&title="+encodeURI($("#admin-Title").val())+"&post="+encodeURI(postbody)+"&category="+encodeURI(cf);
			$.post(url,data,function(txt){$("#return").html(txt);goToPage("?p=blog","_self");});}
			break;
			case"edit":
			data="f=blog&a=select&date="+str;
			$.post(url,data,function(txt){$("#admin-NewPost").html(txt);$("#admin-NewPost").fadeIn("slow");});
			break;
			case"save":var postbody=$(".jTagEditor-editor").val();if(postbody=='undefined'||postbody==''){postbody=$("admin-Body").val();}
			if($("#admin-Title").val()!=''&&postbody!=''){
			data+="&title="+encodeURIComponent($("#admin-Title").val())+"&post="+encodeURIComponent(postbody)+"&date="+str;
			$.post(url,data,function(){goToPage("?p=blog","_self");});}
			break;
			case"remove":
			$("#"+div).hide();
			data+="&date="+str;
			$.post(url,data,function(txt){$("#"+div).html(txt);$("#"+div).fadeIn("slow");});
			break;
		}
		break;
		case"queue":
		switch(a){
			case"allow":
			data+="&id="+div;
			case"clear":
			$.post(url,data,function(){goTo(f);});
			break;
			case"deny":
			data+="&table="+str+"&item="+div+"&field="+field+"&id="+id;
			$.post(url,data,function(){goTo(f);});
			break;
		}
		break;
		case"notes":
		switch(a){
			case"add":
			if($("#admin-Body").val()!=''){
				data+="&message="+$("#admin-Body").val();
				$.post(url,data,function(txt){$("#return").html(txt);$("#return").fadeIn("slow");});}
			break;
			case"remove":
				data+="&id="+str;
				$.post(url,data,function(txt){$("#note_"+str).html(txt);$("#note_"+str).fadeIn("slow");});
			break;
		}
		break;
		case"advanced":
		switch(a){
			case"save":
				data+="&file="+$("#configinput"+str).val()+"&item="+str;
				$('#config'+str).slideUp("fast",function(){
					$('#f'+str).html("<div class='object-functions'><img src='/images/edit.png' title='edit' alt='edit' onClick='admin(\"advanced\",\"select\",\""+str+"\");'/></div>");
					$('#val'+str).html("<div onClick='admin(\"advanced\",\"select\",\""+str+"\");'>"+$("#configinput"+str).val()+"</div>");
					$.post(url,data,function(){$('#config'+str).slideDown("slow");});
				});
				
				break;
			case"select":
				data+="&item="+str;
				$('#config'+str).slideUp("fast",function(){
					$('#f'+str).html("<div class='object-functions'><img src='/images/save.png' title='save' alt='save' onClick='admin(\"advanced\",\"save\",\""+str+"\");'/></div>");
					$.post(url,data,function(txt){$('#val'+str).html(txt);$('#config'+str).slideDown("slow");});
				});
			break;
			case"tableoptimize":
			case"tablereset":
				data+="&name="+str;
				$.post(url,data,function(txt){$('#'+a+str).animate({opacity:'hide'},'slow',function(){$('#'+a+str).animate({opacity:'show'},'slow');});});
			break;
		}
		break;
		case"shop":
		switch(a){
			case"add":var postbody=$(".jTagEditor-editor").val();if(postbody=='undefined'||postbody==''){postbody=$("admin-Body").val();}
			if($("#admin-Title").val()!=''){
				$('#upload-form').ajaxSubmit({target:'#preview',url:url,beforeSubmit:function(){if(ext($('#upload-file').val().toLowerCase())=="png"||ext($('#upload-file').val().toLowerCase())=="gif"||ext($('#upload-file').val().toLowerCase())=="jpg"||ext($('#upload-file').val().toLowerCase())=="jpeg"||ext($('#upload-file').val())==""){$('#preview').html("<img src='/images/loading.gif' alt='Loading...' />").show();}else{return false;}},success:function(){goToPage("?p=shop","_self");}});
			}
			break;
			case"save":var postbody=$(".jTagEditor-editor").val();if(postbody=='undefined'||postbody==''){postbody=$("admin-Body").val();}
			if($("#admin-Title").val()!=''){
				data+="&title="+encodeURIComponent($("#admin-Title").val())+"&description="+encodeURIComponent(postbody)+"&category="+encodeURIComponent($("#admin-Catsel").val())+"&weight="+encodeURIComponent($("#admin-Weight").val())+"&id="+str;
				$.post(url,data,function(){goToPage("?p=shop","_self");});
			}
			break;
			case"edit":
			data="f=shop&a=select&id="+str;
			$.post(url,data,function(txt){$("#upload-form").html(txt);$("#upload-form").fadeIn("slow");});
			break;
			case"remove":
			$("#shop-"+str).hide();
			data+="&id="+str;
			$.post(url,data,function(txt){$("#shop-"+str).html(txt);$("#shop-"+str).fadeIn("slow");});
			break;
		}
		break;
		}
}