/* SCROLLING */


/* begin http://www.brainjar.com/dhtml/drag/ */
var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  /* If an element id was given, find it. Otherwise use the element being
     clicked on. */

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if ($.browser.msie)
      dragObj.elNode = window.event.srcElement;
    else
      dragObj.elNode = event.target;

    /* If this is a text node, use its parent element. */

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  /* Get cursor position with respect to the page. */

  if ($.browser.msie) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  } else {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  /* Save starting positions of cursor and element. */

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  /* Update element's z-index. */

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  /* Capture mousemove and mouseup events on the page. */

  if ($.browser.msie) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  } else {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  /* Get cursor position with respect to the page. */

  if ($.browser.msie) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  } else {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  /* Move drag element by the same amount the cursor has moved. */


/* limit range */
  var knob  = dragObj.elNode;
  var arrow = dragObj.elNode.previousSibling;
  var bar   = dragObj.elNode.parentNode;
  var y0 = parseInt(arrow.clientHeight);
  var y1 = parseInt(bar.clientHeight)
		- parseInt(arrow.clientHeight)
	  - parseInt(knob.clientHeight);
  var y = (dragObj.elStartTop  + y - dragObj.cursorStartY);
  if(y < y0) y = y0;
  if(y > y1) y = y1;


  dragObj.elNode.style.top  = y + "px";

/* no horizontal movement */
/*  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px"; */

/* move target scroll area */
  scroll_adjust(dragObj.elNode.parentNode,'target');


  if ($.browser.msie) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  } else
    event.preventDefault();
}

function dragStop(event) {

  /* Stop capturing mousemove and mouseup events. */

  if ($.browser.msie) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  } else {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}
/* end http://www.brainjar.com/dhtml/drag/ */

function scroll_init(obj)
{
	scroll_adjust(obj,'knob');
}

function scroll_adjust(obj,who)
{
	var bar = obj;
	var target = obj.previousSibling.firstChild.nextSibling.firstChild;
	var target_h = parseInt(target.clientHeight) - parseInt(bar.clientHeight);
	var knob = obj.firstChild.nextSibling;
	if(target_h>0) {
		bar.style.visibility = 'visible';

		var arrow = obj.firstChild;
		switch(who) {
		case 'knob':
			var target_y = target.style.top ? parseInt(target.style.top) : 0;
			knob.style.top = Math.floor(arrow.clientHeight-target_y*
					(bar.clientHeight-knob.clientHeight-arrow.clientHeight*2)
					/target_h
				)+"px";
			break;
		case 'target':
			var knob_y = knob.style.top ? parseInt(knob.style.top)
										-parseInt(arrow.clientHeight) : 0;
			var knob_h = parseInt(bar.clientHeight)
						- parseInt(arrow.clientHeight)*2
						- parseInt(knob.clientHeight);
			target.style.top = Math.floor(-knob_y*target_h/knob_h)+"px";
			break;
		}
	}
}

function scroll_move(obj, direction)
{
	var bar = obj;
	var target = obj.previousSibling.firstChild.nextSibling.firstChild;
	var target_h = parseInt(target.clientHeight) - parseInt(bar.clientHeight);
	if(target_h>=0) {
		var target_y = target.style.top ? parseInt(target.style.top) : 0;
		target_y += direction * 64;
		if(target_y < -target_h) { target_y = -target_h; }
		if(target_y >  0)        { target_y =  0; }
		target.style.top = target_y+"px";

		scroll_adjust(obj,'knob');
	}

	return false;
}

function scroll_drag(obj, ev)
{
	dragStart(ev);
}


/* IMAGING */

function set_png(id,src) {
	var title = document.getElementById(id);
	if(title) {
		if(document.all) {
			title.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"')";
			switch(id) {
				case 'title':
					title.style.marginBottom = "-6px"; /* weird */
					break;
			}
		} else {
			title.style.backgroundImage = "url('"+src+"')";
		}
	}
}

