function login(ab) {
	if ($("head > link[src='/jquery.form.js']").length==0) {
		$("head").append('<script type="text/javascript" src="/jquery.form.js" />');
	}
	ab.user.select();
	if ($(ab).attr("action")) {
		action = $(ab).attr("action");
	} else {
		action = '?!manage';
	}
	$.get(action,$(ab).formToArray(),
	function (data) { 
		if(data.match(/^false/)) {
			message = data.replace(/^false(\||)/," || ").replace(/^\s\|\|\s$/,"");
			$(".message").text("Login Incorrect"+message).hide().fadeIn(500).fadeOut(3000);
		} else {
			popup(data);
		}
	});
	return false;
}

var global_click;

function load_editor(object) {
	if ($("head > link[src='/wymeditor/wymeditor/jquery.wymeditor.pack.js']").length==0) {
//		alert("test");
		$("head").append('<script type="text/javascript" src="/wymeditor/wymeditor/jquery.wymeditor.pack.js"></script>');
	}
	if (!object) {
		object = '.editor';
	}
	jQuery(object).wymeditor({

      //classes panel
      classesItems: [
        {'name': 'date', 'title': 'PARA: Date', 'expr': 'p'},
        {'name': 'hidden-note', 'title': 'PARA: Hidden note',
         'expr': 'p[@class!="important"]'},
        {'name': 'important', 'title': 'PARA: Important',
         'expr': 'p[@class!="hidden-note"]'},
        {'name': 'border', 'title': 'IMG: Border', 'expr': 'img'},
        {'name': 'special', 'title': 'LIST: Special', 'expr': 'ul, ol'}
      ],
      
      //we customize the XHTML structure of WYMeditor by overwriting 
      //the value of boxHtml. In this example, "CONTAINERS" and 
      //"CLASSES" have been moved from "wym_area_right" to "wym_area_top":
      boxHtml:   "<div class='wym_box'>"
              + "<div class='wym_area_top'>"
              + WYMeditor.TOOLS
              + WYMeditor.CONTAINERS
              + WYMeditor.CLASSES
              + "</div>"
              + "<div class='wym_area_left'></div>"
              + "<div class='wym_area_right'>"
              + "</div>"
              + "<div class='wym_area_main'>"
              + WYMeditor.HTML
              + WYMeditor.IFRAME
              + WYMeditor.STATUS
              + "</div>"
              + "<div class='wym_area_bottom'>"
              + "</div>"
              + "</div>",
 
      //postInit is a function called when WYMeditor instance is ready
      //wym is the WYMeditor instance
      postInit: function(wym) {
 
        //we make all sections in area_top render as dropdown menus:
        jQuery(wym._box)
            //first we have to select them:
            .find(".wym_area_top .wym_section")
            //then we remove the existing class which make some of them render as a panels:
            .removeClass("wym_panel")
            //then we add the class which will make them render as a dropdown menu:
            .addClass("wym_dropdown")
            //finally we add some css to make the dropdown menus look better:
            .css("width", "160px")
            .css("float", "left")
            .css("margin-right", "5px")
            .find("ul")
            .css("width", "140px");
            jQuery(wym._box).find(wym._options.iframeSelector)
                .css('height', '350px');
 
        //add a ">" character to the title of the new dropdown menus (visual cue)
        jQuery(this._box).find(".wym_tools, .wym_classes ")
            .find(WYMeditor.H2)
            .append("<span>&nbsp;&gt;</span>");
       	
        $(".layover ul.manage_pages a")
        .click(function() {
      	$.get(this.href,{"!manage":"get_page_zone"},function(data){wym.html(data);});
      	if (obj = $(".editor").get(0)) {
  			while (!obj.parentNode.tagName.match("FORM")) {
  				obj = obj.parentNode;
  			}
  			form = obj.parentNode;
  			if (this.href.match(/\?/)) {
	  				action = this.href + "&!manage=save_page_zone";
				} else {
					action = this.href + "?!manage=save_page_zone";
				}
	  			form.action = action;
      	}
      	return false;
      });
      $(".layover ul.manage_pages a")
      .mouseenter(function() {
    	  	$(".layover ul.manage_pages li").children(".manage_page_zones").remove();
    	  	obj = this.parentNode;
    	  	$.get(this.href,{"!manage":"list_page_zones"},function(data) {
    	  		if (!data.match(/^only_one/)) {
    	  			$(obj).append('<ul class="manage_page_zones" style="position: absolute;background:#999;z-index:500;left: 10px;list-style:none;padding: 4px;">' + data + '</ul>');
    	  			$(obj).children("ul.manage_page_zones").children("li").children("a")
    	  			.click(function() {
    	  		      	$.get(this.href,{"!manage":"get_page_zone"},function(data){wym.html(data);});
    	  		      	if (obj = $(".editor").get(0)) {
    	  		  			while (!obj.parentNode.tagName.match("FORM")) {
    	  		  				obj = obj.parentNode;
    	  		  			}
    	  		  			form = obj.parentNode;
    	  		  			if (this.href.match(/\?/)) {
    	  		  				action = this.href + "&!manage=save_page_zone";
    	  					} else {
    	  						action = this.href + "?!manage=save_page_zone";
    	  					}
    	  		  			form.action = action;
    	  		      	}
    	  		      	return false;
    	  		      });
    	  		}
	  		});
      })
      $(".layover ul.manage_pages").mouseleave(function() {$(".layover ul.manage_pages li").children(".manage_page_zones").remove();});
      
      $(".layover ul.manage_pages li")
      .css("position","relative");
      
            regex = new RegExp("http://.*?/");
            
            first_load = window.location.href.replace(regex,"/admin/").replace(/\#/,'');
            obj = $(".editor").get(0);
            while (!obj.parentNode.tagName.match("FORM")) {
            	obj = obj.parentNode;
            }
            form = obj.parentNode;
            if (window.location.href.match(/\?/)) {
	  				action = window.location.href + "&!manage=save_page_zone";
				} else {
					action = window.location.href + "?!manage=save_page_zone";
				}
  			form.action = action;
            $.get(window.location.href,{"!manage":"get_page_zone"},function(data){wym.html(data);});
        }
    });
}
	
jQuery(function() {
	$(".admin a.ma")
	.click(function () {
		$.get(this.href,'',function(data){ popup(data); });
		return false;
	});
});

function set_manage_links() {
	$(".layover a.manage").click(function () {
		$.get(this.href,'',function(data){ popup(data); });
		return false;
	});
}

function prep_form(form,fn) {
	form.submit(function () {
		obj1 = this;
		$.get(this.action,$(this).formToArray(),function (data) { 
			obj = obj1;
			fn(data,obj1);
		});
		return false;
	});
}

function activate_upload() {
	if ($("head > link[src='/jquery.form.js']").length==0) {
		$("head").append('<'+'script type="text/javascript" src="/jquery.form.js" /'+'>');
	}

		var button = $('#button1'), interval;
	
		new AjaxUpload(button,{
	
			//action: 'upload-test.php', // I disabled uploads in this example for security reasons
	
			action: 'upload.htm', 
	
			name: 'myfile',
	
			onSubmit : function(file, ext){
	
				// change button text, when user selects file			
	
				button.text('Uploading');
	
				// If you want to allow uploading only 1 file at time,
	
				// you can disable upload button
	
				this.disable();
	
				
	
				// Uploding -> Uploading. -> Uploading...
				interval = window.setInterval(function(){
					var text = button.text();
					if (text.length < 13){
						button.text(text + '.');					
					} else {
						button.text('Uploading');				
					}
				}, 200);
			},
			onComplete: function(file, response){
				button.text('Upload');
							
				window.clearInterval(interval);
							
				// enable upload button
				this.enable();
				
				// add file to the list
				$('<li></li>').appendTo('#example1 .files').text(file);						
			}
		});
}

function ajaxFileUpload(action,field_name,fn)
{
	if ($("head > link[src='/ajaxfileupload.js']").length==0) {
		$("head").append('<script type="text/javascript" src="/ajaxfileupload.js" />');
	}
	
	//starting setting some animation when the ajax starts and completes
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
	
	/*
		prepareing ajax file upload
		url: the url of script file handling the uploaded files
                    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
		dataType: it support json, xml
		secureuri:use secure protocol
		success: call back function when the ajax complete
		error: callback function when the ajax failed
		
            */
	$.ajaxFileUpload
	(
		{
			url:action, 
			secureuri:false,
			fileElementId:'product_display',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
//						alert(data.msg);
						fn();
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e + "\n" + data);
			}
		}
	)
	
	return false;

}  
