$(function () {
			
	if($('#servicesList').length){jQuery('#servicesList').jcarousel({ vertical: true, scroll: 3});}
	if($('#seekHelpList').length){jQuery('#seekHelpList').jcarousel({ vertical: true, scroll: 3});}
	if($('#centersGallery').length){jQuery('#centersGallery').jcarousel({scroll: 1,initCallback: mycarousel_initCallback})}
	
	if ( $('select').length ){$("select").selectbox();}
	
	$(function(){ $('fieldset > input').checkBox();});
	$(function(){ $('#servList1').jScrollPane({showArrows:true, scrollbarWidth:19, dragMinHeight:19, dragMaxHeight:58});});
	$(function(){ $('#servList2').jScrollPane({showArrows:true, scrollbarWidth:19, dragMinHeight:19, dragMaxHeight:58});});
	$(function(){ $('#servList3').jScrollPane({showArrows:true, scrollbarWidth:19, dragMinHeight:19, dragMaxHeight:58});});
	$(function(){ $('#servList4').jScrollPane({showArrows:true, scrollbarWidth:19, dragMinHeight:19, dragMaxHeight:58});});
	
	if ( $('#serviceTabs').length ){$("#serviceTabs > ul").tabs({fxFade: true});}
	if ( $('#medCentr').length ){$("#medCentr > ul").tabs({fxFade: true});}
	if ( $('#sidemenu').length ){
		$("#sidemenu > ul").tabs({fxFade: true});
		$("#sidemenu .ui-tabs-nav li:last").addClass('last');
	}	

	if($('.func').length){
		$('.func').hover(function(){$(this).addClass('hovered')}, function(){$(this).removeClass('hovered')});
	}
	if($('#reception .func').length) {
		$('#reception .func').click(function(){
			$(this).parent().css('display','none');
			$('#reception .forgot').css('display','block');
			$('#card_required_sign').css('display','none');
			$('#small_view').val('0');
		});
	}
	
	$(".accordion h3:first").css("background-image", "none");
	$(".accordion h3").hover(function(){$(this).addClass("hovered");}, function(){$(this).removeClass("hovered");});	
	$(".accordion div").hide();
    $(".accordion h3").click(function(){
        $(this).next("div").slideToggle(500)
        .siblings("div:visible").slideUp(500);
        $(this).toggleClass("active");
        $(this).siblings("h3").removeClass("active");
		if ($("#sidemenu .accordion h3:first").hasClass("active")) { 
			$("#sidemenu .accordion h3:first").css("background", "#fff url(/images/acc_dots.gif) repeat-x left top");
		}
		else {
			$("#sidemenu .accordion h3:first").css("background", "none");
		}
     });
    
    $(".accordion ul").hide();
    $(".accordion h5:not(.black)").hover(function(){$(this).addClass("hovered");}, function(){$(this).removeClass("hovered");});
    $(".accordion h5:not(.none)").click(function(){ 
    	$(this).next("ul").slideToggle(500) 
    	.siblings("ul:visible").slideUp(500); 
    	$(this).toggleClass("active"); 
    	$(this).siblings("h5").removeClass("active"); 
    });
    
    //below line must be commented
	//$("div.innertabs_wrap div.innertabsdiv1").hide();
	$("div.innertabs_wrap ul.innertabs li:not(.activated)").hover(function(){$(this).addClass("hovered");},function(){$(this).removeClass("hovered");});
	$("div.innertabs_wrap ul.innertabs li:not(.appoint)").click(function(){
		var index = $("div.innertabs_wrap ul.innertabs li").index(this);
		if ($("div.innertabs_wrap div.innertabsdiv1").eq(index).css("display") == 'block') {
			$("div.innertabs_wrap div.innertabsdiv1").eq(index).hide("slow");
			$(this).removeClass("activated");
		}
		else {
			$("div.innertabs_wrap div.innertabsdiv1").hide("slow");
			$("div.innertabs_wrap div.innertabsdiv1").eq(index).show("slow");
			$("div.innertabs_wrap ul.innertabs li.activated").removeClass("activated");
			$(this).addClass("activated");
			$("div.innertabs_wrap ul.innertabs li").each(function(){
				if ($(this).is(":first-child") ) {
					$(this).css("padding-bottom", "6px");
				}
			});
		}
	});
	
	
	$("div.hint .close").click( function(){
		var name = $(this).parent().get();
		$( name).css('visibility','hidden');		
	});
	
	$("#gallery li a").bind("click", function(){
		if($("#map").css("visibility") == 'visible') {
			$("#map").css("visibility","hidden"); 
		}									  
		if($("#gallery_prev").css("visibility") == 'hidden') {			
			show_Hint('gallery_prev');			
		}	
	});
	$("#partners li").hover(function(){ 
		$(this).css("background","url(/images/partners_frame.gif) no-repeat 7px 7px");
		var index = $("#partners li").index(this);
		
		if ($(this).offset().left < 239) {			
			$("#tip_wrap div.tip").eq(index).css("left", $(this).offset().left+121);
			$("#tip_wrap div.tip").eq(index).addClass("turned");
		}
		else {
			$("#tip_wrap div.tip").eq(index).css("left", $(this).offset().left-232);
		}
		$("#tip_wrap div.tip").eq(index).css("top", $(this).offset().top+4);
		$("#tip_wrap div.tip").eq(index).css("visibility","visible");
	},
	function(){
		var index = $("#partners li").index(this);
		$("#tip_wrap div.tip").eq(index).css("visibility","hidden");
		if($("#tip_wrap div.tip").eq(index).hasClass("turned")) {
				$("#tip_wrap div.tip").eq(index).removeClass("turned")
		}
		$(this).css("background","none");
	});
	if ($('.sRes_catwrap').length ) {
		$('.sRes_catwrap').each(function(){me_select(this)});
		$('.w_inp').each(function(){
		  $(this).click(function(){
			$(this).find('div')[0].show_me();
		  })
		});
		$('.moreButton').hover(
			function(){$(this).css('background-position','left bottom');},
			function(){$(this).css('background-position','left top');}
		);
		//$(".sRes_catwrap div").hide();
		//$(".sRes_catwrap p").click(function(){
		//		$(this).next("div").slideToggle("slow");
		//		$(this).toggleClass("active");			 
		// });
	}

	if($("#city_list").length) { 
		$("#city_list li").hover(function(){if(!($(this).hasClass("first") || $(this).hasClass("last") || $(this).hasClass("selected"))){$(this).addClass("hovered");}},function(){$(this).removeClass("hovered");}); 
		var index = -1; 
		$("#city_list li").click( 
		function(){ 
			if(!($(this).hasClass("first") || $(this).hasClass("last"))){ 
					if (!$(this).hasClass("selected")) { 
							var ctname = $(this).text(); 
							$("#city span").text(ctname); 
							$("#city_list li").each(function(){ if($(this).hasClass("selected")){$(this).removeClass("selected");}}); 
							$(this).addClass("selected"); 
					} 
			} 
			// it's a ajax call for change cities districts. Code location w3t_site_logic/tpl_main.php
			var index = $(this).attr("id");
			xajax_doAjaxCityChange(index);			
		}); 
		}
        
        
   //initialize function for click event on fc form
   var links = $(".fc_input");
   links.each( function() {
      $(this).bind("click", {elem: $(this)}, dofc_ServiceItemClick);
   })  
   
   //initialize function for click event on c form
   var links = $(".c_input");
   links.each( function() {
      $(this).bind("click", {elem: $(this)}, doc_ServiceItemClick);
   })           
   
            
});
//--------------------------------------------------------------------------------------------------------
function refreshServSelect(){
	$(".sRes_catwrap div").hide();
	$(".sRes_catwrap p").click(function(){
			$(this).next("div").slideToggle("slow");
			$(this).toggleClass("active");			 
	 });
};

