/*
 * Initialize event handlers
 */




$(document).ready(function() {

// start cycle plugin on homepage
 $('.fce-contenthome .right .text a').click(function() {
     if($('.slides').parent().find('#next').length<1) {
       $('.slides')
         .parent()
         .append('<div id="next"><img src="fileadmin/templates/images/lb_arrow_right.jpg" alt="Right"></div>')
         .append('<div id="prev"><img src="fileadmin/templates/images/lb_arrow_left.jpg" alt="Left"></div>');
     }
     $('.slides')
       .cycle({
         fx: 'fade'
         ,prev: '#prev'
         ,next: '#next'
         ,timeout: 0
       });
     $('.slides').parents('.infolayer').css('top', '20px');
     $('.slides').parent().find('.close').click(function() {
       $('.slides').cycle('destroy');
     });
 });


var $boxes = $('.fce-producthome .menu li:not(.blank) img').hide();
//alert($boxes.length);
div = 0;
//$('#animate').click(function () {
/*while (div < $boxes.length)
{
	$($boxes[div++]).fadeIn('slow');
}*/


(function() {  
  $($boxes[div++] || []).fadeIn('slow', arguments.callee);  
})(); 

//});
  
  
  $('.fce-producthome .menu li img').hover(
  	function() {
		
  		$(this).animate({"marginTop" : '+=4px'});
  	},
  	function() {
  		$(this).animate({"marginTop": '-=4px'});
  	}
  
  );
  
  $('.fce-productdetail .menu li img').hover(
  	function() {
		
  		$(this).animate({"marginTop" : '+=4px'});
  	},
  	function() {
  		$(this).animate({"marginTop": '-=4px'});
  	}
  
  );
  
  
});






var currentExternalOverlayUrl = '';

var currentOverlayId = '';
var tabapiCount = 0;
var tabapi = new Array();

var scrollableSeekCount = 0;

/*
 * Initialize JS functions
 */
