// JavaScript Document

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var editor;

$(document).ready(function(){
	
	$('#searchbox').bind('click', function(){
		if ($(this).val() == "Search") {
			$(this).val('');
		}
	});
	
	$('#searchbox').bind('outerClick', function(){
		if ($(this).val() == "") {
			$(this).val('Search');
		}
	});
	
	$('.web-hover-links').bind('mouseover', function(){
		var menutext = $(this).attr("type");
		hover_menu_text(menutext);
	});
	
	$('.web-hover-links').bind('mouseout', function(){
		hover_menu_text_remove();
	});
});

function openKCFinder_singleFile() {
    window.KCFinder = {};
    window.KCFinder.callBack = function(url) {
        window.KCFinder = null;
        // Actions with url parameter here
    };
    //window.open('/system/plugins/kcfinder/browse.php', 'kcfinder_single');
	var left = (screen.width/2)-(800/2);
	var top = (screen.height/2)-(400/2);
	window.open ('/system/plugins/kcfinder/browse.php', 'kcfinder_single','toolbar=no, location=no, directories=no, status=nomenubar=0,resizable=1,width=800,height=400,top='+top+',left='+left); 
}
 
function openKCFinder_multipleFiles() {
    window.KCFinder = {};
    window.KCFinder.callBackMultiple = function(files) {
        window.KCFinder = null;
        for (var i; i < files.length; i++) {
            // Actions with files[i] here
        }
    };
    window.open('/system/plugins/kcfinder/browse.php', 'kcfinder_multiple');
}

function removeEditor(textareaid) {
	if ( !editor )
		return;
	// Retrieve the editor contents. In an Ajax application, this data would be
	// sent to the server or used in any other way.
	$(textareaid).val(editor.getData());
	// Destroy the editor.
	editor.destroy();
	editor = null;
}

function delete_admin_confirm(txtpath) {
	var answerdelete = confirm("Are you sure you want to delete this?");
	if (answerdelete) {
	   $.ajax({
		  url: txtpath,
		  success: function(data) {
			$('#petrus-admin-ajax-load').fadeIn(1000).html(data);
		  }
		});
	} else {
		alert("Delete cancel");
	}
	return false;
}

function load_admin_page(txtpath) {
	$('#petrus-admin-ajax-load').html('<div id="petrus-admin-ajax-load-loader"></div>');
	$('#petrus-admin-ajax-container').slideDown('slow');
	$.ajax({
	  url: txtpath,
	  success: function(data) {
		$('html, body').animate({scrollTop:0}, 'slow');
		$('#petrus-admin-ajax-load').hide();
		$('#petrus-admin-ajax-load').fadeIn(1000).html(data);
	  }
	});
}

function save_admin_page(txtpath,formname) {
	$.post(txtpath, 
	   $("#"+formname).serialize(),
		   function(data){
			   $('#petrus-admin-ajax-container').slideUp('slow');
		   }
	   );
}

function save_admin_page_image(txtpathfile, txtpath, formname, fileid) {
	$.post(txtpath, 
	   $("#"+formname).serialize(),
		   function(data){
			   $('#petrus-admin-ajax-container').slideUp('slow');
		   }
	   );
	
	$.ajaxFileUpload
	(
		{
			url:txtpathfile, 
			secureuri:false,
			fileElementId:fileid,
			dataType: 'script',
			success: function (data, status)
			{
			},
			error: function (data, status, e)
			{
			}
		}
	)
}
	
function save_page_module_file(txtpathfile,txtpath,formname,txtpathview,moduleid,fileid,path_id) {
	$.ajaxFileUpload
	(
		{
			url:txtpathfile, 
			secureuri:false,
			fileElementId:fileid,
			dataType: 'script',
			success: function (data, status)
			{
				$(path_id).val(data);
				$.post(txtpath,
				   $("#"+formname).serialize(),
					   function(data){
						   //$('#petrus-admin-ajax-container').slideUp('slow');
						   $('#petrus-admin-ajax-load').fadeIn(1000).html(data);
						   $.ajax({
							  url: txtpathview,
							  success: function(data) {
								//$('#petrus-module-'+moduleid).html(data);
							  }
							});
					   }
				   );
			},
			error: function (data, status, e)
			{
				$('#petrus-admin-ajax-load').html('Error!');
			}
		}
	)
}

