/**
 * @version: 0.1
 * @date   : 27.05.2011 07:39:49
 * @autor  : bernd göbel, strich-komma
 * 
 */


  function show_curPage(i){
    var currentBox = $('.carousel li:nth-child('+(i+1)+')');
    var boxID = currentBox.attr('id');
  
    if(  projectInfo[boxID]['link'] != '' && projectInfo[boxID]['link_name'] != '' ){
      var tmpHTML = '<a href="'+urldecode( projectInfo[boxID]['link'] )+'" target="_blank">'+urldecode( projectInfo[boxID]['link_name'] )+'</a>'
    }
    else {
      var tmpHTML = $('.carousel li:nth-child('+(i+1)+')').find('h2').text() ;
    }
    $('.carousel .carousel-info').html( '<h2>' + tmpHTML + '</h2>' );
    referencesPage = i;
    
    if( true == referencesOpen){
      if( currentBox.find('p.image').length < 2 && projectInfo[boxID]['imageList'] != '' ){
          tmpImages = projectInfo[boxID]['imageList'].split(',');
          for ( n = 1; n < tmpImages.length; n++){
            currentBox.append('<p class="image addImages"><img src="x_images/'+projectInfo[boxID]['dir']+'/'+tmpImages[n]+'" alt="" /></p>');
          }
        }
        var newHeight = ( (currentBox.find('p.image').length) * 660 + 20 ) + 'px';
        currentBox.css('height', newHeight); 
        $('.carousel-wrap').css('height', newHeight );
        tmpHTML = currentBox.find('div.text').html();
        $('.carousel .carousel-info').html( tmpHTML );
    }
  }

  function open_page( me ){
    var currentBox = $('.carousel li:nth-child('+(referencesPage + 1)+')');
    var boxID = currentBox.attr('id');
      
    if( me.hasClass('open') ){
      referencesOpen = true;
      $('.carousel').addClass('open');
      me.removeClass();
      me.addClass('carousel-control close carousel-close');
      if( currentBox.find('p.image').length < 2 && projectInfo[boxID]['imageList'] != '' ){
        tmpImages = projectInfo[boxID]['imageList'].split(',');
        for ( n = 1; n < tmpImages.length; n++){
          currentBox.append('<p class="image addImages"><img src="x_images/'+projectInfo[boxID]['dir']+'/'+tmpImages[n]+'" alt="" /></p>');
        }
      }
      var newHeight = ( (currentBox.find('p.image').length) * 660 + 20 ) + 'px';
      currentBox.css('height', newHeight); 
      $('.carousel-wrap').css('height', newHeight );
      tmpHTML = currentBox.find('div.text').html();
      $('.carousel .carousel-info').html( tmpHTML );
    }
    else {
     referencesOpen = false;
     $('.carousel').removeClass('open');
     me.removeClass();
     me.addClass('carousel-control open carousel-open');
     var newHeight = '680px';
     $('.carousel li').css('height', newHeight); 
     $('.carousel-wrap').css('height', newHeight );

     if(  projectInfo[boxID]['link'] != '' && projectInfo[boxID]['link_name'] != '' ){
        var tmpHTML = '<a href="'+urldecode( projectInfo[boxID]['link'] )+'" target="_blank">'+urldecode( projectInfo[boxID]['link_name'] )+'</a>'
      }
      else {
        var tmpHTML = $('.carousel li:nth-child('+(referencesPage+1)+')').find('h2').text() ;
      }
      $('.carousel .carousel-info').html( '<h2>' + tmpHTML + '</h2>' );
    }
  }
  
  
  function write_href(){
     $(".carousel ul li").each(function (i) {
      var boxID = $(this).attr('id');
      if(  projectInfo[boxID]['link'] != '' && projectInfo[boxID]['link_name'] != '' ){
        var tmpHTML = '<a href="'+urldecode( projectInfo[boxID]['link'] )+'" target="_blank">'+urldecode( projectInfo[boxID]['link_name'] )+'</a>';
        
        $(this).find('div.text h2').html(tmpHTML);
        
      }
     });
  }
  
  function get_page(){
    var ref = $.getUrlVar('page');
    if( typeof ref == 'undefined' ) {
      return false;
    }else{
      $('.carousel li').each(function(n) {
        if( $(this).hasClass(ref) ){
          referencesClass = ref;
          referencesCount = n;
        }  
      });
    }
  }
  
/* Getparameter */
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

function urldecode (str) {
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/urldecode    // +   original by: Philip Peterson
    return decodeURIComponent((str + '').replace(/\+/g, '%20'));
  }

