jQuery.dialog = {
	open: function (message, callback, custom_options) {
			jQuery(document.body).append('<div id="jquery_dialog" title="InterAdmin" style="display:none"><div id="jquery_dialog_txt"></div></div>');
			// Exemplo de mudança do Title $("#jquery_dialog").attr( 'title', "New Title" );
			jQuery.dialog.callback = callback;
			jQuery.dialog.val = null;
			message = message.replace('\r\n', '<br />');
			message = message.replace('\n', '<br />');
			jQuery("#jquery_dialog_txt").html(message);
			jQuery("#jquery_dialog").show();
			dialog_options = {
				modal: true,
				minHeight: 85,
				resizable: false,
				draggable: false,
				close: function() {
					if (jQuery.isFunction(jQuery.dialog.callback)) setTimeout("jQuery.dialog.callback(" + jQuery.dialog.val + ")", 10);
					jQuery(this).dialog("destroy");
					jQuery("#jquery_dialog").remove();
				},
				buttons: {
					"OK": function() {
						jQuery(this).dialog("close");
					}
				}
			}
			
			for(key in custom_options) dialog_options[key] = custom_options[key];
			jQuery("#jquery_dialog").dialog(dialog_options);
			
			frame = jQuery("#jquery_dialog").parent().parent();
			jQuery("div.ui-dialog-buttonpane button:contains('OK')", frame).addClass("ok");
			jQuery("div.ui-dialog-buttonpane button:contains('Cancel')", frame).addClass("cancel");
			jQuery("#jquery_dialog").keypress(function (e) {
				if (e.which == 13) jQuery("div.ui-dialog-buttonpane button.ok").click();
		    });
		},
	callback: null,
	val: null
}

jQuery.alert = function (message, custom_options) {
	jQuery.dialog.open(message, null, custom_options);
	jQuery("#jquery_dialog_txt").before('<img src="/_default/img/dialog/alert.gif" alt="" align="left" style="margin-right:10px"/>');
	jQuery("div.ui-dialog-buttonpane button.ok").focus();
}

jQuery.confirm = function (message, callback) {
	jQuery.dialog.open(message, callback, {
		buttons: {
			"OK": function() {
				jQuery.dialog.val = true;
				jQuery(this).dialog("close");
			},
			"Cancel": function() {
				jQuery.dialog.val = false;
				jQuery(this).dialog("close");
			}
		}
	});
	jQuery("div.ui-dialog-buttonpane button.ok").focus();
}

jQuery.prompt = function (message, default_val, callback) {
	jQuery.dialog.open(message, callback, {
		buttons: {
			"OK": function() {
				jQuery.dialog.val = "'" + jQuery("#jquery_dialog_input input").val() + "'";
				jQuery(this).dialog("close");
			},
			"Cancel": function() {
				jQuery(this).dialog("close");
			}
		}
	});
	jQuery("#jquery_dialog").append('<div id="jquery_dialog_input"><input type="text" value="' + default_val + '"/></div>');
	jQuery("#jquery_dialog_input input").focus();
}


/**
 * Copyright (c) 2008 Paulo Rodrigues (fox.com.br)
 * Colaborattor: João Pedro Barbosa - Rewritten by Carlos Rodrigues using objects  
 * Version: 1.0.5
 * Release: 2008-08-15 
 */
jQuery.mediabox = function(boxes, controllers){
	this.boxes = boxes;
	this.controllers = controllers;
	this.mediaboxId = jQuery.mediabox.instances.length;
	jQuery.mediabox.instances[this.mediaboxId] = this;
};
jQuery.mediabox.instances = []; 
jQuery.mediabox.prototype = {
	/**
	 * array Array of objects (divs) with the boxes
	 */
	boxes: null,
	/**
	 * array Array of objects (divs) with the controllers
	 */
	controllers:  null,
	/**
	 * int Interval between each rotation
	 */
	interval: 15000,
	/**
	 * bool If FALSE the Next and Previous buttons won´t change the current item if its the last or the first.
	 */
	looping: true,
	/**
	 * int Index of the current item
	 */
	selected: 0,
	change: function(number, fromrotate) {
		this.selected = number;
		this.boxes.hide();
		jQuery(this.boxes[number - 1]).fadeIn();
		this.controllers.removeClass("pointer_selected");
		jQuery(this.controllers[number - 1]).addClass("pointer_selected");
		if (this.timeout && !fromrotate) {
			this.stop();
			this.rotate(true);
		}
	},
	rotate: function(nolooping){
		if (!nolooping) this.next(true);
		this.timeout = setTimeout('jQuery.mediabox.instances[' + this.mediaboxId + '].rotate()', this.interval);
	},
	stop: function(){
		this.timeout = clearTimeout(this.timeout);
	},
	next: function(fromrotate) {
		number = this.selected + 1;
		if (number > this.boxes.length) { 
			if (this.looping) number = 1;
			else return false;
		}
		this.change(number, fromrotate);	
	},
	previous: function() {
		number = this.selected - 1;
		if (number  < 1) { 
			if (this.looping) number = this.boxes.length;
			else return false;
		}
		this.change(number);	
	}
}

