$(document).ready(function() {

	if($("#accordion").length > 0) {
		$("#accordion dt").click(function() {

			var sel = $(this);
			$(this).find("h2").addClass("active");

			// add class to the element
			// to indicate current selector
			sel.addClass("act current");

			// loop through all dd elements to slide them up
			// except the current one in case we simply want to slide it up
			sel.parent().children("dd").each(function() {
				if($(this).is(":visible") && !$(this).prev("dt").hasClass("current")) {
					if($(this).prev("dt").hasClass("act")) {
						$(this).prev("dt").removeClass("act").find("h2").removeClass("active");
					}
					$(this).slideUp(300);
				}
			});

			// toogle slide and remove class from the current selector
			sel.next().slideToggle(300, function() {
				if(!$(this).is(":visible")) {
					$(this).prev("dt").removeClass("act").find("h2").removeClass("active");
				}
				sel.removeClass("current");
			});

			return false;

		});
	}

	init_swap_text_boxes();

	//		$(".login-button").click(function () {
	//				$(this).toggleClass('active');
	//				$("#login-form").slideToggle();
	//		});

			$(".legal-headline").click(function () {
					$(".legal").slideToggle();
			});

			var ulWidth=0;
			var subaWidth=0;
			$(".menu ul ul").each(function(){
				var mainaWidth = $(this).parent().children().width() / 2;
				$(this).find('a').each(function(){
					subaWidth += $(this).html().length * 8;
				});
				ulWidth += subaWidth;
				margin = ulWidth / 5 - mainaWidth;

				$(this).css({width: ulWidth+'px', marginRight: '-'+margin+'px'});
				ulWidth = 0;
				subaWidth = 0;
				mainaWidth = 0;
			});

			//menu !!!
			//$(".menu ul li a:first").addClass("current");
			//$(".menu ul ul:first").addClass("sub-menu");
			//$(".menu ul ul a:first").addClass("current-green");

			$(".menu li.menuactive ul").addClass("sub-menu");

			var current = $(".menu .current");
			var subMenu = $(".menu .sub-menu");
			$(".menu ul li.parent").hover(function() {
				$(".menu ul li a").removeClass("current");
				$(this).find("a:not('.menu ul ul a')").addClass("current");
				$(".menu ul ul").removeClass("sub-menu");
				$(this).find("a:not('.menu ul ul a')").parent().find("ul").addClass("sub-menu");
		/*	},function() {
				$(".menu ul li a").removeClass("current");
				$(current).addClass("current");
				$(".menu ul ul").removeClass("sub-menu");
				$(subMenu).addClass("sub-menu"); */

				 },

				  function () {
						$(this).children().removeClass("sub-menu");
						$(this).find("a:not('.menu ul ul a')").removeClass("current");
						$(".menu li.menuactive ul").addClass("sub-menu");
			});

			// Slider with clickable title
			$('.slider .items').cycle({
				fx:'fade',
				timeout:5000,
				fit:1,
				before:function(){$('.slider .the_graduate a').fadeOut();},
				after:function(currSlideElement, nextSlideElement, options, forwardFlag) {
					var current = nextSlideElement.id;
					var name = $(nextSlideElement).find('img').attr('alt');
					var url = $(nextSlideElement).find('a').attr('href');
					$('.slider .the_text').html('<a href="'+url+'" title="'+name+'">'+name+'</a>');
					
					//$(".colors-image").wrap('<a class="colors-image-a" href="'+url+'" title="'+name+'"></a>');
					
					$(".colors-image-a").attr('href',url);
				},
				pager:'.nav',
				pagerAnchorBuilder: function(idx, slide) {
					return '.nav li:eq(' + idx + ') a';
				}
			});

					/* Custom selectbox */
		$('select').wrap('<div class="select_container" />');
		$('.select_container').attr('id', function(arr){return 'selectbox_' + arr;}).each(function(){
			var cid = $(this).attr('id');
			$(this).find('select').addClass(cid).hide();
			$(this).append('<div class="'+cid+'"><a href="#" class="toggle_select"><span class="selected"></span><span class="arrow">&nbsp;</span></a><ul class="'+cid+'"></ul></div>');
			$('div.'+cid+' ul.'+cid).hide();
			var dvalue = $(this).find('select option:first').html();
			if ($(this).find('select option:selected').length > 0)
				dvalue = $(this).find('select option:selected').html();
			$('div.'+cid+' a.toggle_select .selected').html(dvalue).parent().click(function(event){
				$('div#'+cid).css('borderColor','#ccc');
				event.preventDefault();
				$(this).toggleClass('active');
				$('div.'+cid+' ul.'+cid).slideToggle('fast');
				$('div.'+cid).hover(function(){},function(){
					$('ul.'+cid).slideUp();
					$('div.'+cid+' a.toggle_select').removeClass('active');
					$('div#'+cid).css('borderColor','');
				});
			});

			$(this).find('select option').each(function(){
				var ovalue = $(this).attr('value');
				var otxt = $(this).html();
				$('#'+cid).find('ul').append('<li><a href="#'+ovalue+'">'+otxt+'</a></li>');
			});

			$(this).find('ul li a').click(function(event){
				event.preventDefault();
				var avalue = $(this).attr('href').replace('#','');
				var atxt = $(this).html();
				$('select.'+cid).val(avalue);
				$('select.'+cid).change();
				$('div.'+cid+' a.toggle_select .selected').html(atxt).parent().removeClass('active');
				$('div.'+cid+' ul.'+cid).slideUp();
			});

			var container_width = 200;//$(this).width();
			$(this).find('ul').width(container_width - 2);
			var selected_width = $(this).width() - 46;
			$(this).find('.selected').width(selected_width);
			$(this).find('li:last').addClass('last');
		});

		$(".position h2:odd").addClass("h2-pink");
		$(".position h2:even").addClass("h2-green");

		$("#inner-menus li ul li a.parent:odd").addClass("pink");
		$("#inner-menus li ul li a.parent:even").addClass("green");

		// MODAL for jobs page

			//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();

		//Get h2 (job name) & insert it to the job title
		var jname = $(this).parent().find("h2").html();
		$('input#job-name-label').val(jname);

		//Get the A tag
		var id = $(this).attr('href');

		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();

		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});

		//transition effect
		$('#mask').fadeIn(500);
		$('#mask').fadeTo("normal",0.8);

		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);

		//transition effect
		$(id).fadeIn(1000);

	});

	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();

		$('#mask').hide();
		$('.window').hide();
	});

	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});