function element_opacity(element, opacity) {
	if (element.filters) {
		if(element.filters.alpha) {
			element.filters.alpha.opacity=100*opacity;
		}
	} else {
		element.style.opacity=
		element.style.MozOpacity=opacity;
	}
}

var slide;
function Slide_add(id, list) {
	this.slot_list.push({
			id:id,
			image_list:list,
			image_index:0,
			transition:0});
}
function Slide_timer(func,delay) {
	if(this.timer_id)
		clearTimeout(this.timer_id);
	this.timer_id = setTimeout(function() {
		slide.timer_id = null;
		func();
	},delay);
}
var console;
function Slide_round() {
	if(this.slot_list.length == 0) {
		this.timer(function () { slide.round() }, this.delay_round);
		return;
	}
	this.slot_index = (this.slot_index + 1) % this.slot_list.length;
	var slot = this.slot_list[this.slot_index];
	slot.image_index = (slot.image_index + 1) % slot.image_list.length;
	var to = document.getElementById(slot.id+"b");
	if(!to) return; /* break the cycle, probably content unloaded */
	slot.transition = 0;
	to.onload  = function() { slide.fade(); };
	to.onerror = function() { slide.round(); };
	to.src = slot.image_list[slot.image_index];
	/* console.log("load (%d:%d) %s", this.slot_index, slot.image_index, slot.image_list[slot.image_index]); */
}
function Slide_fade() {
	var slot = this.slot_list[this.slot_index];
	var from = document.getElementById(slot.id+"a");
	var to   = document.getElementById(slot.id+"b");
	if(!from || !to) return; /* break the cycle, probably content unloaded */

	slot.transition += .1;
	if(slot.transition < .8) {
		if(to.style.visibility != "visible")
			to.style.visibility = "visible";
		element_opacity(to, slot.transition);
		this.timer(function () { slide.fade(); }, this.delay_fade);
	} else {
		from.onerror = /* XXX */
		from.onload  = function() {
			var slot = slide.slot_list[slide.slot_index];
			var to   = document.getElementById(slot.id+"b");
			if(!to) return; /* break the cycle, probably content unloaded */
			to.style.visibility = "hidden";
			slide.timer(function () { slide.round(); }, slide.delay_interval);
		}
		from.src = to.src;
	}
}
function Slide() {
	this.delay_round = 500;
	this.delay_fade = 100;
	this.delay_interval = 1500;
	this.slot_list = new Array();
	this.slot_index = 0;
	this.timer_id = null;

	this.add   = Slide_add;
	this.timer = Slide_timer;
	this.round = Slide_round;
	this.fade  = Slide_fade;

	this.timer(function () { slide.round(); }, this.delay_interval);
}

var slide;
function slideshow_init()
{
	slide = new Slide();
}
function slideshow(id, list)
{
	slide.add(id, list);
}

var switch_img_prev = -3;
var switch_img_next = -1;
function switch_img(base_id, count, id)
{
	if(id<0) {
		id = ((window[base_id+"selected"] ? window[base_id+"selected"] : 0) + id+2 + count) % count;
	}
	window[base_id+"selected"] = id;

	for(i=0; i<count; i++) {
		var img = document.getElementById(base_id+i);
		img.style.visibility = (i == id) ? 'visible' : 'hidden';
	}
}

function adapt_img_parent(obj)
{
	var div = obj.parentNode;
	if(!div) return; /* possibly destroyed by internal navigation */
	if(div.clientWidth < obj.clientWidth) div.style.width = obj.clientWidth+"px";
	if(div.clientHeight < obj.clientHeight) div.style.height = obj.clientHeight+"px";
}


function fix_image_margin_on_non_ie(id,border)
{
	var img = document.getElementById(id);
	if(!border) border = 0;
	img.style.marginBottom = (border-($.browser.msie ? 2 : 3))+"px";
}

function playpath(path)
{
	return path.replace('disco_musica/','player.php?');
}


