var focus = 0;
$(document).ready(function(){
	resizeButtons();
	$("#right_buttons_min div").hover(function(){
		$(this).animate({ "right":"-1px" }, 200 );
    }, function(){
        $(this).animate({ "right":"-21px" }, 200 );
    });
	
	$(".input_line input, .textarea_line textarea").focus(function(){
		focus = 1;
	});
	$(".input_line input, .textarea_line textarea").blur(function(){
		focus = 0;
	});

	$("#header .top_menu .top_menu_left ul li.first").hover(
		function(){
			if (!$(this).find("a").hasClass("root-item-selected"))
				//$("#header .top_menu").addClass("top_menu_act");
				$("#header .top_menu").css("background", "url(/images/bg_menu_left_act.png) left top no-repeat");
		},
		function(){
			//$("#header .top_menu").removeClass("top_menu_act");
			$("#header .top_menu").css("background", "url(/images/bg_menu_left.png) left top no-repeat");
			
		}
	)
	var params = {
        changedEl: "form select",
        visRows: 5,
        scrollArrows: true
    }
    cuSel(params);
	//$('#faqForm').hide();
	//$('#requestForm').hide();
	
	$('form.ajax_form').submit(function(){
		var rel = $(this).attr('rel');
		var form = this;
		$.post($(this).attr('action'),$(this).serialize(),function(data){
			if ($(data).find('div.error_message[rel='+rel+']').size()>0){
				$(form).parent().find('.error_message').remove();
				$(form).parent().find('.success_message').remove();
				$(form).find('img[alt=CAPTCHA]').attr('src',$(data).find('form.ajax_form[rel='+rel+'] img[alt=CAPTCHA]').attr('src'));
				$(form).find('input[name=captcha_sid]').val($(data).find('form.ajax_form[rel='+rel+'] input[name=captcha_sid]').val());
				$(form).before($(data).find('div.error_message[rel='+rel+']').eq(0));
			}
			if ($(data).find('div.success_message[rel='+rel+']').size()>0){
				$(form).parent().find('.error_message').remove();
				$(form).parent().find('.success_message').remove();
				$(form).before($(data).find('div.success_message[rel='+rel+']').eq(0));				
				$(form).hide();
			}
		});
		return false;
	});
	
	if ($(".scrollable").size()>0)
		var horizontal = $(".scrollable").scrollable({ circular: true, vertical: true, speed:1410 }).navigator(".navi").autoscroll({ autoplay: true });
		//var horizontal = $(".scrollable").scrollable({ circular: true, vertical: true, speed:1410 }).navigator(".navi");
		
	$(".show_menu").click(function() {
		$(this).parent().next().slideToggle(300);
	})
	
	if ($(".scroll-bar").size()>0)
		initSlider();
});

$( window ).resize(function() {
	resizeButtons();
});

function resizeButtons () {
	var h = $(window).height();
	var w = $(window).width();
	
	if (w <= 1024) {
		if (!$("#right_buttons").hasClass("hide")) {
			$("#right_buttons").addClass("hide");
			$("#right_buttons_min div").css("right", "-21px");
		}
	}
	else {
		if ($("#right_buttons").hasClass("hide")) {
			$("#right_buttons").removeClass("hide");
			$("#right_buttons_min div").css("right", "-51px");
		}
	}
}

var slideInterval = null;

function initSlider () {
	//scrollpane parts
	var scrollPane = $( ".scroll-pane" ),
		scrollContent = $( ".scroll-content" );
	
	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});
	
	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
		scrollbar.width( handleHelper.width() );
	})
	.mouseup(function() {
		scrollbar.width( "100%" );
	})
	.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
	.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
	
	//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 );
			}
	}
	
	//change handle position on window resize
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	
	function moveLeft() {
		var cont_marg = parseInt( scrollContent.css( "margin-left" ), 10 );
		cont_marg = cont_marg + 20;
		var remainder = scrollPane.width() - scrollContent.width();
		if (cont_marg > 0)
			cont_marg = 0;
		if (cont_marg <= 0 && cont_marg >= remainder) {
			scrollContent.css( "margin-left", cont_marg + "px" );
			resetValue();
		}
	}
	
	function moveRight() {
		var cont_marg = parseInt( scrollContent.css( "margin-left" ), 10 );
		cont_marg = cont_marg - 20;
		var remainder = scrollPane.width() - scrollContent.width();
		if ((remainder - cont_marg) > -20)
			cont_marg = cont_marg + (remainder - cont_marg);
		if (cont_marg <= 0 && cont_marg >= remainder) {
			scrollContent.css( "margin-left", cont_marg + "px" );
			resetValue();
		}
	}
	
	$(".scroll_left").click(function(){
		moveLeft();
		return false;
	})
	$(".scroll_left").mousedown(function(){
		clearInterval(slideInterval);
		slideInterval = setInterval(function(){moveLeft()}, 30);
	}).mouseup(function(){
		clearInterval(slideInterval);
	}).mouseleave(function(){
		clearInterval(slideInterval);
	})
	$(".scroll_right").click(function(){
		moveRight();
		return false;
	})
	$(".scroll_right").mousedown(function(){
		clearInterval(slideInterval);
		slideInterval = setInterval(function(){moveRight()}, 30);
	}).mouseup(function(){
		clearInterval(slideInterval);
	}).mouseleave(function(){
		clearInterval(slideInterval);
	})
}

function thickbox_cusel () {	
	if ($('#TB_window form select').size()>0) {
		var params = {
			changedEl: "#TB_window form select",
			visRows: 5,
			scrollArrows: true
		}
		cuSel(params);
	}
}