old_q='';

	$("#countries").keyup(function(event) {
		// event can be used to block unwanted symbols making request
		if (event.keyCode==13 || event.keyCode==27 || event.keyCode==38 || event.keyCode==40)
			return;
		if (event.keyCode==0){ // show all
			q = "";
		} else {
			q = $(this).val();
			if (q == '' || q == old_q)
				return;
			old_q = q; // to not send same search query equal to last one
		}
		// check "q" for SQL injection
		getSuggestions($('ul#countries_list'),q,'ajax');
	});

	$('ul#countries_list li').live('click',function(){
		$("#countries").val($(this).html());
		$("ul#countries_list").fadeOut('fast');
	});

	$("#country1").keyup(function(event) {
		// event can be used to block unwanted symbols making request
		if (event.keyCode==13 || event.keyCode==27 || event.keyCode==38 || event.keyCode==40)
			return;
		if (event.keyCode==0){ // show all
			q = "";
		} else {
			q = $(this).val();
			if (q == '' || q == old_q)
				return;
			old_q = q; // to not send same search query equal to last one
		}
		// check "q" for SQL injection
		getSuggestions($('ul#countries_list1'),q,'ajax1');
	});

	$('ul#countries_list1 li').live('click',function(){
		$("#country1").val($(this).html());
		$("ul#countries_list1").fadeOut('fast');
	});

	
	$("#country2").keyup(function(event) {
		// event can be used to block unwanted symbols making request
		if (event.keyCode==13 || event.keyCode==27 || event.keyCode==38 || event.keyCode==40)
			return;
		if (event.keyCode==0){ // show all
			q = "";
		} else {
			q = $(this).val();
			if (q == '' || q == old_q)
				return;
			old_q = q; // to not send same search query equal to last one
		}
		// check "q" for SQL injection
		getSuggestions($('ul#countries_list2'),q,'ajax2');
	});

	$('ul#countries_list2 li').live('click',function(){
		$("#country2").val($(this).html());
		$("ul#countries_list2").fadeOut('fast');
	});



	$("#country_sw").keyup(function(event) {
		// event can be used to block unwanted symbols making request
		if (event.keyCode==13 || event.keyCode==27 || event.keyCode==38 || event.keyCode==40)
			return;
		if (event.keyCode==0){ // show all
			q = "";
		} else {
			q = $(this).val();
			if (q == '' || q == old_q)
				return;
			old_q = q; // to not send same search query equal to last one
		}
		// check "q" for SQL injection
		getSuggestions($('ul#countries_list_sw'),q,'ajax_sw');
	});

	$('ul#countries_list_sw li').live('click',function(){
		$("#country_sw").val($(this).html());
		$("ul#countries_list_sw").fadeOut('fast');
	});





	$("#country_se1").keyup(function(event) {
		// event can be used to block unwanted symbols making request
		if (event.keyCode==13 || event.keyCode==27 || event.keyCode==38 || event.keyCode==40)
			return;
		if (event.keyCode==0){ // show all
			q = "";
		} else {
			q = $(this).val();
			if (q == '' || q == old_q)
				return;
			old_q = q; // to not send same search query equal to last one
		}
		// check "q" for SQL injection
		getSuggestions($('ul#countries_list_se1'),q,'ajax_se1');
	});

	$('ul#countries_list_se1 li').live('click',function(){
		$("#country_se1").val($(this).html());
		$("ul#countries_list_se1").fadeOut('fast');
	});




	$("#country_se2").keyup(function(event) {
		// event can be used to block unwanted symbols making request
		if (event.keyCode==13 || event.keyCode==27 || event.keyCode==38 || event.keyCode==40)
			return;
		if (event.keyCode==0){ // show all
			q = "";
		} else {
			q = $(this).val();
			if (q == '' || q == old_q)
				return;
			old_q = q; // to not send same search query equal to last one
		}
		// check "q" for SQL injection
		getSuggestions($('ul#countries_list_se2'),q,'ajax_se2');
	});

	$('ul#countries_list_se2 li').live('click',function(){
		$("#country_se2").val($(this).html());
		$("ul#countries_list_se2").fadeOut('fast');
	});

	$('.tarif .countries-arrow').click(function(){
		var e = jQuery.Event("keyup");
		e.keyCode = 0; // # Some key code value
		$(this).prev().trigger(e);
		
		//if ($("input.countries-class").val() !="") {
		//	//$(this).parent().find('ul').find('li.selected').removeClass('selected');
		//	$(this).parent().find('ul').find('li').each(function(index) {
		//		//alert($(this).text()+"====="+$("input.countries-class").val())
		//		if ($(this).text()==$("input.countries-class").val()) {
		//			$(this).addClass('selected');
		//		}
		//	});
		//}
	});
	$('input.countries-class').keydown(function(event){
		if (event.keyCode==40){ //down arraw
			if ($(this).parent().find('ul').is(':visible')){
				$(this).parent().find('ul').find('li.selected').each(function(){
					if ($(this).next().length != 0)
						$(this).removeClass('selected').next().addClass('selected');
				});
			} else {
				if ($(this).parent().find('ul').find('li').length > 0)
					$(this).parent().find('ul').fadeIn('fast');
			}
		}
		if (event.keyCode==38){ //up arraw
			$(this).parent().find('ul').find('li.selected').each(function(){
				if ($(this).prev().length != 0)
					$(this).removeClass('selected').prev().addClass('selected');
			});
		}
		if (event.keyCode==13){ //Enter
			event.preventDefault();
			if ($(this).parent().find('ul').is(':visible')){
				$(this).parent().find('ul').find('li.selected').trigger('click');
			}
		}
		if (event.keyCode==27){ //Esc
			if ($(this).parent().find('ul').is(':visible')){
				$(this).parent().find('ul').fadeOut('fast');
				old_q='';
			}
		}
	});
	
	$("#countries_list li, #countries_list1 li, #countries_list2 li, #countries_list_se1 li, #countries_list_se2 li, #countries_list_sw li").live('mouseover',function(){
		$(this).addClass('selected')	
	});
	$("#countries_list li, #countries_list1 li, #countries_list2 li, #countries_list_se1 li, #countries_list_se2 li, #countries_list_sw li").live('mouseout',function(){
		$(this).removeClass('selected')	
	});
	
	$(document).keydown(function(event){
		if (event.keyCode==27){ //Esc
			if ($('.tarif ul').is(':visible')){
				$('.tarif ul').fadeOut('fast');
				old_q='';
			}
		}
	});
	$('.tarif ul').hover(function(){
	},function(){
		if ($('.tarif ul').is(':visible')){
			$('.tarif ul').fadeOut('fast');
			old_q='';
		}
	});
	
	/* //   hide suggestion list when out of inbox (problem with scrollbar)
	$('input.countries-class').blur(function(){
		if ($(this).parent().find('ul').is(':visible')){
			$(this).parent().find('ul').fadeOut('fast');
			old_q='';
		}
	});
	*/
	
	$(".above-banner").css({'position':'absolute',
	'top':0,'left':0,
	'z-index':2,
	'cursor':'pointer',
	'background':'transparent',
	'width':'1000px',
	'height':'130px'});

