/*if (top.location != self.location) {
	top.location = self.location.href
}*/

function myPanePage(url){
	$('MyPane_overlay').show();
	new Ajax.Request(url, {
	   onSuccess: function(transport) {
		  $('MyPane_overlay').hide()
		  $('MyPane').innerHTML = transport.responseText;
	  }
	});
	return false;
}

Hash.prototype.toObject = Hash.prototype.toObject || function() { return Object.clone(this); }

var TabbedPane = function(pane, page_urls, args) {
	var args = $H({asynchronous: true, method: 'get', evalScripts: true}).merge(args).toObject();	
	this.load_page = function(page_id) {
		new Ajax.Updater(pane, page_urls[page_id], args);
	}
	
	for (page_id in page_urls) {
		Event.observe(page_id, 'click', function(e) {
			if ('function' == typeof(args.onClick))
				args.onClick(e);

			for (page_id in page_urls) $(page_id).removeClassName('active');
			this.load_page(Event.element(e).addClassName('active').id);
			Event.stop(e);
		}.bindAsEventListener(this));

		//if ($(page_id).hasClassName('active')) { this.load_page(page_id); }
	}
}

var DDSPEED = 10;
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

function ajaxRequest(div, type, box){
	var params = Form.serialize($(div+'Form'));
	new Ajax.Updater(div, '/request/'+type, {asynchronous:true, parameters:params});
	if(box)
		Modalbox.resize(0, 20);
}

function getFeatured(){
	new Ajax.Request('/homeFeatured', {
	  method: 'get',
	  onSuccess: function(transport) {
		if (transport.responseText){
			$('featuredVideos').innerHTML = transport.responseText;
			homeFeatured();
		}
	  }
	});
}

function homeFeatured(){
	setTimeout("getFeatured();",10000);
}

function searchDIV() {

	if(!$('qtype').checked){
		document.searchForm.submit(); 
		return false;	
	}else{	
		if($('qtype').value == "g")
			return true;
		else{
		var el = $('searchall');	
		$('searchDIVloading').show();
		$('searchDIVloading').innerHTML = '<div align="center" style="padding:10px;"><img src="/images/working.gif" border="0"><br/>Se incarca</div>';
		
		if(el.value.length > 2){
			new Ajax.Request('/cauta/fisiere/'+el.value+'&small=yes', {
			  method: 'get',
			  onSuccess: function(transport) {
				if (transport.responseText){
					$('searchDIV').innerHTML = transport.responseText;
					$('searchall').value =  el.value;
					if($('searchDIV').style.display == 'none')
						Effect.SlideDown('searchDIV', {duration: 2.0});		
						window.setTimeout('Effect.SlideUp("searchDIVloading", { duration: 1.0 });', 1000);

					return false;
				}
			  }
			});
		}else{
			$('searchDIV').innerHTML = "<div class='error'>Introduceti minim 3 caractere</div>";
			$('searchDIV').show();
			$('searchDIVloading').hide();
			return false;
		}
		}
	}
}

function reveseSearch(){
	Effect.SlideUp('searchDIV', { duration: 1.0}, { afterFinish: function(){ $('searchDIV').innerHTML = ''; }});
}

function ajaxVote(id, vot, units, section, smallvot){
	if(smallvot)
		$('rate_'+id).innerHTML = '<img src="/images/working.gif" border="0">';
	else
		$('rate_'+id).innerHTML = '<img src="/images/loadingAlbastru.gif" border="0">';
	new Ajax.Updater('rate_'+id, '/ajaxVote/voting?vkey='+id+'&vot='+vot+'&section='+section+'&units='+units+'&small='+smallvot);
}

function sendForm(action, form){
	new Ajax.Request(action+'&'+form, {
	  method: 'get',
	  onSuccess: function(transport) {
		if (transport.responseText){
			$('mainForm').innerHTML = transport.responseText;
			return false;
		}
	  }
	});
}

function setConfig(key, value) {
	new Ajax.Request('/setConfig/' + key + '/' + value, {
	  onComplete: function(transport) {}
	});
}

Event.observe(window,'load',function(evt){
	//$('movable').show();
	var theHeight = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
	var theHeight = theHeight - 200;
	$('movable').setStyle({ top: theHeight + 'px' });
	Event.observe(window,'scroll', function(evt){		
		$('movable').setStyle({ top: theHeight + document.viewport.getScrollOffsets().top + 'px' });
	});
});
