Event.observe(window,"load",function(){
	
	if(typeof jQuery != "undefined")jQuery.noConflict();
	
	//Create entry edit button
	$$(".cms_hidden_entry_id").each(function(element){
		element.onclick = function(){
			var entryId = this.getAttribute("entryid");
			var blockLabelId = this.getAttribute("blocklabelid");
			console.log(entryId);
			if(entryId != null && entryId > 0){
				common_to_layer(EntryEditPage+"/"+entryId,{width:800,height:600});
			}else{
				common_to_layer(EntryEditPage+"/addlabeldentry_"+blockLabelId,{width:800,height:600});
			}
		}
	});
	
	common_element_to_layer($("soy_cms_operation"),{
		targetId:'soy_cms_operation_frame',
		width:300,
		height:230,
		onclose:function(){
			return false;	
		},
		disableClose : true
	});
			
	$("soy_cms_operation").style.width = "100%";
	$("soy_cms_operation").style.height = "100%";
	$("soy_cms_operation").show();
	
	//Toggle entry edit button
	$("soy_cms_operation_toggle_edit_entry_button").onclick = function(){
		if(!this.checked){
			$$(".cms_hidden_entry_id").each(function(element){
			    element.hide();
		  	});
		}else{
			$$(".cms_hidden_entry_id").each(function(element){
			    element.show();
		  	});
		}
	}
	
	//Toggle display draft entry or not
	$("soy_cms_operation_toggle_show_entry_button").onclick = function(){
		
		var search = location.search.toQueryParams();
		
		if(this.checked){
			search.show_all = 1;
		}else{
			search.show_all = 0;
		}
		
		window.location.search = "?" + $H(search).toQueryString();
	};
	
	if($("soy_cms_operation_toggle_show_entry_button").checked){
		$$("a").each(function(ele){
			var tmp = ele.innerHTML;//for IE
			if(ele.href.indexOf("?") != -1){
				ele.href += "&show_all=1";
			}else{
				ele.href += "?show_all=1";
			}
			ele.innerHTML = tmp;//for IE
		});
	}
	
	//CSS
	var temp = new Image();

	temp.src = "/";
	var serverRoot = temp.src;

	temp.src = layerCSS;
	layerCSS = temp.src;

	var cssCount = 0;
	$$("link").each(function(element){
		if(!element.getAttribute("rel") || element.getAttribute("rel").toLowerCase() != "stylesheet"){
			return;
		}
		var image = new Image;
		image.src = element.getAttribute("href");
		var href = image.src;
		
		if(href && href != layerCSS){
			var option = document.createElement('option');
			option.value = href;
			option.innerHTML = (href.indexOf(serverRoot) == 0) ? "/" + href.replace(serverRoot,"") : href;
			$("soy_cms_operation_edit_css_select").appendChild(option);
			cssCount ++;
		}
	});
	if(cssCount != 0){
		$("soy_cms_operation_edit_css_form_wrapper").show();
		$("soy_cms_operation_edit_css_form").action = cssEditAddress;		
		$("soy_cms_operation_edit_css_form").onsubmit = function(){
			return common_submit_to_layer(this,{width:800,height:600});
		}
		
	}
	
	//Template edit button
	if($("soy_cms_operation_edit_template_form")){
		$("soy_cms_operation_edit_template_form").action = templateEditAddress;
		$("soy_cms_operation_edit_template_form").onsubmit = function(){
			return common_submit_to_layer(this,{width:800,height:600,onclose:function(){location.reload();}});
		};
	}	
});