/**
 * params
 *	elemCarousel
 *	perMove
 */
function wdm_slide( objParams ) {
	objParams = objParams || {};
	var _iterator;
	for (_iterator in objParams) {
		this[_iterator] = objParams[_iterator];
	}
	
	this.maxMove = this.triggers.length -1;
	//this.perMove = this.perMove * -1;
	this.currentMove = 0;
	this.timer = null;
	var _self = this;
	
	this.move = function(moveTo) {
		
		if ( moveTo > this.maxMove) {
            moveTo = 0;
		}
       
        this.currentMove = moveTo;
        this.triggers.closest('ul').find('li.active').removeClass('active');
		this.triggers.eq(moveTo).closest('li').addClass('active');
		/*
		this.elemCarousel.animate({
				marginLeft : moveTo
			}, 'slow', 'linear', function() { if (!_self.timer) _self.start(); });
		//this.elemCarousel.css('top', moveTo) */
		this.elemCarousel.find('div.cont:visible').fadeOut('normal', function() {
			_self.elemCarousel.find('div.cont').eq(moveTo).fadeIn('normal', function() { 
						if (!_self.timer) _self.start();
					});
		});
	};
	
	this.start = function() {
		_self.timer = setInterval(function() {
			var _moveTo = _self.currentMove + 1;
			_self.move(_moveTo);
		}, _self.speed);
	};
	
	
	this.stop = function() {
		clearInterval(_self.timer);
		_self.timer = null;
	};
	
	this.start();
	
	this.triggers.each( function(i) {
		$(this).click(function() {
			_self.stop();
			_self.move(i);
			return false;
		});
	});
}

$( function() {
    // convert into flash mp3 player
	var _mp3 = $("#TourtheMachine .themachine table.popup .subcontent span.mp3");
	if (_mp3.length > 0) {
		_mp3.jmp3({
				showfilename: "false",
				backcolor: "333333",
				forecolor: "cacaca",
				// check if IE or not
				autoplay: $.support.cssFloat,
				showdownload: false,
				width: 200
			});
	}
	// autoplay: $.support.cssFloat,
    // menu
	$('div.dd-elem').hide();
	$('div.header ul li a:not(.n-home)').each(function () {
        var _me = $(this),
            _class = _me.attr('class').split(' ')[0],
            _parent = _me.parent('li').eq(0);
		
		if ( _class == '' ) {
			_parent.addClass('ndd');
			return;
		}
        
        var _offset = _parent.position();
		
		var _next = $('.dd-elem.'+_class).css('left', _offset['left'] + 2);
           
        if ( _next.length < 1 ) {
            _parent.addClass('ndd');
            return;
        }
        
        var hideDelay = 50,
            hideDelayTimer = null,
            beingShown = false,
            shown = false;
        
        $([_parent.get(0), _next.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) return;
                
                $(_parent).addClass('hover');
                //beingShown = true;
                /*_next.slideDown( 'normal', function() {
                    beingShown = false;
                    shown = true;
                });*/
                _next.show();
                shown = true;
            })
            .mouseout(function () {
                var _me = $(this);
                hideDelayTimer = setTimeout(function() {
                    shown = false;
                   /* _next.slideUp( 'normal', function() {
                        _me.removeClass('hover');
                    });*/
                    _next.hide();
                    _parent.removeClass('hover');
                    
                }, hideDelay);
            });
    });
	
	
	// left nav
	
	//$('ul li ul:visible').hide();
	
	var _leftColumn = $('div.lcolumn').get(0);
	$('li a', _leftColumn).each(function() {
		var _me = $(this),
			_ul = _me.next('ul');
		
		if ( _ul.length < 1 ) return;

		_me.click( function() {
		
         	if ( _ul.is(':visible') ) {
         	   if ( _ul.hasClass('original')) { 	return false;	}
				      //_me.find('span').removeClass('nddwn');
				      _ul.slideUp();
				      return false;
			     }

			//$('ul li ul:visible').slideUp().prev('a').find('span').removeClass('nddwn');

    	//$('ul li ul:visible').slideUp(); 
    	

 $("ul li ul:visible").each(function(){
        var divID = $(this).attr('id'); 
       if (divID == "originalid") {return;}
       if ( $(this).hasClass('original')) {return; 	}
       $(this).slideUp();
      });

			_ul.slideDown();

			//_me.find('span').addClass('nddwn');
			return false;
			});
	});
	
	//carousel
	
	var _carousel = $('div.leftcolumn .carousel');
	(new wdm_slide({
		'elemCarousel' : _carousel.find('div.carousel-cont'),
        'triggers' : _carousel.find('ul.clearfix li a'),
		'perMove' :568,
		'speed' : 8000
		}) );
});

// video player
//$('.media').media( { width: 400, height: 300, autoplay: true } ); 
