function initTooltips()
{
  $('.content-form-input img[title], .int-tooltip').tooltip({
	 track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    extraClass: "pretty", 
	    fixPNG: true, 
	    opacity: 0.9
	});
}

if($.datepicker != undefined){
		
	/* Polish initialisation for the jQuery UI date picker plugin. */
	/* Written by Jacek Wysocki (jacek.wysocki@gmail.com). */
	$.datepicker.regional['pl'] = {
	        closeText: 'Zamknij',
	        prevText: '&#x3c;Poprzedni',
	        nextText: 'Następny&#x3e;',
	        currentText: 'Dziś',
	        monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec',
	        'Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
	        monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze',
	        'Lip','Sie','Wrz','Pa','Lis','Gru'],
	        dayNames: ['Niedziela','Poniedziałek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
	        dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
	        dayNamesMin: ['N','Pn','Wt','Śr','Cz','Pt','So'],
	        weekHeader: 'Tydz',
	        dateFormat: 'yy-mm-dd',
	        firstDay: 1,
	        isRTL: false,
	        showMonthAfterYear: false,
	        yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['pl']);
}

$(document).ready(function() {
	
	$('.site-disabled').click(function(){
		
		var disabled = $(this).attr('lock');
		site_disabled(disabled);
		return false;
	});
	
	$('.intwork_admin_delete_link').click(function() {
		if (!confirm('Na pewno chcesz to trwale usunąć?')) {
			return false;
		}
	});

	$('.intwork_admin_confirm_link').click(function() {
		var msg = $(this).attr('rel');
		if (!confirm(msg)) {
			return false;
		}
	});
	
	$('.seo-form-container h2').click(function(event){
		$(this).find('a').click();
	});
	
	initTooltips();	    

	// datepicker
	
	if($.datepicker != undefined){
	$('.int-date-picker').datepicker({
			dateFormat: 'dd.mm.yy',
			onSelect: function(dateText, inst){
				date_obj = new Date();
				date_obj_hours = date_obj.getHours();
				date_obj_mins = date_obj.getMinutes();
	
				if (date_obj_mins < 10) { date_obj_mins = "0" + date_obj_mins; }
				if (date_obj_hours == 0) { date_obj_hours = "0" + date_obj_hours; }
	
				date_obj_time = date_obj_hours+":"+date_obj_mins;
		
				$(this).attr('value', dateText + ' ' + date_obj_time);
			}
	});
	}
	runEditors();
	
});


$.fn.scrollTo = function(speed)
{
	var element = $(this); 
	var position = element.position();
	if(position == null)
	{
		alert('ScrollTo: nie można odnaleźć elementu');
		return false;
	}
	var element_top = position.top;
	
	if(speed == undefined)
		speed = 'slow';
	
	var new_position = element_top > $(window).scrollTop() ? element_top - 50 : element_top - 40;
	
	if($(window).scrollTop() != new_position)
	{
		$('html, body').animate({scrollTop: new_position}, speed);
	};
};

$.fn.intDisable = function(add_css){	
	if(this.is_disabled){
		return false;
	}
	
	
	this.int_disable_obj = $('<div><div>');
	this.is_disabled = true;
	
	var position_top = this.position().top;
	var position_left = this.position().left;
	
	this.parents().each(function(){
		if($(this).css('position') == 'relative' || $(this).css('position') == 'absolute'){
			position_top += $(this).position().top;
			position_left += $(this).position().left;
		}			
	});
	
	css = {
			position: 'absolute',
			top: position_top + 'px',
			left: position_left + 'px',
			background: '#fff',
			width: this.outerWidth(true),
			height: this.outerHeight(true),
			zIndex: 7000,
			opacity: 0.7
	};
	
	$.extend(css, add_css);
	
	css.position = 'absolute';
	css.top = position_top + 'px';
	css.left = position_left + 'px';
	css.width = this.outerWidth(true);
	css.height = this.outerHeight(true);
	
	this.int_disable_obj.css(css);
	
	$('body').append(this.int_disable_obj);
};

$.fn.intEnable = function(){
	if(this.is_disabled){
		this.is_disabled = false;
		this.int_disable_obj.remove();
	}
};

var intEditors = {};

$.fn.intEditor = function(name, option, width, height, tiny_add_options, autorun){
	if(option == 'destroy'){
		if(typeof intEditors[name] == 'object'){
			//intEditors[name].tinymce().remove();
			intEditors[name].tinymce().destroy(false);
			delete intEditors[name];
			return true;
		}
	}
	else{
		intEditors[name] = {};
		intEditors[name].editor = this;
		intEditors[name].tiny_add_options = tiny_add_options;
		intEditors[name].width = width;
		intEditors[name].height = height;
	}
	
	if(autorun == true){
		runEditor(name);
	}
};

