$(document).ready(function(){

	var sm_flag = 0,
		sm_ctr = 0,
		sm_left = 0;

	//Get our elements for faster access and set overlay width
	var div_outer = $('div#navTopSubMenuDiv'),
		div_outer2 = $('div#navTopSubMenuDivIn2'),
		div = $('div.sc_menu'),
		arr = $('div#arra'),
		arr2 = $('div#arra2'),
		ul = $('ul.sc_menu'),
		ulPadding = 20;

	//var wd = div_outer.width();
	var wd = $(window).width() - 335;
	//alert(wd);
	div.css("width", (wd - 100) + "px");

	
	//Get menu width
	//var divWidth = div.width();

	//Remove scrollbars	
	div.css({overflow: 'hidden'});
	
	//Find last image container
	var lastLi = ul.find('li:last-child'),
		elem_arr = new Array();

	$("li", ul).each(function(e){
		elem_arr[e] = $(this).width() + 20;
	});

	//var ccc = "";
	var wd_sc_menu = 0;
	for (i in elem_arr){
		//ccc+= "this " + i + "-->" + elem_arr[i];
		wd_sc_menu+= elem_arr[i];
	}
	//alert(ccc);


	function content_resize(){
		/*var w = $( window );
		var H = w.height();
		var W = w.width();
		$( '#content' ).css( {width: W-20, height: H-20} );*/

		wd = $(window).width() - 335;
		if (wd <= 660) wd = 660;

		//wd = div_outer2.width();
		$("div.sc_menu").css("width", (wd - 100) + "px");
		//$("div.fixedwidth").css("width", wd_sc_menu + "px");
		$("div.fixedwidth").css("width", (wd_sc_menu - 20) + "px");
		div_outer.css("width", wd + "px");
		//alert("Stop it!" + wd);
	}
/*
	$(window).resize(function(){
		wd = $(window).width() - 335 - 100;
		$("div.sc_menu").css("width", wd + "px");
		div_outer.css("width", wd + "px");
	});
*/

	$(window).wresize( content_resize );
	content_resize();

	function sharr(){
		if (!sm_left) arr2.css("display", "none");
		else arr2.css("display", "block");
	}

	function sharr2(){
		if ((lastLi[0].offsetLeft - sm_left) < wd) arr.css("display", "none");
		else arr.css("display", "block");
	}

	function elctr(){
		if (!sm_left)arr2.css("display", "none");
		else arr2.css("display", "block");
	}

	function mmove(){
		//alert(sm_ctr + "-->" + sm_left);
		
		sm_left+= elem_arr[sm_ctr];
		div.scrollLeft(sm_left);

		sm_ctr++;
	}

	function mmove2(){

		--sm_ctr;

		//alert(sm_ctr + "-->" + sm_left);
		sm_left = sm_left - elem_arr[sm_ctr];
		div.scrollLeft(sm_left);
		sharr2();
	}

	function mmfove(){
		if (!sm_flag) mmove();
		else mmove2();

		sharr();
		sharr2();
	}

	sharr();

	//alert(lastLi[0].offsetLeft);
	/*$(window).resize(function(){
		alert("Stop it!");
	});*/

	// When user click arrows
	arr.mouseover(function(){
		$(this).css("cursor", "pointer");
	});
	arr2.mouseover(function(){
		$(this).css("cursor", "pointer");
	});
	arr.click(function(){
		sm_flag = 0;
		mmfove();
		return false;
	});
	arr2.click(function(){
		sm_flag = 1;
		mmfove();
		return false;
	});

	/*div.mousemove(function(e){*/
		//As images are loaded ul width increases,
		//so we recalculate it each time
		/*var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;	
		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		div.scrollLeft(left);
	});*/
});
