function search(url, searchkeys)
{
	document.location.href= url + "?s=" + searchkeys;
}

window.detectSmallBrowser = function()
{

	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	myHeight -= 12;
	
	if( myHeight < 585 ) 
	{
		document.body.className = "small";
	}
}

$(function() {

	// Get With of the content
	var contentWidth = $("#noWidthLimit").width();
	
	
	// Stop when content fits in screen
	if( contentWidth < $(window).width() ){
		$("#navigate-left, #navigate-right").addClass('disabled');
		return;
	}

		
	// Correct main content width 
	$("#widthLimit").css({ 'width' : contentWidth + 'px' });
	$("#main").css({ 'overflow' : 'visible' });

	// Cache variables
	var $window = $(window),
		scrollSize = Math.ceil( $(window).width() * 0.75 ),
		$buttons = $("#navigate-left, #navigate-right"),
		maxScroll = contentWidth - $window.width();

	function scrollWindow( direction, $sender, disableAnimate) {
		if( $sender != undefined && $sender.hasClass('disabled') ) return;	
		var toScroll = $window.scrollLeft() + ( scrollSize * direction );
		
		if( toScroll <= 0 )
			toScroll = 0;

		if( toScroll + 50 >= maxScroll  )
			toScroll = maxScroll;

		if( $window.is(":animated") ) 
			return;
	
		manageButtons( null, null, null, toScroll );

		if( disableAnimate )
		{
			$window.scrollTo( Math.round(toScroll), 0 );
		}
		else
		{
			$window.scrollTo(	Math.round(toScroll), 1000 );
		}
	}
	
	// Bind navigation buttons
	$("#navigate-left").bind('click', function(event){
		event.preventDefault();
		scrollWindow( -1, $(this) );
	});
	$("#navigate-right").bind('click', function(event){
		event.preventDefault();
		scrollWindow( 1, $(this) );
	});
	
	function manageButtons( a,b,c, toScroll ) {

		var scrolled = toScroll == undefined ? $window.scrollLeft() : toScroll;
		
		if( scrolled <= 0 ){
	
			$("#navigate-left").addClass('disabled');
			$("#navigate-right").removeClass('disabled');
		}
		else if( scrolled + 50 >=  maxScroll ){
			$("#navigate-right").addClass('disabled');
			$("#navigate-left").removeClass('disabled');
		}
		else
		{	
			$("#navigate-left, #navigate-right").removeClass('disabled');
		}
	}

	$window.bind('scroll', manageButtons);
	
	// Ie7 needs body to work with mousewheel
	$('body').bind('mousewheel', function(event, delta) {
		scrollWindow(delta / -12 , undefined, true);
	});

	// Recalcuate if the buttons need to be disables for the first second
	var manageButtonsIntervalCount = 0;
	var manageButtonsInterval = window.setInterval(function(){
		manageButtonsIntervalCount++;
		if( manageButtonsIntervalCount > 20) window.clearInterval(manageButtonsInterval);
		manageButtons();
	}, 100 );
	
	// disable left by default
	manageButtons();

});
