/***************************************************************
 *  Copyright notice
 *
 *  (c) 2012 Xwave GmbH <support@xwave.ch>
 *  All rights reserved
 *  Author: Christian Juerges <christian.juerges@xwave.ch>
 *  
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

var tx_xw_gal_pos_top = 0;
var tx_xw_gal_pos_bottom = 2;
var tx_xw_gal_loaded = new Array();



function tx_xwgal_scroll_down() {
  // alert('scrolling down');
  // get fist element of list...
  // jQuery('#gal-container-'+id).fadeOut();
  var vSize = tx_xwgal_getVerticalSize();
  
  if(tx_xw_gal_pos_bottom + 1 < vSize) {
    tx_xw_gal_pos_top++;
    tx_xw_gal_pos_bottom++;
    tx_xwgal_debug('bottom:' + tx_xw_gal_pos_bottom + " top: " + tx_xw_gal_pos_top + " li count: " + vSize);
		
    /* var h = jQuery('#gal-container-0').outerHeight(true);
    var hli = jQuery('.gal-item-vertical').outerHeight(false); */
    var h = 225;
    if ( jQuery.browser.msie ) {
      h = 226;
    } else {
      h = 225;
    }
    
    loadCarousel(tx_xw_gal_pos_bottom + 1);
    jQuery('#gal-container-vertical').animate({
      'top': "-=" + h + "px"
    }, "fast");
    // set vertical button states
    tx_xw_gal_set_vertical_button_state();
	  
  } 
}

function tx_xw_gal_set_vertical_button_state() {
  var vSize = tx_xwgal_getVerticalSize();
  // check if we can still go down
  if(tx_xw_gal_pos_bottom +1 < vSize) {
    // show botton enabled
    jQuery('#tx_xw_gal_down').removeClass('tx_xw_gal_next_vertical_disabled');
		
  } else {
    // alert('adding disable class');
    jQuery('#tx_xw_gal_down').addClass('tx_xw_gal_next_vertical_disabled');
		
  }
	
  if(tx_xw_gal_pos_top ==  0) {
    // show botton enabled
    jQuery('#tx_xw_gal_up').addClass('tx_xw_gal_prev_vertical_disabled');
  } else {
    jQuery('#tx_xw_gal_up').removeClass('tx_xw_gal_prev_vertical_disabled');
  }
}

// scroll up
function tx_xwgal_scroll_up() {
  //alert('scrolling up');
  // get fist element of list...
  var vSize = tx_xwgal_getVerticalSize();
  if(tx_xw_gal_pos_top > 0) {
    tx_xw_gal_pos_top--;
    tx_xw_gal_pos_bottom--;
    tx_xwgal_debug('bottom:' + tx_xw_gal_pos_bottom + " top: " + tx_xw_gal_pos_top + " li count: " + vSize);
		
    var h = jQuery('#gal-container-0').outerHeight(true);
    
    jQuery('#gal-container-vertical').animate({
      'top': "+=" + h + "px"
    }, "fast");
	
    tx_xw_gal_set_vertical_button_state();
  }
	
}

// scroll down multiple times
function tx_xw_gal_scroll_to(scrolldownTimes) {

  for(i=0;i<scrolldownTimes;i++) {
    tx_xwgal_scroll_down();
  } 

	
}

// a debug helper 
function tx_xwgal_debug(message) {
  jQuery('#tx_xw_gal_debug').html(message);
}

// get vertical size 
function tx_xwgal_getVerticalSize() {
  var liCount = jQuery('#gal-container-vertical .gal-item-vertical').size();
  // alert('liCount ' + liCount);
  return liCount;
}


function tx_xw_gal_init() {
  // get all gal-container elements...
  for(var i = 0; i < 4; i++) {
    loadCarousel(i);
  }
  
}

function loadCarousel(i) {
  /* jQuery.ajaxSetup ({
    cache: false,
    async: false
  }); */
  
  
  
  if(jQuery.inArray(i, tx_xw_gal_loaded) == -1) {
    var el = '#gal-container-'+i;
    var galuid = jQuery(el).attr('galuid');
    var url = "/index.php?eID=xwgalpi1_loadcarousel&el="+galuid;
    jQuery(el).load(url, null, function() {
      attachjcarousel(galuid);
       
      
    });
    
    
    
    tx_xw_gal_loaded.push(i);
        
  }
}

function attachjcarousel(galuid) {
  jQuery('#'+ 'tx_xwgal' + galuid).jcarousel({
    auto: 0,
    galID: galuid,
    scroll: 5
        
  });
  
  
  jQuery('a.xw-gal-noaccess').fancybox({
      'titleShow': false,
      'padding': 0,
      'margin': 0,
      'autoDimensions': true,
      'autoScale': true,
      'type': 'inline'
  }); 
  
  jQuery('a.xw-gal').fancybox({
    'titleShow': 'true',
        
    'autoDimensions': true,
    'autoScale': true,
    'onComplete': function() {
      loadJWPlayer(this);
    },
    'onCleanup': function() {
      unloadJWPlayer(this); 
    } 
  });
		  
	 
}

function loadJWPlayer(p) {
  if(p.type=='ajax') {
    jwplayer("player").setup({
      flashplayer: "/typo3conf/ext/xw_gal/res/mediaplayer-5.8/player.swf",
      "autostart": true,
      "dock": false,
      "controlbar.position": "bottom"
      
      
    });
  }
}

function unloadJWPlayer(p) {
  if(p.type=='ajax') {
    jwplayer("player").remove();
    
  }
}


/**
 * inits the up/down buttons 
 */
function tx_xw_gal_init_buttons(eventCount) {
  // alert('binding');
  // alert("eventCount " + eventCount   +  " tx_xw_gal_pos_bottom " + tx_xw_gal_pos_bottom);
  if(eventCount -1 <=  tx_xw_gal_pos_bottom ) {
    
    jQuery('#tx_xw_gal_down').addClass('tx_xw_gal_next_vertical_disabled');
  }
  
  jQuery('#tx_xw_gal_down').bind ({
    click: function() {
      tx_xwgal_scroll_down();
    }
  });
  
  jQuery('#tx_xw_gal_up').bind ({
    click: function() {
      tx_xwgal_scroll_up();
    }
  });

 
}

