function intval( mixed_var, base ) {    // Get the integer value of a variable
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 
    var tmp;
 
    if( typeof( mixed_var ) == 'string' ){
        tmp = parseInt(mixed_var);
        if(isNaN(tmp)){
            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if( typeof( mixed_var ) == 'number' ){
        return Math.floor(mixed_var);
    } else{
        return 0;
    }
}

$(document).ready(function(){
	/*
	$("ul.menu li").bind("mouseover", function(){
		$(this).css('opacity', 0.1);
		$(this).addClass('active').stop().animate({opacity: 1}, 1000);
	});
	
	$("ul.menu li").bind("mouseleave", function(){
		$(this).removeClass('active');
	});
	*/
});

var slider_cur = 0;

$(function() {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	
	$(".top0 .country-sel a").attr("href", "javascript:void(0)");
	$(".top0 .country-sel a").bind("click", function(){
		$("#select-country-dialog").dialog("destroy");
	
		$("#select-country-dialog").dialog({
			height: 140,
			modal: true
		});
	});
	
	$(".slider_ctl #next").bind("click", function(){
		var cnt = $("#slider .container .item").length;
		
		$(".slider_ctl").attr("stop", "true");
		$(".slider_ctl").stopTime();
		$(".slider_ctl a#pause img").attr('src', 'i/pause-hover.png');
		$(".slider_ctl a#pause img").attr('title', 'Продолжить');
		$(".slider_ctl a#pause").attr('title', 'Продолжить');

		$('.slider_ctl a#pause').poshytip({
			className: 'tip-yellowsimple',
			showOn: 'hover',
			alignTo: 'target',
			alignX: 'center',
			alignY: 'bottom',
			offsetX: 5
		});
			
		slider_cur++;
		if (slider_cur >= cnt) slider_cur = 0;
		
		var l = -Math.round(1000) * slider_cur;
		
		if (slider_cur != 0) $("#slider .container").animate({left: l}, 1000);
		else
		{
			$("#slider .container").fadeOut(500, function(){
				$("#slider .container").css('left', 0).fadeIn(500);
			});
		}
	});
	
	$(".slider_ctl #back").bind("click", function(){
		var cnt = $("#slider .container .item").length;
		
		$(".slider_ctl").attr("stop", "true");
		$(".slider_ctl").stopTime();
		$(".slider_ctl a#pause img").attr('src', 'i/pause-hover.png');
		$(".slider_ctl a#pause img").attr('title', 'Продолжить');
		$(".slider_ctl a#pause").attr('title', 'Продолжить');
		
		$('.slider_ctl a#pause').poshytip({
			className: 'tip-yellowsimple',
			showOn: 'hover',
			alignTo: 'target',
			alignX: 'center',
			alignY: 'bottom',
			offsetX: 5
		});

		slider_cur--;
		if (slider_cur < 0) slider_cur = cnt - 1;
	
		var l = -Math.round(1000) * slider_cur;
		$("#slider .container").animate({left: l}, 1000);
	});
	
	$(".slider_ctl #pause").bind("click", function(){
		if ( $(".slider_ctl").attr("stop") == "true")
		{
			$(".slider_ctl").attr("stop", "false");
			$(this).attr('title', 'Пауза');
			
			$('.slider_ctl a').poshytip({
				className: 'tip-yellowsimple',
				showOn: 'hover',
				alignTo: 'target',
				alignX: 'center',
				alignY: 'bottom',
				offsetX: 5
			});
			
			$(".slider_ctl").everyTime(4000, function(i) {
				var l = Math.round(intval($("#slider .container").css("left"))) - Math.round(1000);
				
				if (l >= -1000) $("#slider .container").animate({left: l}, 1000);
				else $("#slider .container").animate({left: 0}, 1000);
			});
		}
		else
		{
			$(".slider_ctl").attr("stop", "true");
			$(".slider_ctl").stopTime();
			$(this).attr('src', 'i/back-hover.png');
			$(this).attr('title', 'Продолжить');
			
			$('.slider_ctl a').poshytip({
				className: 'tip-yellowsimple',
				showOn: 'hover',
				alignTo: 'target',
				alignX: 'center',
				alignY: 'bottom',
				offsetX: 5
			});
		}
	});
	
	$(".slider_ctl").everyTime(4000, function(i) {
		var cnt = $("#slider .container .item").length;
		if (cnt <= 1) return;
		
		slider_cur++;
		if (slider_cur >= cnt) slider_cur = 0;
		var l = -Math.round(1000) * slider_cur;
		
		if (slider_cur != 0) $("#slider .container").animate({left: l}, 1000);
		else
		{
			$("#slider .container").fadeOut(500, function(){
				$("#slider .container").css('left', 0).fadeIn(500);
			});
		}
	});
	
	$(".slider_ctl a#back img").bind("mouseover", function(){
		$(this).attr('src', 'i/back-hover.png');;
	});
	
	$(".slider_ctl a#back img").bind("mouseleave", function(){
		$(this).attr('src', 'i/back.png');
	});
	
	$(".slider_ctl a#next img").bind("mouseover", function(){
		$(this).attr('src', 'i/next-hover.png');;
	});
	
	$(".slider_ctl a#next img").bind("mouseleave", function(){
		$(this).attr('src', 'i/next.png');
	});
	
	$(".slider_ctl a#pause img").bind("mouseover", function(){
		$(this).attr('src', 'i/pause-hover.png');;
	});
	
	$(".slider_ctl a#pause img").bind("mouseleave", function(){
		if ($(".slider_ctl").attr("stop") != "true") $(this).attr('src', 'i/pause.png');
	});
	
	// pushy
	$('.slider_ctl a').poshytip({
		className: 'tip-yellowsimple',
		showOn: 'hover',
		alignTo: 'target',
		alignX: 'center',
		alignY: 'bottom',
		offsetX: 5
	});
	
	// select country
	$('.top0 .country-sel a').poshytip({
		className: 'tip-yellowsimple',
		showOn: 'hover',
		alignTo: 'target',
		alignX: 'center',
		alignY: 'bottom',
		offsetX: 5
	});
	
	$("div#slider div#box").bind("mousedown", function(){
		//console.log("down");
		alert(123);
	});
});

// chat

function chat_sendmsg()
{
	$("#chat_tabs #text").effect( "highlight", null, 500);
	$("#chat_tabs #email").effect( "highlight", null, 500);
	
	if ($("#chat_tabs #email").val().length > 0 && $("#chat_tabs #text").val().length > 0)
	{
		$.post("/ajax/email_send.php", {email: $("#chat_tabs #email").val(), text: $("#chat_tabs #text").val() }, function(){
			$("#chat_tabs #text").val("");
			$("#chat_tabs #email").val("");
			
			$("#chat_tabs #send_status").fadeIn(200).delay(3000).fadeOut(200);
		});
	}
}

$(document).ready(function(){
	$("#chat_tabs").tabs();
	
	$("#chat_tabs #submit").button();
	
	$("#chat_tabs #submit").bind("click", function(){
		chat_sendmsg();
	});

	/*$("#chat_tabs #text").bind("keypress", function(e){
		if ((e.keyCode || e.which) == 13) chat_sendmsg();
	});*/

	// инициализируем блок сообщений
	//$("#chat_msgs").load("/ajax/icq.php");
	//$("#chat_history").load("/ajax/chat_history.php");
	
	// сообщаем менеджеру что на сайт зашел посетитель
	/*$("body").append("<div id='123809gh192f' style='hidden'></div>");
	$("#123809gh192f").load("/ajax/icq2manager.php", {title: $("title").text()});*/

	// ставим автообновление чата
	/*$("#chat_msgs").everyTime(5000, function(i) {
		$("#chat_msgs").load("/ajax/icq.php");
		$("#chat_history").load("/ajax/chat_history.php");
	});*/
});