function save_page_module(txtpath,formname,txtpathview,moduleid) {
	$.post(txtpath, 
	   $("#"+formname).serialize(),
		   function(data){
			   //$('#petrus-admin-ajax-container').slideUp('slow');
			   $('#petrus-admin-ajax-load').fadeIn(1000).html(data);
			   $.ajax({
				  url: txtpathview,
				  success: function(data) {
					//$('#petrus-module-'+moduleid).html(data);
				  }
				});
		   }
	   );
}

function save_page_module_close(txtpath,formname,txtpathview,moduleid) {
	$.post(txtpath, 
	   $("#"+formname).serialize(),
		   function(data){
			   //$('#petrus-admin-ajax-container').slideUp('slow');
			   $('#petrus-admin-ajax-container').slideUp('slow');
			   $.ajax({
				  url: txtpathview,
				  success: function(data) {
					//$('#petrus-module-'+moduleid).html(data);
				  }
				});
		   }
	   );
}

function save_admin_message(txtpath,formname) {
	$.post(txtpath, 
	   $("#"+formname).serialize(),
		   function(data){
			   $('#petrus-admin-ajax-load').fadeIn(1000).html(data);
		   }
	   );
}

function showmodulemenu (moduleid) {
	 //Following events are applied to the subnav itself (moving subnav up and down)  
	 /*var iconmenu = $('#petrus_module_menu_icon'+moduleid);
	 var position = iconmenu.position();
	 var offset = iconmenu.offset();
	 var margin = parseInt($('#petrus-module-container-'+moduleid).offset().left);

	 alert(margin);
	 $("ul#petrus_module_menu"+moduleid).css({"left": offset.left+"px", "top": (position.top + 16)+"px"});*/
	 $("ul#petrus_module_menu"+moduleid).fadeIn('slow').show(); //Drop down the subnav on click  
	
	 $("#petrus-module-downmenu"+moduleid).parent().hover(function() {  
	 }, function(){  
		 $("ul#petrus_module_menu"+moduleid).hide(); //When the mouse hovers out of the subnav, move it back up  
	 });  
}

function delete_confirm(txtpath) {
	var answerdelete = confirm("Are you sure you want to delete this?");
	if (answerdelete) {
		window.location = txtpath;
	} else {
		alert("Delete cancel");
	}
	return false;
}

function admin_delete_confirm(txtpath) {
	var answerdelete = confirm("Are you sure you want to delete this?");
	if (answerdelete) {
		$.ajax({
		  url: txtpath,
		  success: function(data) {
			$('#petrus-admin-ajax-load').hide();
			$('#petrus-admin-ajax-load').fadeIn(1000).html(data);
		  }
		});
	} else {
		alert("Delete cancel");
	}
	return false;
}

ImageList= new Array();
ImageListHover = new Array();

function load_image_template(ImageName, ImageNormal) {
	ImageList[ImageName] = new Image();
	ImageList[ImageName].src = ImageNormal;
}

function change_image_template() {
	skitemplate_id = $('#skintemplate_id').val();
	document.images['skin-template-pic'].src = ImageList[skitemplate_id].src;
}

function password_hashmd5(tbid) {
	(function($) { 
		txtpass = $(tbid).val();
		var s_txtpass = new String(txtpass);
		txtpass = s_txtpass.trim();
	})(jQuery);
	hashmd5 = hex_md5(txtpass);
	(function($) { 
		$(tbid).val(hashmd5);
	})(jQuery);
}

/* Petrus Menu */

function loadPetrusMenu() {
	$(".petrus-main-menu li").bind('mouseover', function(){ 
		$(this).find(".petrus-sub-menu-container").fadeIn();
	 });
	 
	$(".petrus-sub-menu-container").parent().hover(function() {  
	 }, function(){  
		 $(".petrus-sub-menu-container").hide(); 
	 });
}

/* Petrus Menu */

/* Petrus Comments */

