jQuery.noConflict();
jQuery(document).ready(function(){
	/*$('#pageContent').show();
	$.ajax({ 
        url:window.location.href, 
        cache:false, 
        success:function(){
            
        }
    });*/
	if(jQuery('#wpabar')){
		jQuery('#wpabar').css("width", jQuery("body").width());
	}
	jQuery("#blogname").blur(function(){
		if(jQuery(this).val()){
			jQuery('#validCheck').fadeIn(500);
			jQuery.post(site_url + "/wp-validate.php", {domain:jQuery(this).val()}, function(data){
				if(data != ''){
					jQuery('#validCheck').hide();
					jQuery('#validSuccess').hide();
					jQuery('#validErr div').html(data);
					jQuery('#validErr').fadeIn(1000);
				}
				else{
					jQuery('#validCheck').hide();
					jQuery('#validErr').hide();
					jQuery('#validSuccess').fadeIn(1000);
				}
			});
			return false;
		}
	});
});
function createPage(){
	var result = false;
	if(!jQuery('#pageform #txtname').val()){
		jQuery('#pageform #txtname').addClass('inputerror');
		jQuery('#pageform #txtname').focus();
		return false;
	}
	else{
		jQuery('#pageform #txtname').addClass('inputbox').removeClass("inputerror");
		result = true;
	}
	if(result == true){
		jQuery('#pageform .button').attr("value", "Loading...");
		jQuery('#pageform .button').attr("disabled", "disabled");
		jQuery.ajax({
			url: site_url + '/wp-linkpage.php',
			data: 'page=&a=add&setVar=yes&name=' + jQuery('#pageform #txtname').val(),
			type: 'post',
			cache: false,
			dataType: 'html',
			complete: function(xhr){
				window.location.href = xhr.responseText;
			},
			error:formValidate.error
		});
	}
	else{}
	return false;
}
function deleteSection(sID, pID, column, pos, totalS){
	if(confirm('Are you sure want to delete this Section?')){
		jQuery.ajax({
			url: site_url + '/wp-linkpage.php',
			data: 'section=&a=delete&setVar=yes&sID=' + sID + '&pID='+ pID + '&column=' + column,
			type: 'post',
			cache: false,
			dataType: 'html',
			complete: function(xhr) {
				var totalR = xhr.responseText;
				var s1 = jQuery.SortSerialize('sort' + column);
				arr = s1.hash.split("&");
				j = 0;
				var change = "Yes";
				for(i = 0; i < arr.length; i++){
					val = arr[i];
					value = val.split("=");
					if("div" + pos + "_" + column + "_" + sID == value[1]){
						change = "No";
						j = i;
					}
					if(change == "Yes" && i > j){
						ch = value[1].split("_");
						var sortNo = parseInt(ch[0][3]) - 1;
						jQuery("#" + value[1]).attr("id", "div" + sortNo + "_" + ch[1] + "_" + ch[2]);
			jQuery("a#delete" + ch[2]).attr("onclick", "deleteSection('"+ch[2]+"', '"+pID+"', "+column+", "+sortNo+", "+totalR+");");
					}
				}
				jQuery("#div" + pos + "_" + column + "_" + sID).fadeOut("slow");
			},
			error:""
		});
	}
}
function createLink(){
	if(formValidate1.validateLink()) {
		jQuery('#linkform .button').attr("value", "Loading...");
		jQuery('#linkform .button').attr("disabled", "disabled");
		var name = "";
		var url = "";
		var desc = "";
		var hgt = "";//Highlight
		var bname = "";
		var baddress = "";
		var banner = "";
		var furl = "";
		var fitems = "";
		var wtitle = "";
		var wcode = "";
		var status = "";
		var section = "";
		var position = "";
		var after = "";
		var action = "";
		var pID = "";
		//Link
		name = document.linkform.txtname.value;
		url = document.linkform.txtaddress.value;
		desc = document.linkform.txtdesc.value;
		hgt = document.linkform.txthighlight.value;
		//Banner
		bname = document.linkform.txtbname.value;
		baddress = document.linkform.txtbaddress.value;
		banner = document.linkform.txtbanner.value;
		//RSS
		furl = document.linkform.txtfeed_url.value;
		fitems = document.linkform.txtfeed_items.value;
		//Widget
		wtitle = document.linkform.txtwtitle.value;
		wcode = document.linkform.txtcode.value;
		if(document.linkform.rdvisible[0].checked == true) status = 1;
		else if(document.linkform.rdvisible[1].checked == true) status = 0;
		section = document.linkform.lstsection.value;
		if(document.linkform.rdposition[0].checked == true) position = document.linkform.rdposition[0].value;
		else if(document.linkform.rdposition[1].checked == true) position = document.linkform.rdposition[1].value;
		else if(document.linkform.rdposition[2].checked == true){
			position = document.linkform.rdposition[2].value;
			after = document.linkform.lstposition.value;
		}
		pID = document.linkform.pID.value;
		option = document.linkform.hdnoption.value;
		if(document.linkform.hdnid.value == "") action = "add"; else action = "edit";
		if(option == 'link'){
			jQuery.post(site_url + '/wp-linkpage.php', {link:'', a:action, setVar:'yes', name:name, url:url, desc:desc, hgt:hgt, status:status, sid:section, pos:position, after:after, pID:pID, option:option}, function(data){
				jQuery("#div" + section).html(data);
				tb_remove();
				return false;
			});
		}
		else if(option == 'banner'){
			jQuery.post(site_url + '/wp-linkpage.php', {link:'', a:action, setVar:'yes', bname:bname, baddress:baddress, banner:banner, status:status, sid:section, pos:position, after:after, pID:pID, option:option}, function(data){
				jQuery("#div" + section).html(data);
				tb_remove();
				return false;
			});
		}
		else if(option == 'rss'){
			jQuery.post(site_url + '/wp-linkpage.php', {link:'', a:action, setVar:'yes', furl:furl, fitems:fitems, status:status, sid:section, pos:position, after:after, pID:pID, option:option}, function(data){
				jQuery("#div" + section).html(data);
				tb_remove();
				return false;
			});
		}
		else if(option == 'widget'){
			jQuery.post(site_url + '/wp-linkpage.php', {link:'', a:action, setVar:'yes', wcode:wcode, wtitle:wtitle, status:status, sid:section, pos:position, after:after, pID:pID, option:option}, function(data){
				self.parent.window.location.reload();
				return false;
			});
		}
	}
	else{}
	return false;
}
function linkTabs(option){
	jQuery('#hdnoption').val(option);
	jQuery('.link_tab a').removeClass('current');
	jQuery('a#' + option + '_tab').addClass('current');
	jQuery('#link_options div').each(function(){
		if(jQuery(this).css('display') == 'block') jQuery(this).fadeOut();
	});
	setTimeout(function(){
		jQuery('div#' + option + '_div').fadeIn();
	}, 400);
}
//Class Definition
var formValidate1 = {
	message: null,
	validateLink: function(){
		option = document.linkform.hdnoption.value;
		if(option == 'link'){
			if(!jQuery('#linkform #txtname').val()){
				jQuery('#linkform #txtname').addClass('inputerror');
				jQuery('#linkform #txtname').focus();
				return false;
			}
			else{jQuery('#linkform #txtname').addClass('inputbox').removeClass('inputerror');}
			if(!jQuery('#linkform #txtaddress').val()){
				jQuery('#linkform #txtaddress').addClass('inputerror');
				jQuery('#linkform #txtaddress').focus();
				return false;
			}
			else{
				jQuery('#linkform #txtaddress').addClass('inputbox').removeClass('inputerror');
				return true;
			}
		}
		else if(option == 'banner'){
			if(!jQuery('#linkform #txtbname').val()){
				jQuery('#linkform #txtbname').addClass('inputerror');
				jQuery('#linkform #txtbname').focus();
				return false;
			}
			else{jQuery('#linkform #txtbname').addClass('inputbox').removeClass('inputerror');}
			if(!jQuery('#linkform #txtbaddress').val()){
				jQuery('#linkform #txtbaddress').addClass('inputerror');
				jQuery('#linkform #txtbaddress').focus();
				return false;
			}
			else{jQuery('#linkform #txtbaddress').addClass('inputbox').removeClass('inputerror');}
			if(!jQuery('#linkform #txtbanner').val()){
				jQuery('#linkform #txtbanner').addClass('inputerror');
				jQuery('#linkform #txtbanner').focus();
				return false;
			}
			else{
				jQuery('#linkform #txtbanner').addClass('inputbox').removeClass('inputerror');
				return true;
			}
		}
		else if(option == 'rss'){
			if(!jQuery('#linkform #txtfeed_url').val()){
				jQuery('#linkform #txtfeed_url').addClass('inputerror');
				jQuery('#linkform #txtfeed_url').focus();
				return false;
			}
			else{jQuery('#linkform #txtfeed_url').addClass('inputbox').removeClass('inputerror');}
			if(!jQuery('#linkform #txtfeed_items').val()){
				jQuery('#linkform #txtfeed_items').addClass('inputerror');
				jQuery('#linkform #txtfeed_items').focus();
				return false;
			}
			else{
				jQuery('#linkform #txtfeed_items').addClass('inputbox').removeClass('inputerror');
				return true;
			}
		}
		else if(option == 'widget'){
			if(!jQuery('#linkform #txtwtitle').val()){
				jQuery('#linkform #txtwtitle').addClass('inputerror');
				jQuery('#linkform #txtwtitle').focus();
				return false;
			}
			else{jQuery('#linkform #txtwtitle').addClass('inputbox').removeClass('inputerror');}
			if(!jQuery('#linkform #txtcode').val()){
				jQuery('#linkform #txtcode').addClass('inputerror');
				jQuery('#linkform #txtcode').focus();
				return false;
			}
			else{
				jQuery('#linkform #txtcode').addClass('inputbox').removeClass('inputerror');
				return true;
			}
		}
	}
};