function showCityList(theid){							 
	var coordX1 = $('#' + theid).offset().top + 17;
	var coordY1 = $('#' + theid).offset().left - 7;	
	var hght = $('#city_list .mid').height();
	$('#city_list').css('top', coordX1);
	$('#city_list').css('left', coordY1);
	$('#city_list').show(500);
	mouseTrace('city_list');
};

function mouseTrace(block_id) {
	var coordX1 = $('#' + block_id).offset().left;
	var coordY1 = $('#' + block_id).offset().top;
	var coordX2 = $('#' + block_id).offset().left + $('#' + block_id).width();
	var coordY2 = $('#' + block_id).offset().top + $('#' + block_id).height();	
	var coordClickX = -1;
	var coordClickY = -1;
	$().click(function(e){
      	coordClickX = e.pageX;
	  	coordClickY = e.pageY;
		if (!((coordClickX > coordX1 )&&(coordClickX < coordX2 )&&(coordClickY > coordY1)&&(coordClickY < coordY2))) {
			$('#'+block_id).hide(500);
		}
	});	
};

function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
};

function show_Hint(the_id) {	
	var doc = $(document).height();
	var wind = $(window).height();	
	var scrolled = getScrollY() + 50;
	var my_height = $("#" + the_id).height();
	var wind_width = $(window).width();
	var my_width = $("#" + the_id).width();
	var lft = (wind_width/2) - (my_width/2);
	if ((doc - (scrolled + my_height)) > 0) {
		$('#' + the_id).css("top",scrolled);
	} else {
		$('#' + the_id).css("top",doc - my_height);
	}
	$('#' + the_id).css("left",lft);
	$('#' + the_id).css("visibility","visible");	
	if(($("#gallery_prev").length) && ($("#gallery_prev").css("visibility") == "visible") && (the_id == "map")) {
		$("#gallery_prev").css("visibility","hidden");
	}
}