function ready_init() {
	/*  MUSIC PLAYER */

	var fg = 'fg';
	var bg = 'bg';
	var index = 0;
	var entry;

	soundManager.debugMode = false;      /* enable debugging output (div#soundmanager-debug, OR console..) */
	soundManager.useConsole = false;     /* use firebug/safari console.log()-type debug console if available */
	soundManager.consoleOnly = true;   /* if console is being used, do not create/write to #soundmanager-debug */
	soundManager.url = '.'; /* path (directory) where SM2 .SWF files will be found. */
	soundManager.useHighPerformance = true; /* position:fixed flash movie for faster JS/flash callbacks */

	soundManager.defaultOptions.volume = 25;
	soundManager.defaultOptions.onfinish = function() {
		var swap = bg; bg = fg; fg = swap;
		entry = playlist[index];
		soundManager.destroySound(bg);
		soundManager.play(fg);
	};
	soundManager.defaultOptions.onplay = function() {
		$('div.player').html(
			(entry.link ? '<a href="'+entry.link+'">' : '') +
				entry.label + (entry.link ? '</a>' : '')
		);
	};
	soundManager.defaultOptions.onpause = function() {
	};
	soundManager.defaultOptions.onbeforefinish = function() {
		soundManager.destroySound(bg);
		index = (index + 1) % playlist.length;
		soundManager.createSound({
			id: bg,
			url: playpath(playlist[index].path)
		});
	};
	soundManager.defaultOptions.onload = function() {
		/* this.setPosition(this.durationEstimate-5000); */ /* debug */
	};

	soundManager.onload = function() {
		soundManager.createSound({
			id: fg,
			url: playpath(playlist[index].path)
		});
		entry = playlist[index];
		soundManager.play(fg);
		$('div#player-pause').show();
		$('div#player-play').hide();
	};

	soundManager.onerror = function() {
		$('div#player-prev').hide();
		$('div#player-next').hide();
		$('div#player-pause').hide();
		$('div#player-play').hide();
		$('div.player').html('<span style="color:silver">para ouvir as m&uacute;sicas instale o <a href="http://get.adobe.com/flashplayer/">flash player</a></span>')
	};


	$('a.player')
	.live('click', function () {
		label = $(this).prev('a').text() + '<span class="extra"> : '+$('a.active').text()+'</span>';

		entry = {
			path: $(this).attr('href'),
			label: label
		};
		soundManager.destroySound(fg);
		soundManager.createSound({
			id: fg,
			url: playpath(entry.path)
		});
		soundManager.play(fg);
		return false;
	});

	$('div#player-pause')
	.live('click', function () {
		soundManager.pause(fg);
		$('div#player-pause').hide();
		$('div#player-play').show();
	});

	$('div#player-play')
	.live('click', function () {
		soundManager.resume(fg);
		$('div#player-pause').show();
		$('div#player-play').hide();
	});

	$('div#player-next')
	.live('click', function () {
		index = (index + 1) % playlist.length;
		soundManager.destroySound(bg);
		soundManager.destroySound(fg);
		soundManager.createSound({
			id: fg,
			url: playpath(playlist[index].path)
		});
		entry = playlist[index];
		soundManager.play(fg);
	});

	$('div#player-prev')
	.live('click', function () {
		index = (index + playlist.length - 1) % playlist.length;
		soundManager.destroySound(bg);
		soundManager.destroySound(fg);
		soundManager.createSound({
			id: fg,
			url: playpath(playlist[index].path)
		});
		entry = playlist[index];
		soundManager.play(fg);
	});

	/* INTERNAL LINKS */

	$('a')
	.live('click', function () {
		var href = $(this).attr('href');
		if(href.indexOf('?') == 0) {
			$('div.conteudo').html('');
			$.get("conteudo.php"+href, function(data){
				$('div.conteudo').html(data)
			}, 'html');
			return false;
		}
	});

}
$(document).ready(ready_init);

/* alert(ready_init.toString().length) */