/*
 * Box de Fotos
 */
function fox_PhotoAlbum(container) {
	this.container = container;
	this.time = 4500;
	this.efeitotime = 10;
	this.atual = 0;
	this.proximo = 1;
	this.timeout;
	this.paused = false;
	this.paused_time = 0;
	this.slideThumbs_animation = false;
	this.id = fox_PhotoAlbum.instances.length;
	this.thumbsArea = 3;
	this.limit_esquerda = 0;
	this.limit_direita = 0;
	fox_PhotoAlbum.instances[this.id] = this;
	
	this.getTableLeft = function() {
		var left = this.table.css('left');
		left = parseInt(left, 10);
		if (isNaN(left)) {
			left = 0;
		}
		return left;
	};
}

fox_PhotoAlbum.instances = [];
fox_PhotoAlbum.prototype.init = function() {
	thumbs = $('table.thumbs td', this.container);
	x = thumbs.eq(0);
	var thumb_vazio = '<img src="/_default/img/px.gif" style="background-color:#efefef" />';
	if (thumbs.length < this.thumbsArea) {
		last = thumbs.eq(thumbs.length - 1);
		for (var i = 0; i < Math.floor((this.thumbsArea - thumbs.length) / 2); i++) {
			x.clone(true).insertBefore(x).addClass('disabled').html(thumb_vazio);
		}
		for (var i = 0; i < Math.ceil((this.thumbsArea - thumbs.length) / 2); i++) {
			last.clone(true).insertAfter(last).addClass('disabled').html(thumb_vazio);
		}
		var slotsDisponiveis = thumbs.length;
	} else {
		var slotsDisponiveis = this.thumbsArea;
	}
	for (var i = 1; i <= Math.floor(slotsDisponiveis / 2); i++) {
		x = thumbs.eq(thumbs.length - i).insertBefore(x);
		this.limit_esquerda = thumbs.length - i;
	}
	this.limit_direita = this.limit_esquerda - 1;
	this.table = $('table.thumbs', this.container);
	this.thumb_width = thumbs.eq(0).width();
	this.thumbs_i = thumbs.length;
	this.timer(0);
}
	
fox_PhotoAlbum.prototype.action = function(acao) {
	if (acao == this.atual || this.slideThumbs_animation || this.thumbs_i < 2) {
		return;
	}
	clearTimeout(this.timeout);
	
	switch (acao) {
		case 'pause':
			if (this.paused) {
				this.paused = false;
				this.timer(this.paused_time);
			} else {
				this.paused = true;
			}
			return;
		case 'back':
			this.slideThumbs_animation = true;
			this.proximo = this.atual - 1;
			break;
		default:
			this.slideThumbs_animation = true;
			if (isNaN(acao)) {
				this.proximo = this.atual + 1;
			} else {
				this.proximo = acao;
			}
	}
	var images = $('ul.images li', this.container);
	var thumbs = $('ul.thumbs img', this.container);
	
	if (this.proximo >= images.length) {
		this.proximo = 0;
	} else if (this.proximo < 0) {
		this.proximo = images.length - 1;
	}
		
	//alert(this.atual +'-'+ proximo)
	thumbs.eq(this.atual).removeClass('on');
	thumbs.eq(this.proximo).addClass('on');
	images.eq(this.atual).css('z-index', 1);
	images.eq(this.proximo).css('z-index', 0);
	images.eq(this.proximo).show();
	//images.eq(this.atual)[0].boxfotos = this;
	
	this.fadeOutCallback = function() {
		this.anterior = this.atual;
		this.atual = this.proximo;
		if (!this.paused) {
			this.timer(0);
		}
		this.slideThumbs(this.atual, true, 5000);
	};
	
	images.eq(this.atual)[0].obj = this;
	images.eq(this.atual).fadeOut('slow', function() {
		this.obj.fadeOutCallback();
	});
}

fox_PhotoAlbum.prototype.timer = function(time) {
	if (time <= this.time) {
		this.paused_time = time;
		$('#home-mediabox-counter-ok').width((time / this.time) * 100 + '%');
		this.timeout = setTimeout('fox_PhotoAlbum.instances['+this.id+'].timer(' + (time + 100) + ')', 100);
	} else {
		this.action();
	}
}

