/**
 * Multiple Elements Cycle Plugin.
 *
 * Provides a simple preview scrolling panel. Shows the given range of li items from the middle and
 * allows scrolling left and right within the list. Does not handle automatic scrolling / time based
 * or wrapping items around. 
 *
 * Note $.multipleElementsCycle needs to be called on a div containing a ul rather then the actual ul

 * Copyright (c) 2009 Will Rossiter (willr.co.nz)
 * Licensed under the GPL license:
 *
 * http://www.gnu.org/licenses/gpl.html
 *
 * @author Will Rossiter <will@silverstripe.com>
 * @version 0.1
 */
(function($) {
	$.fn.multipleElementsCycle = function(options){
		var defaults = {
			elementContainer: '#cycleElements',	// Selector for element (ul) container
			prevElement: '#cycleElementsLeft',	// Selector to scroll previous
			nextElement: '#cycleElementsRight', // Selector to scroll next
			speed: 500,							// Speed to scroll elements
			containerWidth: false,				// Override default width
			//showCount: 4						// Items to show from the list
            //containerWidth: 745,
            showCount: 6
		};
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {
			// GET ELEMENTS
			var totalElements = $(this).find("li");
			var maxIndex = totalElements.length - 1;
            
			// WORK OUT START INDEX
			// assumes that total elements is greater then the slice
			var startIndex = Math.floor((maxIndex - options.showCount) / 2);
			var elementWidth = $(this).find("li").outerWidth(true);
			var margin = ((startIndex + 1) * elementWidth) * -1;
			var lowerIndex = startIndex + 1;
			var upperIndex = startIndex + options.showCount;
			var parent = $(this);
			
			// SORT OUT STYLES
			$(this).find(options.elementContainer).css({
				'width': (options.containerWidth) ? options.containerWidth : elementWidth * options.showCount,
				'overflow': 'hidden'
			});
			$(this).find("ul").css({
				'width': (maxIndex + 1) * elementWidth,
				'padding': '0'
			});
			
			// INIT
			cycle("load");
			
			// CLICK
			$(options.nextElement).click(function(){ cycle("next"); return false; });
			$(options.prevElement).click(function(){ cycle("prev"); return false; });	
			
			// CYCLE
			function cycle(dir){			
				switch(dir){
					case "next":
						if(upperIndex <= maxIndex) {
							$(options.prevElement).show();
							margin = margin - elementWidth;
							upperIndex = upperIndex + 1;
							lowerIndex = lowerIndex + 1;
							$("ul",parent).animate({
									marginLeft: margin
								},options.speed);
								
							if((upperIndex + 1) > maxIndex) {
								$(options.nextElement).hide();
							}
						}					
						break;	
					case "prev":
						if(lowerIndex >= 0) {
							$(options.nextElement).show();	
							upperIndex = upperIndex - 1;
							lowerIndex = lowerIndex - 1;
							margin = margin + elementWidth;
							$("ul",parent).animate({
								marginLeft: margin
							}, options.speed);
							if((lowerIndex-1) < 0) {
								$(options.prevElement).hide();
							}
						}
						break;
						
					case "load": {
						$("ul",parent).animate({
							marginLeft: margin
						}, options.speed);
						break;
					}
					default:
						break; 
				}													
			}
		}) 
	}
})(jQuery);

/*______________________________________________________________________________
 */
// Code for application
jQuery(document).ready(function() {
    $("#webinar_scroller").multipleElementsCycle();

    // Transparancy for webinars item
    jQuery("#cycleElements li").fadeTo("slow", 0.4);
    jQuery("#cycleElements li").mouseover(function(){
        jQuery(this).fadeTo("fast", 1);
    })
    jQuery("#cycleElements li").mouseleave(function(){
        jQuery(this).fadeTo("fast", 0.4);
    })
    //
    
    var needed_width = 100; //px
    check_web_info_length(needed_width);
})

function check_web_info_length(nedded_width){
    var web_info_items = jQuery(".web_info");
    web_info_items.each(function(){
        while(jQuery(this).width() > nedded_width){
            var current_content = jQuery(this).html();
            var last_whitespace_pos = current_content.lastIndexOf(' ');
            if(last_whitespace_pos == -1) break;

            var part1 = current_content.slice(0, last_whitespace_pos);
            var part2 = current_content.slice(last_whitespace_pos+1);
            var new_content = part1 + "<br/>" + part2;
            jQuery(this).html(new_content);
        }

        if(jQuery(this).width() > nedded_width) {
            jQuery(this).css("width", nedded_width);
            jQuery(this).css("overflow", "hidden");
        }

        var h = parseInt(jQuery(this).css("top")) + jQuery(this).height();
        var closest_li_m_b = parseInt(jQuery(this).closest("li").css("margin-bottom"));
        var H = closest_li_m_b + jQuery(this).closest("li").height();

        if(H < h + 5) {
            var dif = h + 5 - H;
            var a = closest_li_m_b;
            var b = (a + dif).toString() + "px";
            jQuery(this).closest("li").css("margin-bottom", b);
        }
    })
}