/* TopUp.host = "http://ideva.ca/";*/
//  TopUp.images_path = "/lib/js/jquery/topup/images/top_up/";

Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
};

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}
	


_programline_height = 63;
_programline_button_height = -1;

function redrawUI() {
	$('#menu-main ul li ul').css('width', $('#menu-main ul').width() + 'px');

	rleft = $('#contents-main').height() + 120;

/*
//	if (typeof rdocheight == 'undefined') {
		rdocheight = $(document).height();
//	};

	rleft = rdocheight - 50;
*/
	$('#footer-container').css('top', rleft + 'px');
}


$(document).ready(function(){

	$('#print-button').click(function(){
		window.print();
	});

	$('table.tablesorter tbody td').attr('rel', 'closed');
	$('table.tablesorter tbody td').height(_programline_height);
//	$('.tgl').height(_programline_height);
	_programline_button_height = $('.tgl').height();


	if ($.browser.msie) {
		_programline_height = 70;
		_programline_button_height = -1;
	}
	if ($.browser.mozilla) {
		_programline_height = 60;
		_programline_button_height = -1;
	}
	
	var tallest = 0;
	$('.vedette.small').each(function(){
		if ($(this).height() > tallest) {
			tallest = $(this).height();
		}
	});
	$('.vedette.small').height(tallest + 5);


    $('.anythingSlider').anythingSlider({
        easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 6000,					// How long between slide transitions in AutoPlay mode
        startStopped: false,			// If autoPlay is on, this can force it to start stopped
        animationTime: 1800,				// How long the slide transition takes
        hashTags: false,				// Should links change the hashtag in the URL?
        buildNavigation: false,			// If true, builds and list of anchor links to link to each slide
		pauseOnHover: false,			// If true, and autoPlay is enabled, the show will pause on hover
		startText: '',             // Start text
        stopText: ''               // Stop text
    });
    
    $('a').edecrypt();

	/* Benevoles */
	$('#benevoles-uploader-switch a').click(function(el) {
		$('#benevoles-uploader').slideDown('fast', redrawUI);
		$('html,body').animate({scrollTop: $('#benevoles-uploader' + id).offset().top},100);
		return false;
	});

	$('.benevoles-media a').lightBox();
	$('a.photo').lightBox();
	  	
	Date.format = 'yyyy/mm/dd';
	$('#date').datePicker({clickInput:true, createButton:false});

	/* Programmation */
	$('.tgl').height(_programline_height);

    $("#tableprog").tablesorter({
    	sortList: [[0,0],[1,0],[2,0]],
    	widgets: ['zebra']
    });

    $('#tableprog tr').mouseover(function(){
    	_opened = ($(this).attr('rel') == 'opened');

		if (!_opened) {
	    	$(this).addClass("prog-hover");
		}
    });
    $('#tableprog tr').mouseout(function(){
    	_opened = ($(this).attr('rel') == 'opened');

		if (!_opened) {
	    	$(this).removeClass("prog-hover");
		}
    });
    
    // Get list of opened panels
    if ($.cookie('panels') == null) {
    	$.cookie('panels', ':');
    }
	panels = $.cookie('panels').split(':');
	var arLen=panels.length;
	for ( var i=0, len=arLen; i<len; ++i ){
		id = panels[i];
		if (id != '') {
			$('#' + id).addClass('prog-hover');
			$('#' + id).attr('rel', 'opened');
			$('#' + id).height($('#alt' +id).height() + _programline_height);
			$('#' + id + ' .tgl').height($('#alt' +id).height() + _programline_height);
			$('#' + id + ' .tgl').removeClass('plus').addClass('minus');
  			if ($.browser.msie) {
	    		$('#alt' + id).show();
	    	}
	    	else {
	    		$('#alt' + id).fadeIn();
	    	}
		}
	}
	redrawUI();

    $('#tableprog tr').click(function(evt) {
    	id = $(this).attr('id');
    	_opened = ($(this).attr('rel') == 'opened');

    	// if opened, close it
    	if (_opened) {
    		if ( (evt.target.nodeName == 'TD') || (evt.target.nodeName == 'SPAN') ) {
    			panels = $.cookie('panels').split(':');
    			panels.splice(panels.indexOf(id), 1);
    			panels = panels.join(':');
	    		$.cookie('panels', panels);
				$(this).attr('rel', 'closed');
				if ($.browser.msie) {
	    			$('#alt' + id).hide();
	  				$('#' + id).height(_programline_height);
	  				$('#' + id + ' .tgl').height(_programline_height);
		  			$('#' + id + ' .tgl').removeClass('minus').addClass('plus');
		  			redrawUI();
				}
				else {
	    			$('#alt' + id).fadeOut(function() {
	  					$('#' + id).height(_programline_height);
	  					$('#' + id + ' .tgl').height(_programline_height);
		  				$('#' + id + ' .tgl').removeClass('minus').addClass('plus');
		  				redrawUI();
		    		});
				}
    		}
    	} // else open it
    	else {
    		if ($.cookie('panels') == null) {
    			$.cookie('panels', ':');
    		}
    		panels = $.cookie('panels').split(':');
    		panels[panels.length] = id;
    		panels = panels.unique();
    		panels = panels.join(':');
    		$.cookie('panels', panels);

			$(this).attr('rel', 'opened');
			if ($.browser.msie) {
	  			$(this).height($('#alt' +id).height() + _programline_height - 6);
	  			$('#' + id + ' .tgl').height($('#alt' +id).height() + _programline_height - 6);
	  			$('#' + id + ' .tgl').removeClass('plus').addClass('minus');
	  		}
	  		else {
	  			$(this).height($('#alt' +id).height() + _programline_height );
	  			$('#' + id + ' .tgl').height($('#alt' +id).height() + _programline_height);
	  			$('#' + id + ' .tgl').removeClass('plus').addClass('minus');
	  		}
  			redrawUI();
    		$('#alt' + id).show();
    	}
    });
});