function initPage() {

  /*
   * Hero-Fade-In
   */
  $('.fce-productdetail .right').css('display', 'none');
  $('.fce-productdetail .right').fadeIn(1250);
  $('.fce-details .right .col1').css('display', 'none');
  $('.fce-details .right .col1').fadeIn(1250);
 
  /*
   * Expose product menu images 
   */
  //setTimeout('exposeProductMenus()', 1000);
 

 
 
  /*
   * Lightboxes
   */ 
   
   
  $('body').append('<div id="externaloverlay" style="display:none; position:absolute;"><div class="wrap"></div><div class="close"><img src="fileadmin/templates/images/close.gif" alt="Close"></div></div>');
  
  var footerOverlayAPI = $("#footermesnu a[rel]").overlay({
    oneInstance: false,
    api: true,
    closeOnClick: false, 
    top: '20%',
    left: 'center',
    expose: { 
      color: '#000000', 
      closeOnClick: false 
    },
    onBeforeLoad: function() {
      
	
      var wrap = this.getContent().find("div.wrap");
      var url = this.getTrigger().attr("href");      

      var find = url.search(/nonjslightbox%5Bpid%5D/);
alert(find);
		if(find == -1)
			return false;

		url = url.substring(find + 23);
		find = url.indexOf('&');

		if(find != -1)
			url = url.substring(0, find);
	
		url = 'index.php?id=' + url;

      if(wrap.is(':empty') || currentExternalOverlay != url) {
        wrap.html('');
        wrap.load(url + ' #contentcontainer > *'); 
        currentExternalOverlay = url;
      }
    }
  });
   
  
  $(".infolayer").each(function() {
    var temp = $(this).clone();
    $(this).remove();
    temp.appendTo('body');
  });


  $(".infolayer .tabs").after('<div class="tabnavi"><div class="prev"><a href="#"><img src="/fileadmin/templates/images/prev.gif" alt="Prev" /></a></div><div class="next"><a href="#"><img src="/fileadmin/templates/images/next.gif" alt="Next" /></a></div></div>');


$("#footermenu a[rel]").overlay({
    oneInstance: false,
    closeOnClick: false, 
	 api: true,
	 absolute: true,
	 left: 'center',
	 top: '13%',
    expose: { 
      color: '#000000', 
      closeOnClick: false 
    },
    onBeforeLoad: function () { 
    	var wrap = this.getContent().find("div.wrap");
    	currentExternalOverlay == url;
		currentOverlayId = this.getOverlay().attr('id');
      for(var i = 0; i < tabapi.length; i++) {
        tabapi[i].next();
		  tabapi[i].prev();
      }
    }
  });
  
  

 $("#contentcontainer a[rel]").overlay({
    oneInstance: false,
    closeOnClick: false, 
	 api: true,
	 absolute: true,
	 left: 'center',
	 top: '13%',
    expose: { 
      color: '#000000', 
      closeOnClick: false 
    },
    onBeforeLoad: function () { 
		currentOverlayId = this.getOverlay().attr('id');
      for(var i = 0; i < tabapi.length; i++) {
        tabapi[i].next();
		  tabapi[i].prev();
      }
    }
  });
  

  // Fix a bug in jQuery Tools Tabs 
  $("div.tabs ul li a").each(function() { $(this).attr('href', $(this).attr('href').replace(/(.*)#/, '#')); });


  // Create the tabs
  $('div.tabs ul').each(function () {
    var id = tabapiCount++;

    tabapi[id] = $('div.tabs ul').tabs('.pane', { api: true });
 
    if(tabapi[id]) {

      // Fix background (bug?!)
      tabapi[id].onClick(function(index) {

        var tabs = $('#' + currentOverlayId + ' .tabs .panes .pane');
        var selTab = tabs.eq(index);

		  $('div.tabs').css('background-image', 'none');
    
        if(selTab.attr('overlay')) {
          $('div.tabs').css('background-image', 'url(/' +  selTab.attr('overlay') + ')');
        }
		  else if(selTab.attr('overlaycss')) {
		    $('div.tabs').css('background-image', selTab.attr('overlaycss'));
  		  }


		  if(index == 0) {
			 $('.tabnavi .prev').css('display', 'none');
		  }
        else {
          $('.tabnavi .prev').css('display', 'block');
 		  }

		  if(index + 1 >= tabs.length) {
			 $('.tabnavi .next').css('display', 'none');	
		  }
        else {
          $('.tabnavi .next').css('display', 'block');
 		  }

      });

      var selTab = tabapi[id].getPanes().eq(0);
      if(selTab.css('background-image')) {
        $('div.tabs').css('background-image', selTab.css('background-image'));
      }

		tabapi[id].getPanes().each(function () { 
			
			var bg = $(this).css('background-image');

			if(bg && bg != 'none')
				$(this).attr('overlaycss', bg);

			$(this).css('background-image', 'none'); 
		});


      // Set next and back
      $('.infolayer .tabnavi .next').click(function() { tabapi[id].next(); return false; });
      $('.infolayer .tabnavi .prev').click(function() { tabapi[id].prev(); return false; });
    }
  });

  // Load Scrollable menu (if required)
  loadScrollable();

  
  // Add reflection to the menu (always required)
//  $('#mainmenu li.active a img').reflect({height: 1});

//var new_src= $('#mainmenu li.active a img').attr('src').replace('.jpg', 'a.jpg');
//	$('#mainmenu li.active a img').attr('src', new_src );


  /*
   * Contentcontainer hover
   */
  $('#contentcontainer').hover(
  
    function () {
      $('#mainmenu li:not(.active) a img').unreflect();
      $('#mainmenu li.active a img').reflect({height: 1});
      $('.submenu:not(.active-submenu) ').css('display', 'none');
    },

    function () { }

  );


  /*
   * Hover for main menu items
   */
  $('#mainmenu li').hover(

    // On mouse over
    function () {

      $('#mainmenu li a img').unreflect();
      $('.submenu:not(.active-submenu)').css('display', 'none');

/*      var pagename = $(this).children('a').children('img').attr('id');*/
	   var pagename = $(this).children('a').attr('id');
      $('#submenu-' + pagename).css('display', 'block'); 

      $(this).children('a').children('img').reflect({height: 1});
    },

    // On mouse out
    function () { }

  );
}



/*
 * Load  the scrollable plugin if required
 */
function loadScrollable() {

  // Do nothing if page does not contain scroll elements
  if($('div.scrollable').length <= 0) 
    return; 

  // load the scrollable jquery plugin and call init function
/*  $.getScript('/fileadmin/templates/scripts/jquery.scrollable.js',
              function() { setTimeout(initScrollable, 100) } );*/
  $.getScript('/fileadmin/templates/scripts/urlparam.js');
  $.getScript('/fileadmin/templates/scripts/jquery.scrollable.js', initScrollable);

}

/*
 * Init the scrollable plugin 
 */
function initScrollable() {

  var api = $('div.scrollable').scrollable({
    size: 11,
    api: true,
    clickable: false,
    prev: '.prev',
    next: '.next',
    prevPage: '.prevPage',
    nextPage: '.nextPage',
    items: '.items'
  });

  if(!api)
    return;

  api.onBeforeSeek(function(index) { 

    $('div.scrollable ul.items li a').each(function() {
	   $(this).attr('href', $(this).attr('href').replace(/\?st=\d*&no_cache=1/, '') + '?st=' + index + '&no_cache=1' );
	 });

  });
  
  var conf = api.getConf();
  conf.speed = 0;
  
  var goto = $(document).getUrlParam('st');

  for(var i = 0; i < goto; i++) {
    api.next();
  }

  conf.speed = 400;
/*  //Move to active page
  var items = api.getItems();
  var activeIndex = 0;
  for(var i = 0; i < items.size(); i++) {
    var itm = $(items.get(i));
    if(itm.hasClass('active')) {
      /* activeIndex = i; */
/*      break;
    }
	 if(i >= 10) 
		 api.next();
  } 

/*  api.setPage(Math.floor(activeIndex/11));
  conf.speed = 400; */

}


function exposeProductMenus() {

	var first = $('.fce-producthome .menu li:not(.blank):first img');

	if(!first || first.length <= 0)
		return;

	var width = parseInt(first.css('width').replace(/px/, ''));
	var height = parseInt(first.css('height').replace(/px/, ''));
	var prop = width / height;
	var exposeWidth = (width - 3);
	var exposeHeight = (exposeWidth / prop);

	var speed = 200;
	var interval = 3000;

   first.animate( { 'width': exposeWidth + 'px', 'height': exposeHeight + 'px'}, speed );
	first.animate( { 'width': width + 'px', 'height': height + 'px' }, speed );

	setTimeout('exposeProductMenus()', interval); 
}

// Call initPage upon ready DOM
jQuery(initPage);


