var baseUrl = "";

$(document).ready(function () {
	$.fn.wait = function(time, type) {
	    time = time || 1000;
	    type = type || "fx";
	    return this.queue(type, function() {
	        var self = this;
	        setTimeout(function() {
	            $(self).dequeue();
	        }, time);
	    });
	};
	jQuery.fn.exists = function(){	
		if($(this).length > 0) {
			return $(this);
		}
		else return null;
	};
	allFunctions();
});

function allFunctions() {
	jquery();
	ajax();
	scrollTo();
	reflection();
	bubble();
	dialog();
	tabs();
	accordion();
	sortable();
	copyPaste();
	confirm_();
	textEditor();
	dialog_popup();
}

function jquery() {
	$("#loading").ajaxStart(function() {
	    $(this).show();
	}).ajaxStop(function() {
	    $(this).hide();
	});
	$(".button").button();
	$(".hide_slow").wait(5000).hide(1000);
	
	var top_settings = {
	          tl: { radius: 5 },
	          tr: { radius: 5 },
	          bl: { radius: 0 },
	          br: { radius: 0 },
	          antiAlias: true,
	          validTags: ["div"]
	      };
	
	var bottom_settings = {
	          tl: { radius: 0 },
	          tr: { radius: 0 },
	          bl: { radius: 5 },
	          br: { radius: 5 },
	          antiAlias: true,
	          validTags: ["div"]
	      };

	$('.rounded_top').corner(top_settings);
	$('.rounded_bottom').corner(bottom_settings);

	$("img.slideshow").imgr({
		radius: "5px 0px 0px 0px"
	});
	$(".datepicker").datepicker({
		dateFormat: 'yy-mm-dd', 
		showAnim: 'fold', 
		altFormat: 'yy-mm-dd'
	});
}

function bubble() {
	if($(".bubble").exists()) {
		$(".bubble").each(function() {
			$(this).CreateBubblePopup({ 
				innerHtml: $(this).attr("btitle"),
				themeName: 	'all-grey',
				themePath: 	baseUrl + '/styles/jquerybubblepopup-theme'
			});
		});
	}	
}

function ajax() {
	$(".ajax").live("click", function(e) {
        if($('.editor').length != 0) {
            tinyMCE.get($('.editor').attr("id")).remove();
            $('.editor').remove();
        }
		var this_ = $(this);
		if($(this).hasClass("news")) {
			$(".background_news_").removeClass("background_news_");
			$(this_).addClass("background_news_");
		}
		var url = $(this).attr("href");
		var content = $(this).attr("hreflang");
		$(content).load(url, $(content).find(":input").serializeArray(), function(data) {
			$(".editor").each(function(index) {
				tinyMCE.execCommand("mceAddControl", false, $(this).attr("id"));
			});
			jquery();
			tabs();
		}).css({ opacity: 0 }).fadeTo(1000, 1);
		$(content).empty().html('<div class="width_100 text_align_center"><img class="margin_top_50 margin_bottom_50" src="'+baseUrl+'/images/ajax-loader.gif" /></div>');
		e.preventDefault();
	});
}

function scrollTo() {
	$(".scroll").bind("click", function() {
		var scroll = $(this).attr("name");
		$("html,body").animate({ scrollTop: $(scroll).offset().top }, {duration: 'slow', easing: 'swing'});
	});
}

function reflection() {
	$(".nav-reflection a").hover(function() {
	    $(this).stop().css({position: "relative"}).animate({ top: "-10px" }, 200);
	    $(this).parent().find("span").stop().css({position: "relative"}).animate({ top: "18px", opacity: 0.25 }, 200);
	},function(){
	    $(this).stop().css({position: "relative"}).animate({ top: "0px" }, 300);
	    $(this).parent().find("span").stop().css({position: "relative"}).animate({ top: "1px", opacity: 1 }, 300);
	});	
}

function dialog() {
	$(".dialog").live("click", function(e) {
		content = $(this).attr("content");
		url = $(this).attr("href");
		title = $(this).attr("title");
		var theUrl = window.location.pathname;
		var dialog = $(content).dialog({
			width: 600,
			title: title,
			modal: true,
			resizable: false,
			position: ["center",20],
			open: function(event, ui) { 
				jQuery(".ui-dialog-titlebar-close").hide();
			    $(this).parent().find('.ui-dialog-buttonpane button:contains("Ok")').button({
			        icons: { primary: 'ui-icon-circle-check' }
			    });
			    $(this).parent().find('.ui-dialog-buttonpane button:contains("Cancell")').button({
			        icons: { primary: 'ui-icon-circle-close' }
			    });
			    $(this).parent().find('.ui-dialog-buttonpane button:contains("Ok") > .ui-button-text').text(button_ok);
			    $(this).parent().find('.ui-dialog-buttonpane button:contains("Cancell") > .ui-button-text').text(button_cancel);
			},
			buttons: {
				"Ok": function(ui) {
					$(this).load(url, $(content + " form").find(":input").serializeArray(), function(data) {
						if($(data).hasClass('success')) {
							$(":button:contains('Elküldöm')").hide();
						}
						if(data.trim() == "ok") {
							$(this).dialog("close");
							$(this).html(data);
							location.href = theUrl;
						}
					}).dialog("open");
				},
				"Cancell": function() {
					$(this).dialog("close");
					$(this).html("");
				}
			}
		}).load(url + content);
		e.preventDefault();
	});
}