//$(".above-banner").click(function() {
//	self.location = 'http://018.co.il/int-calls/callsto/day-Night';
//});


//	$(".flashplayer").wrap('<div style="position:relative; z-index:0;" /><!-- Look at Functions.js!!! -->');


function getSuggestions(obj,q,ajax_url){
	// check "q" for SQL injection
	$.getJSON(ajax_url+'.php?q='+encodeURIComponent(q),function(results){
		obj.find('li').remove(); //remove all LIs
		
		$(results).each(function(i){
			// add new LIs
			first_c='';
			if (i==0) {
				//first_c=' class="selected"';
				first_c=' ';
			}
			
			if (this==$("input.countries-class").val()) {
				first_c=' class="selected"';
			}
			obj.append("<li"+first_c+">"+this+"</li>");
			
			if (obj.find('li').length > 10){
				obj.css({'overflow-y':'scroll'});
			} else {
				obj.css({'overflow':'hidden', 'overflow-y':'hidden'});
			}
		});
		if ($(results).length == 1 && $(results)[0] == q){
			obj.fadeOut('fast').css({'overflow':'hidden', 'overflow-y':'hidden'});
			return;
		}
		if ($(results).length > 0)
			obj.fadeIn('fast');
		else {
			obj.fadeOut('fast');
			obj.css({'overflow':'hidden', 'overflow-y':'hidden'});
			return;
		}
	});
}


});