// if window is scrolled, px
function getScrollY() 
{
    scrollY = 0;    
    if (typeof window.pageYOffset == "number") {
        scrollY = window.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;
    }  else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop; 
    } else if (window.scrollY) {
        scrollY = window.scrollY;
    }
    return scrollY;
}

// if window is scrolled, px
function getScrollX()
{
    scrollX = 0;
    if (typeof window.pageXOffset == "number") {
        scrollX = window.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        scrollX = document.documentElement.scrollLeft;
    }  else if (document.body && document.body.scrollLeft) {
        scrollX = document.body.scrollLeft;
    } else if (window.scrollX) {
        scrollX = window.scrollX;
    }
    return scrollX;
}

function me_select (el,param){
	var self=$(el)[0]; 
    if(!param) param={};
    else if(typeof(param)=='function')
        param={show:param};
        
    function checkMouse (e){
        var el = e.target;
        while (true){
            if (el == self) {
                return true;
            } else if (el == document) {
                hide_me();
                return false;
            } else {
                el = el.parentNode;
            }
        }
    };
    function show_me(){
        if(param.show) param.show.apply(self);
        else $(self).show();
        $(document).bind('mousedown', checkMouse);
        return false;
    };  
    function hide_me(){
        $(document).unbind('mousedown', checkMouse);
        if(param.hide)
            param.hide.apply(self);
        else
            $(self).hide();
        return false;
    };
    self.show_me=show_me;
    self.hide_me=hide_me;
    $(window).bind('unload',function(){self=null});
}

function get_updatedSelectedServices(elem, cur_serv_value, cur_value, prefix){
	
	var result = cur_value;
	var li_id = "li_"+cur_value;
	
	if ($(elem).attr("checked")) {
		if (cur_serv_value.indexOf(cur_value) == -1){
			result = cur_serv_value + "#" + cur_value;
			
			var visible_elem = '<li id="'+li_id+'">'+$(elem).next("label").text()+'</li>';
			$(visible_elem).appendTo("#"+prefix+"order_list_service");			
		}		
	} else {
		result = cur_serv_value.replace("#" + cur_value, "");

		var full_li_id = "#"+li_id;
		$(full_li_id).remove();
	}
	
	return result;
}

function dofc_ServiceItemClick(eventData){
	var elem = eventData.data.elem;
	var cur_serv_value = $("#fc_services").val();
	var cur_value = $(elem).attr("id");
	
	new_value = get_updatedSelectedServices(elem, cur_serv_value, cur_value, 'fc_');
	
	$("#fc_services").val( new_value );
}

function doc_ServiceItemClick(eventData){
	var elem = eventData.data.elem;
	var cur_serv_value = $("#c_services").val();
	var cur_value = $(elem).attr("id");

	new_value = get_updatedSelectedServices(elem, cur_serv_value, cur_value, 'c_');

	$("#c_services").val( new_value );
}
