Event.observe(window, 'load', navLeaky, false);

var Glance = new Object();

Glance.pgInfo = Array();
Glance.pgInfo['all'] = { page: 0, perPage: 25 };
Glance.pgInfo['films'] = { page: 0, perPage: 25 };
Glance.pgInfo['books'] = { page: 0, perPage: 25 };
Glance.pgInfo['fans'] = { page: 0, perPage: 25 };
Glance.pgInfo['leaky'] = { page: 0, perPage: 25 };
Glance.pgInfo['favs'] = { page: 0, perPage: 25 };

Glance.registerLoading = function() {
}

Glance.updateLinks = function(request){
  $('glance-news').scrollTop = 0;
  var element= document.getElementById('glance-news-next');
  if(Glance.pgInfo[element.getAttribute('nt')].page < 1){
    $('glance-prev-link', 'glance-pages').invoke('hide');
    $('glance-pages').innerHTML = '';
  }else{
    $('glance-pages').innerHTML = '| Page ' + (Glance.pgInfo[element.getAttribute('nt')].page+1) + ' |';
    $('glance-prev-link', 'glance-pages').invoke('show');
  }
}

function navLeaky() {
  var tlBehaviour = {
    '.menu-link' : function(element) {
		element.onmouseover = function() {
        $$('.sub-menu').each(function(e){
          Element.hide(e);
        });
        $A(element.parentNode.getElementsByTagName('div')).each(function(e){
          Element.show(e);
        });
      }
    },

	'.submit-disable' : function(element) {
		element.onsubmit = function() {
			$$('.submit').each(function(e){
				e.disabled = true;
			});
		}
	},
	'.form-submit' : function(element) {
		element.onclick = function() {
			element.parentNode.submit();
			return false;			
		}
	},
	
	'.click-hide' : function(element) {
		element.onfocus = function() {
			if (element.value == element.getAttribute('start')) element.value = '';
		}
		element.onblur = function() {
			if (element.value == '') element.value = element.getAttribute('start')
		}
	},
	
	'.select-jump' : function(element) {
		element.onchange = function() {
			element.disabled = true;
			element.parentNode.submit();
		}
	},
		
	'.tab' : function(element) {
		element.onclick = function () {
			if (element.className != 'tab selected') dhtmlHistory.add('#/?tab='+element.getAttribute('id'), null);
			$$('.tab').each(function(e){
				e.className = 'tab';
			});
			element.className = 'tab selected';
			$$('.t-source').each(function(e){
				Element.hide(e);
			});
			Element.show('tab-'+element.getAttribute('id'));
			return false;
		}
	},
	
	'.advanced-search' : function(element) {
		element.onclick = function () {
			new Effect.Phase(element.getAttribute('id')+'_field', {duration: 0.5});
		}
	},
	
	'#cropImage' : function(element) {
		new Cropper.Img('cropImage', {
			minWidth: 200,
			minHeight: 200,
			maxWidth: 200,
			maxHeight: 200,
			displayOnInit: true,
			onloadCoords: { x1: 10, y1: 10, x2: 210, y2: 210 },
			onEndCrop: saveCropValues
		});
	},

    '.comment_window' : function(element) {
      element.onclick = function() {
        window.open(element.href, 'comments', 'width=480,height=480,scrollbars=yes,status=yes');
        return false;
      }
    },

	'.gallery-tab' : function(element) {
	  element.onclick = function() {
		$$('.gallery-tab').each(function(e){
		  e.className = 'gallery-tab';
		});
		element.className = 'gallery-tab active';
		$$('.gals-items').each(function(e){
		  Element.hide(e);
		});
		$$('.galleries-'+element.getAttribute('id')).each(function(e){
		  Element.show(e);		  
		});
		return false;
	  }
	},

    '.glance-icons' : function(element) {
      element.onmouseover = function() {
        document.getElementsByClassName('glance-icons').each(function(e){
	      if(element.getAttribute('nt') != e.getAttribute('nt')){
            e.className = 'glance-icons';
            $A(e.parentNode.getElementsByTagName('a')).each(function(ea){
              ea.className = ea.getAttribute('nt');
            });
          }
        });
        element.className = element.getAttribute('nt') + ' sel glance-icons'; 
        $A(element.getElementsByTagName('a')).each(function(e){
          e.className = e.getAttribute('nt') + ' sel-i';
        });
      }
      element.onmouseout = function() {
        document.getElementsByClassName('glance-icons').each(function(e){
          $A(e.getElementsByTagName('a')).each(function(ea){
            if(e.getAttribute('cur') == '0'){
              e.className = 'glance-icons';
              ea.className = ea.getAttribute('nt');
            }else{
              e.className = e.getAttribute('nt') + ' sel glance-icons'; 
              ea.className = ea.getAttribute('nt') + ' sel-i'; 
            }
          });
        });
      }
      element.onclick = function() {
        document.getElementsByClassName('glance-icons').each(function(e){
	      if(element.getAttribute('nt') == e.getAttribute('nt')){
	    	element.setAttribute('cur', '1');
            $('all_glance', 'films_glance', 'books_glance', 'fans_glance', 'leaky_glance').invoke('hide');
            Element.show(element.getAttribute('nt') + '_glance');
	  	  }else{
            e.setAttribute('cur', '0');
          }
        });

        return false;
      }
    },

    '.glance-news-archive' : function(element) {
      element.onclick = function() {
        (element.getAttribute('md') == 'next' ? Glance.pgInfo[element.getAttribute('nt')].page++ : Glance.pgInfo[element.getAttribute('nt')].page--);
        new Ajax.Updater(
          element.getAttribute('nt') + '_glance', '/glance_news/' + element.getAttribute('nt') + '/' + Glance.pgInfo[element.getAttribute('nt')].page, 
          {asynchronous:true, evalScripts:true, onLoading: Glance.registerLoading, onComplete: Glance.updateLinks}
        ); 
        
        return false;
      }
    },

    '.actor-link-hidden' : function(element) {
      element.onclick = function() {
        new Ajax.Updater(
          'actor_info_' + element.getAttribute('ai'), '/actor_info/' + element.getAttribute('ai'), 
          {asynchronous:true, evalScripts:true, onComplete: function(response){
            document.getElementsByClassName('actor-info').each(function(e){
	          if($('actor_info_' + e.getAttribute('ai')).style.display != 'none'){
		        new Effect.PhaseOut('actor_info_' + e.getAttribute('ai'), {duration: 0.5});
		        new Effect.PhaseIn('actor_' + e.getAttribute('ai'), {duration: 0.5});
	          }
            });
	        new Effect.PhaseOut('actor_' + element.getAttribute('ai'), {duration: 0.5});
	        new Effect.PhaseIn('actor_info_' + element.getAttribute('ai'), {duration: 0.5});
          }
          }
        ); 
        
        return false;
      }
    },
    
    '#vote a' : function(element) {
      element.onclick = function() {
        new Ajax.Request(this.href, {method: 'get', parameters: '', 
          onComplete: function(r){ 
            $("vote_results").innerHTML = r.responseText; 
            Element.hide('vote');
          }
        });
        return false;
      }
    }
  }
	
  Behaviour.register(tlBehaviour);
  Behaviour.apply();
}

report_comment = function(comment_id) {
  Element.insert($('comment-'+comment_id), { before: $('report_comment')});
  Effect.SlideDown('report_comment', {duration: 0.3})
  $('report_comment').action = "/comments/" + comment_id;
  return false;
}

function new_announcement() {
	new Effect.Phase('new_announcement');
}

function saveCropValues(coords, dimensions) {
	$('x1').value = coords.x1;
	$('y1').value = coords.y1;
	$('x2').value = coords.x2;
	$('y2').value = coords.y2;
	$('width').value = dimensions.width;
	$('height').value = dimensions.height;
}