function dialog_popup() {
	$(".dialog_popup").each(function() {
		content = $(this).attr("content");
		title = $(this).attr("title");
		var dialog = $(this).dialog({
			width: 900,
			title: title,
			modal: true,
			resizable: false,
			position: ["center",20],
			open: function(event, ui) { 
				jQuery(".ui-dialog-titlebar-close").hide();
				jQuery(".ui-dialog-titlebar").hide();  
			    $(this).parent().find('.ui-dialog-buttonpane button:contains("Cancell")').button({
			        icons: { primary: 'ui-icon-circle-close' }
			    });
			    $(this).parent().find('.ui-dialog-buttonpane button:contains("Cancell") > .ui-button-text').text(button_more);
			},
			buttons: {
				"Cancell": function() {
					$(this).dialog("close");
					$(this).html("");
				}
			}
		});
	});
}

function tabs() {
	$(".tabs").tabs({
		selected: selected,
		load: function(event, ui) {
			jquery();
			$(".editor").each(function(index) {
				tinyMCE.execCommand("mceAddControl", false, $(this).attr("id"));
			});
		},
		select: function(event, ui) {
	        if($('.editor').length != 0) {
	            tinyMCE.get($('.editor').attr("id")).remove();
	            $('.editor').remove();
            }
		}
	});	
}

function accordion() {
	var icons = {
		header: "ui-icon-circle-arrow-e",
		headerSelected: "ui-icon-circle-arrow-s"
	};
	var accordion = $(".accordion").accordion({
		collapsible: true,
		autoHeight: false,
		clearStyle: true,
		active: false
	});
	$(".accordion-noicons").each(function(){
		accordion.accordion({
			icons: false
		});
	});
	$(".accordion-active").each(function(){
		accordion.accordion({
			active: 0
		});
	});
}

function sortable() {
	$("#sortable").each(function(){
	    $(this).sortable({
	        placeholder: "ui-state-highlight",
	        update : function () {
	  		var order = $("#sortable").sortable("serialize");
	  		$.ajax({
	  			url: $(this).attr("href"),
	  			type: "POST",
	  			data: order,
	  			success: function(data, textStatus, XMLHttpRequest) {
//	  				$("#success").html("Sikeres sorrendváltozás");
	  			}
	  		});
	        }
	      });
	});
}

function confirm_() {
	$(".confirm").click(function() {
		return confirm($(this).attr("title"));
	});	
}

function copyPaste() {
	$(".copy").bind("keyup", function(e) {
		$($(this).attr("to")).val(MinusEkezet($(this).attr("value")));
	});
}

function textEditor() {
	if($(".editor").exists()) {
		tinyMCE.init({
			// General options
			mode : "textareas",
			theme : "advanced",
			width : "100%",
			height : "450",
			plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",
			convert_urls:true,
			relative_urls:false,
			remove_script_host:false,
			
			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Example content CSS (should be your site CSS)
			content_css : "css/content.css",

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",

			// Style formats
			style_formats : [
				{title : 'Bold text', inline : 'b'},
				{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
				{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
				{title : 'Example 1', inline : 'span', classes : 'example1'},
				{title : 'Example 2', inline : 'span', classes : 'example2'},
				{title : 'Table styles'},
				{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
			],

			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
		});
	}
}

function MinusEkezet(text) {
	text = text.replace(/á/g, 'a');
	text = text.replace(/é/g, 'e');
	text = text.replace(/í/g, 'i');
	text = text.replace(/ó/g, 'o');
	text = text.replace(/ö/g, 'o');
	text = text.replace(/ő/g, 'o');
	text = text.replace(/ú/g, 'u');
	text = text.replace(/ü/g, 'u');
	text = text.replace(/ű/g, 'u');
	text = text.replace(/Á/g, 'A');
	text = text.replace(/É/g, 'E');
	text = text.replace(/Í/g, 'I');
	text = text.replace(/Ó/g, 'O');
	text = text.replace(/Ö/g, 'O');
	text = text.replace(/Ő/g, 'O');
	text = text.replace(/Ú/g, 'U');
	text = text.replace(/Ü/g, 'U');
	text = text.replace(/Ű/g, 'U');
	text = text.replace(/\//g, '-');
	text = text.replace(/ /g, '-');
	text = text.replace(/,/g, '');
	text = text.replace(/!/g, '');
	text = text.toLowerCase();
	return text;
}

