var obj = null;
var timeout = 500;

function hideMenu() {
	if (obj) {
		obj.find('ul').hide();					
		obj = null;					
	}
}

function showMenu() {
	if (obj) {
		obj.find('ul').show();
		obj = null;
	}
}


var width = 0;
var visible_width = 0;
var start_offset = 0;
function makeCarousel() {
	// make width of carousel block
	width = $("li", "#carousel > div > ul").size()*140-15;
	visible_width = $("#carousel > div.carousel-horisontal").width();
	start_offset = $("#carousel > div.carousel-horisontal").offset();
	start_offset = start_offset.left;
	
	$("#carousel > div > ul").width(width+15);
	$("#carousel > div.scroll-left").bind('click', function(){
		offset = $("#carousel > div > ul").offset();
		left = offset.left - start_offset;
		if (left < -140) scroll_ = 140;
		else scroll_ = -left;
		if (left < 0) $("#carousel > div > ul").animate({"left": "+="+scroll_+"px"}, "slow");
	});
	$("#carousel > div.scroll-right").bind('click', function(){
		offset = $("#carousel > div > ul").offset();
		left = offset.left - start_offset;
		scroll_ = width + left - visible_width;
		if (scroll_ > 140) scroll_ = 140;
		$("#carousel > div > ul").animate({"left": "-="+scroll_+"px"}, "slow");
	});
}

var slide = 1;
var slides = 3;
var oldslide = 1;
function SliderSet() {
	$("#slider").stopTime('SliderTimer');
	$("#slider").everyTime(5000, 'SliderTimer', function(i) {
		$(this).find("> #slider-image > div#slide-"+slide).fadeOut("slow");
		$(this).find("> .slider-bullets > a.bullet-"+slide+" > img").attr("src","/img/slider-bullet.jpg");
		slide++;
		if (slide>slides) slide=1;
		$(this).find("> #slider-image > div#slide-"+slide).fadeIn("slow");
		$(this).find("> .slider-bullets > a.bullet-"+slide+" > img").attr("src","/img/slider-bullet-active.jpg");
		oldslide = slide;
	});
}

var cur_phones = 1;
$(document).ready(function() {	
	$(document).pngFix();
	$('#phones div.menu ul li.more ul').hide();

	// apply bgiframe if available to fix IE6 Select Overlap Bug
	if ($.fn.bgiframe){
		$('#phones div.menu ul li.more ul').bgiframe();
	}
	$('#phones div.menu ul li.more').hover(function() {		
		hideMenu();
		obj = $(this);					
		showMenu();
	}, function() {
		obj = $(this);
		setTimeout("hideMenu()",timeout);
	});
	
	$("td", "table.makeheight").each(function(){
		$(this).find("> div > div").height($(this).height()-20);
	});
	
	if ($('#carousel').length != 0) makeCarousel();
	
	//Убираем слайдер, на замену ему флэшка
/*	SliderSet();

	$("#slider > .slider-bullets > a").bind("click", function(){
		$("#slider").stopTime('SliderTimer');
		$("#slider > #slider-image > div#slide-"+oldslide).fadeOut("slow");
		$("#slider > .slider-bullets > a.bullet-"+oldslide+" > img").attr("src","/img/slider-bullet.jpg");
		_oldslide = $(this).attr("class");
		slide = oldslide = _oldslide.substr(7,1);
		$("#slider > #slider-image > div#slide-"+slide).fadeIn("slow");
		$("#slider > .slider-bullets > a.bullet-"+slide+" > img").attr("src","/img/slider-bullet-active.jpg");
	});*/
	
// Phones
	$("a", "#phones > .menu").bind("click", function(){
		id = $(this).attr("class");
		if (id.length == 0) return false;
		id = id.substr(5,2);

		$("#phones-"+cur_phones).addClass("hidden");
		$("#phones > .menu a.city-"+cur_phones).parent().removeClass("active");
		
		cur_phones = id;
		$("#phones-"+cur_phones).removeClass("hidden");
		$("#phones > .menu a.city-"+id).parent().addClass("active");
	});
	
	$("input.date").datepicker({
		showOn: 'button',
		buttonImage: 'img/date-icon.jpg',
		buttonImageOnly: true,
		dateFormat: 'dd-mm-yy',
		locale: 'ru'
	});

// Для раздела FAQ
    $('.faq').delegate('.faq_q', 'click', function(){
        var q = this;
        var a = $('.faq_a', this.parentNode);
        if($(a).css('display') == 'none')
          $(a).slideDown(200);
        else
          $(a).slideUp(200);
    });
});

//Лого
var flashvars = {};
var params = {
  wmode: "opaque"
};
swfobject.embedSWF("/flash/life-in-turkey.swf", "up_logo", "572", "130", "9.0.0", "/flash/expressInstall.swf",flashvars, params);

//Tour price table
$(document).ready(function() {
	$(".showTourPrice").toggle(
	function () {
	  $(".tableTourPrice").slideDown("slow");
	  $(".showTourPrice").text("Скрыть часть туров");
	  return false;
	},
	function () {
	  $(".tableTourPrice").slideUp("slow");
	  $(".showTourPrice").text("Показать больше туров");
	  return false;
	}
	);
});
