JobitorSlideShow = function(params){
	function param_default(pname, def) {
		if (typeof params[pname] == "undefined")
		{
			params[pname] = def;
		}
		return params[pname];
	};
	this.leftArrow 				= param_default("leftArrow", "");
	this.rightArrow 			= param_default("rightArrow", "");
	this.slideshowContainer 	= param_default("slideshowContainer", "");
	this.slideshowTitle 		= param_default("slideshowTitle", "");
	this.enableClick 			= true;


	var self = this;
	// Initial
	this.init = function(){
		$("#" + this.slideshowContainer + "> div").each(function (i) {
			if(i == 0){
				this.style.display = "block";
				if (document.getElementById(self.slideshowTitle))
					document.getElementById(self.slideshowTitle).innerHTML = $(this).attr("jtitle");
			}else{
				this.style.display = "none";
			}
		});
		setInterval( function(){
			var this_self = self;

		}, 5000 );
	};

	// Next image
	this.next = function(){
		var point = null;
		var current_elem = null;
		$("#" + this.slideshowContainer + "> div").each(function (i) {
			if(this.style.display == "block" || this.style.display == ""){
				point = i;
				current_elem = this;
			}
		});
		if($("#" + this.slideshowContainer + "> div").length == point + 1){
			// Clear data
			this.hideAllData();

			$("#" + this.slideshowContainer + "> div:first").fadeIn("slow", function() {
				var this_self = self;
				this_self.enableClick = true;

            	$("#" + this.slideshowContainer + "> div:first").css("display", "block");
        	});

        	this.changeTitle($("#" + this.slideshowContainer + "> div:first"));
		}else{
			// Clear data
			this.hideAllData();

			$(current_elem).next().fadeIn("slow", function() {
				var this_self = self;
				this_self.enableClick = true;

            	$(current_elem).next().css("display", "block");
        	});

        	this.changeTitle($(current_elem).next());
		}
	}

	// Prev
	this.prev = function(){
		var point = null;
		var current_elem = null;
		$("#" + this.slideshowContainer + "> div").each(function (i) {
			if(this.style.display == "block" || this.style.display == ""){
				point = i;
				current_elem = this;
			}
		});
		if(point == 0){
			// Clear data
			this.hideAllData();

			$("#" + this.slideshowContainer + "> div:last").fadeIn("slow", function() {
				var this_self = self;
				this_self.enableClick = true;

            	$("#" + this.slideshowContainer + "> div:last").css("display", "block");
        	});

        	this.changeTitle($("#" + this.slideshowContainer + "> div:last"));
		}else{
			// Clear data
			this.hideAllData();

			$(current_elem).prev().fadeIn("slow", function() {
				var this_self = self;
				this_self.enableClick = true;

            	$(current_elem).prev().css("display", "block");
        	});

        	this.changeTitle($(current_elem).prev());
		}
	}

	this.changeTitle = function(elem_obj){
		if( this.slideshowTitle != "" && document.getElementById(this.slideshowTitle) ){
			document.getElementById(this.slideshowTitle).innerHTML = elem_obj.attr("jtitle");
		}
	}

	// Hide all data
	this.hideAllData = function(){
		$("#" + this.slideshowContainer + "> div").each(function (i){
			this.style.display = "none";
		});
	}

	// Event left
	if(this.leftArrow != "" && document.getElementById(this.leftArrow)){
		document.getElementById(this.leftArrow).onclick = function(){
			var this_self = self;
			if(this_self.enableClick){
				this_self.prev();
				this_self.enableClick = false;
			}
			return false;
		}
	}
	// Event right
	if(this.rightArrow && document.getElementById(this.rightArrow)){
		document.getElementById(this.rightArrow).onclick = function(){
			var this_self = self;
			if(this_self.enableClick){
				this_self.next();
				this_self.enableClick = false;
			}
			return false;
		}
	}

	this.init();
};
jQuery(document).ready(function($){
	window.slideShowData = new JobitorSlideShow({
		leftArrow: 			"slideshow_left_arrow",
		rightArrow: 		"slideshow_right_arrow",
		slideshowContainer: "slideshow_container",
		slideshowTitle: 	"slideshow_title"
	});

	// Popup windows
	$("#title_face_popup_window").dialog(
	    {
	        buttons: function(){
				var initPopupWin = new Object();
				initPopupWin[gettext("Отмена")] = function() {
	        		$(this).dialog("close");
	        	}
				return initPopupWin;
			}(),
	        modal: true,
	        autoOpen: false,
	        bgiframe: true,
	        width: 250,
	        resizable: false,
	        dialogClass: 'JobitorPopupWindow'
	    }
	);
});