function runEditor(name){
	if(intEditors[name].executed == true){
		return true;
	}
	
	intEditors[name].editor.addClass('int-admin-wysiwyg-editor');
	//tinyMCE
	var height = intEditors[name].height;
	var width = intEditors[name].width;
	var tiny_options = {
		height: height,
		width: width,
  	// Location of TinyMCE script
  	script_url : IntLib.getRB() + 'js/tiny_mce/tiny_mce.js',
		language: 'pl',
  	// General options
  	theme : "advanced",
  	skin : "o2k7",
  	skin_variant : "silver",
  	plugins : "intfile,intgallery,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,advlist,intimage",
  	// Theme options
  	theme_advanced_buttons1 : "newdocument,|,cut,copy,paste,pastetext,pasteword,|,intgallery,intfile,intimage,template,link,unlink",
  	theme_advanced_buttons2 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons3 : "search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons4 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons5 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,pagebreak,restoredraft",
  	theme_advanced_toolbar_location : "top",
  	theme_advanced_toolbar_align : "left",
  	theme_advanced_statusbar_location : "bottom",
  	theme_advanced_resizing : true,
  	theme_advanced_resize_horizontal : false,
  	extended_valid_elements : "iframe[src|width|height|name|align],div[class|id|style]",	
  	template_external_list_url : IntLib.getRB() + 'admin.php/mceTemplates/getTemplatesList',
  	font_size_style_values : "8pt",



  	//theme_advanced_disable: "image,advimage",

//  	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",
  	document_base_url : IntLib.getRB(),
  	convert_urls : false,
  	content_css: IntLib.getRB() + 'css/style.css',
  	forced_root_block: false,
  	
  	
  	save_onsavecallback: function(){
  		$('#' + IntDynamicContent.options.buttons.apply).click();
  		return false;
  	},
  	
  	onchange_callback: function(){
  		if(IntDynamicContent){
  			IntDynamicContent.changes_not_saved = true;
  		}
  	}
  };
	$.extend(tiny_options, intEditors[name].tiny_add_options);
	intEditors[name].editor.tinymce(tiny_options);
	intEditors[name].executed = true;
}

function runEditors(){
	for(name in intEditors){
		runEditor(name);
	}
}

var IntLib = function(){};

	function site_disabled(disabled){
		if(disabled == "1"){
			var text = 'Czy na pewno chcesz zablokować stronę';
			var title = 'Blokada strony';
		}else{
			var text = 'Czy na pewno chcesz odblokować stronę';
			var title = 'Odblokowanie strony';
		}
		var confirm = new IntNotify({
		type: 'confirm',
		title: title,
		text: text,

			confirm_ok: function()
			{
				$(this).IntNotify.get().close();
				
				$.ajax({
					url:  IntLib.getRB() + 'admin.php/panel/lock',
					type: 'post',
					data: {disabled: disabled},
					async: false,
					dataType: 'json',
					success: function(data)
					{
						location.href = location.href; 
					},
					
					error: function(response)
					{
						alert(response.responseText);
					}
			
				});
			}
		});
	}
	
	
IntLib.addslashes = function(it) {
	if(typeof it == 'string'){
		str = it;
		str=str.replace(/\\/g,'\\\\');
		str=str.replace(/\'/g,'\\\'');
		str=str.replace(/\"/g,'\\"');
		str=str.replace(/\0/g,'\\0');
		return str;
	}
	
	if(typeof it == 'object'){
		for(i in it){
			it[i] = IntLib.addslashes(it[i]);
		}
	}
	
	return it;
};

IntLib.stripslashes = function(it) {
	if(typeof it == 'string'){
		str = it;
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\0/g,'\0');
		str=str.replace(/\\\\/g,'\\');
		return str;
	}
	
	if(typeof it == 'object'){
		for(i in it){
			it[i] = IntLib.stripslashes(it[i]);
		}
	}
	
	return it;
};	

IntLib.parseJSON = function(str){
	//str = ;

	return $.parseJSON(IntLib.fixQuotes(str));
};

IntLib.toJSON = function(obj){
	return $.toJSON(IntLib.fixQuotes(obj));
};

IntLib.fixQuotes = function (obj){
	return obj;
	if(typeof obj == 'object'){
		for(i in obj){
			if(typeof obj[i] == 'string')
				obj[i] = IntLib.fixQuotes(obj[i]);
		}
	}
	else if(typeof obj == 'string'){
		return obj.replace("'", "");
	}
	
	return obj;
}
	
/**
 * REWRITE BASE - ZMIENIĆ
 */
IntLib.getRB = function()
{
	return '/';
};