fox_PhotoAlbum.prototype.slideThumbs = function(dir, slide, time) {
	if (slide) {
		dif_1 = this.atual - this.anterior;
	} else {
		if (!this.slideThumbs_animation) {
			dif_1 = (dir == 'back') ? -1 : 1;
			this.action(this.atual + dif_1);
		}
		return false;
	}
	
	if (dif_1 < 0) {
		dif_1 = this.thumbs_i + dif_1;
	}
	if (dif_1 > this.thumbs_i / 2) {
		dif_1 = dif_1 - this.thumbs_i;
	}
	
	// Necessário quando quantidade é menor
	if (this.thumbs_i < this.thumbsArea) {
		if (this.anterior == this.limit_esquerda && this.atual == this.limit_direita && this.atual > 0) {
			dif_1 += this.thumbs_i;
		} else if (this.anterior == this.limit_direita && this.atual == this.limit_esquerda) {
			dif_1 -= this.thumbs_i;
		}
	}
	
	this.animationCallback = function() {
		this.slideThumbs_animation = false;
		for (var i = 0; i < this.clonecells.length; i++) {
			if (dif_1 < 0) {
				thumbs.eq(thumbs.length - (i + 1)).remove();
			} else {
				thumbs.eq(i).remove();
			}
		}
		this.table.css('left', 0)
	}
	
	thumbs = $('table.thumbs td');
	this.clonecells = new Array();
	
	var new_left;
	
	if (dif_1 < 0) {
		for (var i = 0; i < (dif_1 * -1); i++) {
			this.clonecells[i] = thumbs.eq(thumbs.length-(1 + i)).clone(true).insertBefore(thumbs.eq(0));
			thumbs = $('table.thumbs td');
			this.table.css('left', this.getTableLeft() - this.thumb_width)
		}
		new_left = 0
	} else {
		for (var i = 0; i < dif_1; i++) {
			this.clonecells[i] = thumbs.eq(i).clone(true).insertAfter(thumbs.eq(thumbs.length-1));
			thumbs = $('table.thumbs td');
		}
		new_left = (this.getTableLeft() + (this.thumb_width * -1)) * dif_1;
	}
	
	this.table[0].obj = this;
	
	$('table.thumbs', this.container).animate(
		{left: new_left + 'px'},
		function() {
			this.obj.animationCallback()
		}
	);
}

// Novo MediaBox - Usado na fox e Lumini
jQuery.slideshow = function(selector) {
	this.container = $(selector);
	this.duration = 6000;
	this.effectDuration = 10;
	this.currentIndex = 0;
	this.nextIndex = 1;
	this.timeout;
	this.paused = false;
	this.currentDuration = 0;
	this.onTransition = false;
	
	this.init = function() {
		this.timer(0);
	}
	
	this.pause = function () {
		clearTimeout(this.timeout);
		if (this.paused) {
			this.paused = false;
			this.timer(this.currentDuration);
			this.container.find('.slideshow-pause').removeClass('play').addClass('pause');
		} else {
			this.paused = true;
			this.container.find('.slideshow-pause').removeClass('pause').addClass('play');
		}
	}
	
	this.previous = function () {
		this.goTo(this.currentIndex - 1);
	}
	
	this.next = function () {
		this.goTo(this.currentIndex + 1);
	}
		
	this.goTo = function(index) {
		if (index == this.currentIndex || this.onTransition) {
			return;
		}
		clearTimeout(this.timeout);
		
		var items = this.container.find('.slideshow-item');
		var icons = this.container.find('.slideshow-icon');
		
		if (items.length == 1) {
			clearTimeout(this.timeout);
			return;
		}
		
		this.nextIndex = index;
		
		if (this.nextIndex >= items.length) {
			this.nextIndex = 0;
		} else if (this.nextIndex < 0) {
			this.nextIndex = items.length - 1;
		}
			
		icons.eq(this.currentIndex).removeClass('active');
		icons.eq(this.nextIndex).addClass('active');
		items.eq(this.currentIndex).css('z-index', 1);
		items.eq(this.nextIndex).css('z-index', 0);
		items.eq(this.nextIndex).show();
		this.onTransition = true;
		
		var instance = this;
		items.eq(this.currentIndex).fadeOut('slow', function() {
			instance.currentIndex = instance.nextIndex;
			instance.onTransition = false;
			if (!instance.paused) {
				instance.timer(0);
			}
		});
	}
	
	this.timer = function(time) {
		if (time <= this.duration) {
			this.currentDuration = time;
			this.container.find('.slideshow-progress').width((time / this.duration) * 100 + '%');
			
			var instance = this;
			this.timeout = window.setTimeout(function() {
				instance.timer(time + 100);
			}, 100);
		} else {
			this.next();
		}
	}
}