function post_petrus_comments(id, module_id, txtpath, formname) {
	$('#petrus-comments-loadingspinner'+module_id).show();
	$.post(txtpath, 
	   $("#"+formname).serialize(),
		   function(data){
			   $('#petrus-comments-loadingspinner'+module_id).hide();
			   $('#petrus-comments-message'+module_id).html(data);
		   }
	   );
}

function delete_petrus_comments(comments_id, txtpath) {
	$('#petrus-comments-loadingspinner'+comments_id).show();
	var answerdelete = confirm("Are you sure you want to delete this?");
	if (answerdelete) {
		$.ajax({
		  url: txtpath+"/"+comments_id,
		  success: function(data) {
			$('#petrus-comments-loadingspinner'+comments_id).hide();
			$('#petrus-comments-'+comments_id).remove();
		  }
		});
	} else {
		$('#petrus-comments-loadingspinner'+comments_id).hide();
	}
}

function approve_petrus_comments(comments_id, txtpath) {
	$('#petrus-comments-loadingspinner'+comments_id).show();
	$.ajax({
	  url: txtpath+"/"+comments_id,
	  success: function(data) {
		$('#petrus-comments-loadingspinner'+comments_id).hide();
		$('#petrus-disable'+comments_id).show();
		$('#petrus-approve'+comments_id).hide();
	  }
	});
}

function disable_petrus_comments(comments_id, txtpath) {
	$('#petrus-comments-loadingspinner'+comments_id).show();
	$.ajax({
	  url: txtpath+"/"+comments_id,
	  success: function(data) {
		$('#petrus-comments-loadingspinner'+comments_id).hide();
		$('#petrus-disable'+comments_id).hide();
		$('#petrus-approve'+comments_id).show();
	  }
	});
}

var newspage_start = 0; 

function next_news(totalnews_page) {
	
	if (newspage_start < (totalnews_page - 1)) {
		newspage_start = newspage_start + 1; 
	}
	$('#sliderContainer').animate({
	  'marginLeft' : newssliderwidth*(-newspage_start)
	});
}

function prev_news() {
	if (newspage_start > 0) {
		newspage_start = newspage_start - 1; 
	}
	$('#sliderContainer').animate({
	  'marginLeft' : newssliderwidth*(-newspage_start)
	});
}

function goto_news(news_num, newssliderwidth) {
	$('#sliderContainer').animate({
	  'marginLeft' : newssliderwidth*(-news_num)
	});
}

function load_teaser_news(path, id) {
	$(id).html("<img src=\"/files/images/spinner.gif\">");
	$.ajax({
	  url: path,
	  success: function(data) {
		$(id).html(data).fadeIn();
	  }
	});
}

function redirect_url(urlpath,id) {
	window.location = urlpath+$('#'+id).val();
}

/* Hover Definition */

function hover_menu_text(menutext) {
	mxpos = 0;
	mypos = 0;
	text = "";
	text += "<div id=\"petrus-hover-menu-msg\" class=\"petrus-hover-menu-msg\">\n";
	text += "<ul>\n";
	text += "<li class=\"petrus-hover-menu-msg-pointer\"></li>\n";
	text += "<li class=\"petrus-hover-menu-msg-content\"><div id=\"petrus-hover-menu-msg-text\">"+menutext+"</div></li>\n";
	text += "</ul>\n";
	$("body").prepend(text);
	$(document).mousemove(function(e){
		mxpos = e.pageX;
		mypos = e.pageY;
		$("#petrus-hover-menu-msg").css("top",mypos+"px");
		$("#petrus-hover-menu-msg").css("left",(mxpos+20)+"px");
	}); 
}

function hover_menu_text_remove() {
	$("#petrus-hover-menu-msg").remove();
}

function next_news_teaser(totalnews_page) {
	if (newspage_start < (totalnews_page - 1)) {
		newspage_start = newspage_start + 1; 
	}
	$('#sliderContainerPhoto').animate({
	  'marginLeft' : newssliderwidth*(-newspage_start)
	});
}

function prev_news_teaser() {
	if (newspage_start > 0) {
		newspage_start = newspage_start - 1; 
	}
	$('#sliderContainerPhoto').animate({
	  'marginLeft' : newssliderwidth*(-newspage_start)
	});
}

