document.ondragstart = function () { return false; };

function AdManager( banners ) {
	this.banners = banners;
	this.currentBanner = 0;
	this.nextBanner = parseInt(this.currentBanner) + 1;
	this.currentMessage = 0;
	
	this.load = function() {
		this._createLinks();
		this._loadBanner();
	};
				
	this.next = function() {
		var _this = this;
		this._loadMessage();
		this._timer = setInterval(function() {
			_this._loadMessage();
		}, delay);	
	};
	
	this.showNextBanner = function() {
		var _this = this;
		this._loadBanner();
	};
	
	this._clean = function() {
		clearInterval(this._timer);
	};
				
	this._createLinks = function() {
		$('#teaser-switch').html('');
		for ( var i in this.banners ) {
			B = this.banners[i];
			$('#teaser-switch').append('<li id="banner' + i + '"><a href="javascript:;" data="' + i + '" title="' + B.keyMessage + '"></a></li>');
			var _this = this;
			
			$('#banner' + i + ' a').click(function() {
				_this.currentBanner = parseInt($(this).attr('data'));
				_this.nextBanner = parseInt($(this).attr('data')) + 1;
				_this.showNextBanner();
				return false;
			});
		}
	};
				
	this._loadMessage = function() {
		if ( typeof this.banners[this.currentBanner].messages[this.currentMessage] == 'undefined' ) {
			this.currentBanner++;
			this._changeBanner();
		}
		
		var M = this.banners[this.currentBanner].messages[this.currentMessage];
		var el = $('#teaser h4').fadeOut(100, function(){
			$('#teaser h4').html(M.message);
			el.fadeIn(100);
		});	
		this.currentMessage++;
	};
				
	this._loadBanner = function() {
		if ( typeof this.banners[this.currentBanner] == 'undefined' ) {
			this.currentBanner = 0;
		}
		
		if ( typeof this.banners[this.nextBanner] == 'undefined' ) {
			this.nextBanner = 0;
		}
		this.currentMessage = 0;
		
		var B = this.banners[this.currentBanner];
		var NB = this.banners[this.nextBanner];

		$('#teaser-switch li a').removeClass('current'); 
		$('#banner' + this.currentBanner + ' a').addClass('current');
		
		var el = $("#teaser img").hide();
		$('#teaser img').attr('src', B.image);
		$('#teaser img').attr('alt', B.title);
		$('#teaser img').attr('title', B.title);
		el.show();
		$('#teaser h3').html(B.keyMessage);
		$('#teaser a').attr('href', B.href);
		$('#teaser .inner').css({
			"background-image" : "url('" + NB.image + "')"
		});
	};

	this._changeBanner = function() {
		if ( typeof this.banners[this.currentBanner] == 'undefined' ) {
			this.currentBanner = 0;
		}
		
		this.nextBanner = this.currentBanner + 1;
		if ( typeof this.banners[this.nextBanner] == 'undefined' ) {
			this.nextBanner = 0;
		}
		
		this.currentMessage = 0;
		
		var B = this.banners[this.currentBanner];
		var NB = this.banners[this.nextBanner];
		
		$('#teaser-switch li a').removeClass('current'); 
		$('#banner' + this.currentBanner + ' a').addClass('current');

		var teaser_image = $("#teaser img").fadeOut(500, function() {
			teaser_image.attr('src', B.image);
			teaser_image.attr('alt', B.title);
			teaser_image.attr('title', B.title);
			teaser_image.show();
			$('#teaser .inner').css({
				"background-image" : "url('" + NB.image + "')"
			});
		});
		var teaser_message = $('#teaser h3').fadeOut(100, function() {
			teaser_message.html(B.keyMessage);
			teaser_message.fadeIn(100);
		});
		$('#teaser a').attr('href', B.href);
	};

};
		

function pos(value) {
	var scrollPane = $( ".customer-showcase-placer" ),
		scrollContent = $( ".customer-logos" );
		
	if ( scrollContent.width() > scrollPane.width() ) {
		scrollContent.css( "margin-left", Math.round( value / 100 * ( scrollPane.width() - scrollContent.width() ) ) + "px" );
	} else {
		scrollContent.css( "margin-left", 0 );
	}
}
		

$(function() {
	var scrollPane = $( ".customer-showcase-placer" ),
		scrollContent = $( ".customer-logos" );
	
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
            pos(ui.value);
		},
		click: function() {
			return false;
		},
		stop: function() {
            scrollbar.down = false;
			scrollbar.drag = false;
        }
	});
	
	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
		scrollbar.width( handleHelper.width() );
	})
	.mouseup(function() {
		scrollbar.width( "100%" );
	})
	.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
	
	scrollbar
        .find('.ui-slider-handle').html('<span></span>')
        .find('span').css({
                'display':'block'
            ,   'height':'10px'
            ,   'width':'100%'
        })
        .mousedown(function(e) {
            scrollbar.down = true; scrollbar.x = e.clientX; scrollbar.left = parseInt($(this).parent().css('left'));// * ($('.scroll-bar').width() - $('.ui-slider-handle').width()) / 100;
            e.stopPropagation();
            e.preventDefault();
        });
        
        $(document).mousemove(function(e) {
            if (scrollbar.down) {
                scrollbar.down = false; scrollbar.drag = true;
            } 
            
            if (scrollbar.drag) {
                var value = scrollbar.left + e.clientX - scrollbar.x;
                if (value < 0) { value = 0; }
                if (value > $('.scroll-bar').width() - $('.ui-slider-handle').width()) { value = $('.scroll-bar').width() - $('.ui-slider-handle').width(); }
                value = Math.round(value / ($('.scroll-bar').width() - $('.ui-slider-handle').width()) * 100);
                $( ".scroll-bar" ).slider('value', value); pos(value); 
            }
        })
        .mouseup(function(e) {
            scrollbar.down = false; scrollbar.drag = false;
        })
	
	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css( "overflow", "hidden" );
	
	
	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
		scrollbar.find( ".ui-slider-handle" ).css({
			width: handleSize,
			"margin-left": -handleSize / 2
		});
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}
	
	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}
	
	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
			var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}
	
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
});


$(function() {
	$(document).ready(function() {
		$('.toggle-list').children('li').removeClass('open').addClass('closed');
		//we want first section expand
		if($('.toggle-list').children()[0]) {
		   $($('.toggle-list').children()[0]).removeClass('closed').addClass('open');
		}
	});
});

$(function() {
	
	//function below cause hyperlink in event that unable to link to new page
	/* 
	$(".toggle-list li a").click(function(e) {
		$(this).parentsUntil("li").parent("li").toggleClass("open").toggleClass("closed");
		e.preventDefault;
		return false;
	});
	*/
	$(".toggle-list .toggle").click(function(e) {
		$(this).parent("li").toggleClass("open").toggleClass("closed");
		e.preventDefault;
		return false;
	});
	
	//hyperlink can use to expand a section
	$(".toggle-list li a.toggle-link").click(function(e) {
		$(this).parents("li").toggleClass("open").toggleClass("closed");
		e.preventDefault;
		return false;
	});
});

$(function(){
	$('dl.tabs dt a').click(function(e){
		$(this)
			.parent('dt')
			.siblings().removeClass('selected').end()
			.next('dd').andSelf().addClass('selected');
		e.preventDefault;
		return false;
	});
});

