var skyBackground = "images/sky.png";
var skySpeed = 47;
var timing=0;
var soflen=0;
var favOpen1=0;
var sinaythegay=0;

var currentTime = new Date()
var Mdate = currentTime.getDate() + "/" + (currentTime.getMonth() + 1) + "/" + currentTime.getFullYear()

function moveSky(movert,movedn,hPos,vPos){if(arguments[4]){document.getElementById('sky').style.backgroundImage = "url(\"" + arguments[4] + "\")";};if (arguments[5]){document.getElementById('sky').style.backgroundRepeat = arguments[5];};if(!isNaN(hPos)){if((movert!=0) && (hPos>0)){hPos=-100000;}hPos += movert;} if(!isNaN(vPos)){if((movedn!=0) && (vPos>0)){vPos=-100000;}vPos+= movedn;};document.getElementById('sky').style.backgroundPosition= hPos + 'px ' + vPos + 'px';if(isNaN(hPos)){hPos = "\"" + hPos + "\""};if (isNaN(vPos)){vPos = "\"" + vPos + "\"";};setTimeout("moveSky("+movert+","+movedn+","+hPos+","+vPos+")",skySpeed);}
 
$(function(){
	
	$('#dmenu div').hover(
		function(){
			
			pos=$(this).position().left;
			title=$(this).html().replace(/<[^>]+>/g,'');
			link=$(this).children('a').attr('href');
			id=$(this).index();
			$('<div class="dmm" onclick="window.location.href=\''+link+'\'" style="cursor:pointer;width:127px;height:38px;top:-10px;left:'+(pos+250)+'px;position:absolute;height:38px;background:url(/images/smenubg.gif)"><div style="width:100%;height:100%;background:url(/images/smenur.gif) no-repeat right"><div style="width:100%;height:100%;background:url(/images/smenul.gif) no-repeat left;text-align:center;color:#1d63ab;line-height:32px;font-size:14px;font-family:arial">'+title+'</div></div></div>').appendTo('#imber').mouseleave(function(){$(this).remove()}).mouseenter(function(){$('.dmm').not(this).remove()});
					
		}
	);

	$('#iface').html('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2FTel-Aviv-Yafo-Israel%2FTlv2go-Tel-Aviv-apartments%2F161126893922904&amp;width=200&amp;colorscheme=light&amp;connections=6&amp;stream=false&amp;header=true&amp;height=320" style="overflow:hidden;border:0px;border:none; overflow:hidden; width:200px; height:320px;" scrolling="no" frameborder="0"></iframe>');
	/*bpos=$('.bottom:last').position();
	$('#nach').css({'left':(bpos.left-10)+'px','top':(bpos.top-245)+'px','display':''});*/
	$('#checkin').datepicker({showAnim: 'show',dateFormat:'dd/mm/yy',minDate: Mdate}).change(function(){
		var minDate = $('#checkout').datepicker("option", "minDate");
		md=$(this).val();
		$('#checkout').datepicker( "option", "minDate", md);
	});
	$('#checkout').datepicker({showAnim: 'show',dateFormat:'dd/mm/yy',minDate:Mdate});
	//$('.hh').mousemove(function(){$('#bubble').hide();clearTimeout(timing);});
	$.get('http://www.tlv2go.com/ip2c',function(s){$('#country, input[name=country]').val(s);});
	$('#bfbtn').click(function(){
		if($('#email').val().length<6 || $('#email').val().indexOf('@')==-1){alert('Please enter a valid email address, we will contact you by email');return;}
		$(this).unbind('click').css('cursor','auto').html('<img src="/images/loader.gif" style="margin:5px 55px 0px 0px">');
		var strData='';
		$('#gaoffer input').each(function(){strData=strData+'&'+$(this).attr('name')+'='+$(this).val();});
		$.ajax({
			url:'getlead.php',
			type:'POST',
			cache:false,
			dataType:'html',
			data:strData,
			success:function(){
				window.setTimeout(function(){
					$('#bookfrm,#bfbtn,#ibcomm').fadeOut(function(){
						$('<div style="font-size:14px;position:absolute;left:75px;bottom:-40px;text-align:center;">Thank you for your request<br>We will get back to you shortly with a great offer</div>').appendTo('#ibbr').animate({bottom:50});
					})	
				},550);
				
				var iframe = document.createElement('iframe');
				iframe.style.width = '0px';
				iframe.style.height = '0px';
				document.body.appendChild(iframe);
				iframe.src = 'http://www.tlv2go.com/adwords_conversion.html';
			}
		});
	});
	
	moveSky(-1,0,0,0, skyBackground);

	sdiv=new Array('#fdd51d','#1d62ab','#599de4','#99d2f4','#7ee9a6','#50b45e','#5d5d5d');
	$('#showme div').each(
		function(i){
			if(i==0){$(this).css('background','#fff').attr('k','y').html('<div class="show2">'+$(this).html()+'</div><div class="show1" style="width:28px;background:'+sdiv[i]+'"></div></div>')}
			else{
			   $(this).html('<div class="show2">'+$(this).html()+'</div><div class="show1" style="background:'+sdiv[i]+'"></div></div>');
			 }
		}
	).hover(
		function(){if($(this).attr('k')!='y'){$(this).css('background','#fff');$(this).children('.show1').animate({width:18},'fast');}},
		function(){if($(this).attr('k')!='y'){$(this).css('background','#ebebeb');$(this).children('.show1').animate({width:10},'fast');}}
	).click(function(){
		sm=parseInt($(this).attr('id').replace('sm',''));
		if(sm>=6){window.top.location.href='http://www.tlv2go.com/?m=1&id='+sm;}
		if(sm>0){$('#nrooms').val(sm);}else{$('#nrooms').val('');}
		$('.smdiv').css('background','#ebebeb').attr('k','0');$(this).attr('k','y').css('background','#fff');$('.show1').animate({width:10},'fast');$(this).children('.show1').stop().animate({width:28},'fast');

		m=sm>=6?4:2;
		if(sm==8){window.top.location.href='http://www.tlv2go.com/?m=3&id=me5';return;}
		$.get('http://www.tlv2go.com/?m=2&b='+sm,function(html){
			$('#contents').stop().animate({opacity:0},'slow',function(){
				$('#contents').html(html).animate({opacity:1});aptBox();aptclick();
			});
		})

	});
	
	$('#logo2').hide();
	$('#logo').click(function(){window.top.location.href='/';}).hover(function(){$('#logo2').fadeIn(205);},function(){$('#logo2').fadeOut('slow');});	
	aptBox();
	//$('#dmenu div').click(function(){window.location.href='http://www.tlv2go.com/?m=3&id='+$(this).attr('id');});
	$('#ssublets').click(function(){window.location.href='/?s=1';});
	
	$('.soffer').hover(
		function(){$(this).css('border','1px #bbb solid');},
		function(){$(this).css('border','1px #fff solid');}
	).click(function(){window.top.location.href='http://www.tlv2go.com/?m=1&id='+$(this).attr('id').replace('spo','');});
	
	$('#bookbar').hover(
		function(){$('#goffer').animate({top:27},'fast');},
		function(){$('#goffer').animate({top:34},'fast');$(this).unbind('mouseenter mouseleave');}
	);
	
	$('#ispec').hover(
		function(){
			clearTimeout(sinaythegay);
		},
		function(){sinaythegay=window.setTimeout('chSoffer(1)',700);}
	)
	
	$('.soffer').attr('show',0).hide().eq($('.sobox1').length).attr('show','y').fadeIn();
	$('.soffer').eq(0).attr('show','y').fadeIn(function(){sinaythegay=window.setTimeout('chSoffer(1)',2000);});
	
	
	
	$('#mftitle').animate({opacity:0.6},0).hover(function(){$(this).animate({opacity:1},'fast')},function(){if($(this).attr('k')!='1'){$(this).animate({opacity:0.6},'fast')}})
	.click(function(){if(favOpen1){favClose();}else{favOpen();}});
	
	aptclick();
	
	$('#favmover').width(($('.favbox').length*92));
	$('.gocheck').click(
		function(){
			if($('input[name='+$(this).attr('id')+']').val()=='yes'){$(this).children('img').attr('src','/images/n_check.gif');$('input[name='+$(this).attr('id')+']').val('no');}
			else{$(this).children('img').attr('src','/images/n_checked.gif');$('input[name='+$(this).attr('id')+']').val('yes');}
		}
	)
	
	$('.smaps').hover(
		function(){$(this).parents('.aptin').children('.apics').children('.dsmap').fadeIn('fast')},
		function(){$(this).parents('.aptin').children('.apics').children('.dsmap').fadeOut('fast')}
	);
	
	$('.choosenum').focus(function(){
		t=$(this);
		pos=t.offset();
		
		ih='<div style="display:none"></div><div id="dnum1">1</div><div id="dnum2">2</div><div id="dnum3">3</div><div id="dnum4">4</div><div id="dnum5">5</div><div id="dnum6">6</div><div id="dnum7">7</div><div id="dnum8">8</div><div id="dnum9">9</div><div id="dnum10">10</div>';
		iw=25;
		if($(this).attr('id')=='nrooms'){iw=65;ih='<div id="dnum0">Studio</div><div id="dnum1">1</div><div id="dnum2">2</div><div id="dnum3">3</div>';}
		
		$('<div style="left:'+pos.left+'px;top:'+(pos.top+22)+'px;" class="numchooser">'+ih+'</div>').appendTo('body').slideDown('fast');
		$('.numchooser div').width(iw);

		$('.numchooser div').addClass('numch_hover0').hover(
			function(){$(this).removeClass('numch_hover0').addClass('numch_hover1');},
			function(){$(this).removeClass('numch_hover1').addClass('numch_hover0');}
		).mousedown(function(){t.val($(this).html());});
		
	}).blur(function(){$('.numchooser').fadeOut('fast',function(){$(this).remove();})})
	.keydown(function(e){
		if(e.which==40 || e.which==38){
			v=Number($(this).val().replace(/[^0-9]/g,''));
			if($(this).attr('id')=='nrooms' && v==0 && !$(this).val()){v=-1;}
			
			if(e.which==40){v++;}
			if(e.which==38){
				
				v=$(this).attr('id')=='nrooms' && v==-1 ? 3 : v-1;
				if($(this).attr('id')=='nrooms'){v=v==-1?3:v;}
			
			}
			
			$(this).val($('.numchooser div').eq(v).html());
			
			$('.numchooser div').removeClass('numch_hover1').addClass('numch_hover0');
			$('#dnum'+v).removeClass('numch_hover0').addClass('numch_hover1');
		}
		if(e.which==13 && v){$('.numchooser').fadeOut('fast',function(){$(this).remove();});}
	});
	
});

function chSoffer(e){
clearTimeout(sinaythegay);
if($('.sobox'+e).length==1){chSoffer(e==1?2:1);return}

t=$('.sobox'+e+'[show=y]');
i=t.index()+1;
l=$('.soffer').length;

if(e==1){
	
	xstart=$('.sobox2').length;
	x=2;
	i=i>(l-xstart-1) ? 0 : i;
}
else{
	x=1;
	i=i>l-1 ? l-$('.sobox2').length : i;	
}

t.attr('show',0).fadeOut('slow',function(){
	$('.soffer').eq(i).attr('show','y').fadeIn('slow',function(){
		sinaythegay=window.setTimeout('chSoffer('+x+')',4500);
	});
})
}


function aptclick(){
$('.so, .aptname, .apics, .smaps').click(function(){window.top.location.href='/?m=1&id='+$(this).parents('.aptbox').attr('id');});
$('.fav').click(function(){
	favs($(this).attr('id'));
});
}

function favOpen(){if(favOpen1){return};favOpen1=1;$('#gaoffer,#mfholder').animate({height:'+=80'},300);$('#mftitle').attr('k','1').stop().animate({opacity:1},300);$('#myarr').attr('src','/images/n_arr.png');}
function favClose(){if(!favOpen1){return};favOpen1=0;$('#gaoffer,#mfholder').animate({height:'-=80'},300);$('#mftitle').attr('k','0').stop().animate({opacity:0.6},300);$('#myarr').attr('src','/images/n_arr1.png');}

function favmove(m){
op=m==1?'+':'-';
pos=$('#favmover').position();
if(m==2){
	fcont=$('#fcont').width();
	favmover=$('#favmover').width();
	if(pos.left<=fcont-favmover){return;}
}
else{if(pos.left>=0){return};}

$('#favmover').animate({left:op+'=92'},'fast');
}

function favs(e,m){
	id=e.replace('fav','');
	$.get('/?m=4&id='+id,function(s){
		if(s.fmode==1)
		{
			$('#favmover').width((($('.favbox').length+1)*92));
			$('#fav'+id).attr('title','Remove from favorites').html('<img src="/images/fav1.gif">');
			if(s.bmode!=1){$('#mfdiv').html('');}
			slc=s.bmode?'#favmover':'#mfdiv';
			favOpen();
			$.get('/?m=5&b='+s.bmode+'&id='+id,function(h){
				$(h).css('display','none').prependTo(slc).fadeIn('slow');
			});
				$('#favorites').children('img').attr('src','/images/n_checked.gif');$('input[name=favorites]').val('yes');
				if(m){$('#aptfav').html('<img src="/images/fav1.gif"> &nbsp;Remove from favorites');}
		}
		else
		{
			$('#fav'+id).attr('title','Add to favorites').html('<img src="/images/fav0.gif">');
			if(s.bmode!=1){$('#mfdiv').html('<center style="color:#666"><br><br><br><br>To add favorites Apartments please click on the star next to them</center>');}
			else{
				$('#favbox'+id).fadeOut(function(){$(this).remove();$('#favmover').width(($('.favbox').length*92));})
			}
			if(m){$('#aptfav').html('<img src="/images/fav0.gif"> &nbsp;Add to  favorites');}
		}
	},'json');
}
function specmove(c){
return;
	c=c>(soflen-1)?0:c;
	$('.soffer').animate({top:'-=181'},'slow',function(){
		$('.soffer').eq((c-1)).css('top',181*(soflen-1)+'px');
		
	});
	c++;window.setTimeout('specmove('+c+')',5000);
}

function aptBox(){
	//$('#bubble').mousemove(function(){$(this).show();});
	$('.aptbox').hover(
		function(){
			clearTimeout(timing);
			id=$(this).attr('id');
			didit=$(this).attr('didit')?1:0;
			p=$(this).position();
			//$('#bubble').css('top',(p.top-70)+'px').css('left',(p.left)+'px').css('z-index',9999999).html('<div>'+$('#'+$(this).attr('id')+'summ').html()+'</div>').show().stop().animate({top:'-=10'},'def');
			arrPics=$('#'+id+'arrpics').html().split(',');
			if(didit){timing=window.setTimeout(function(){keepMovePic($('#'+id+'move'));},10)}
			else{
				$('<img src="/thumbs.php?s=images/uploads/'+arrPics[0]+'&w=198">')
				.load(function(){
					$('<img src="/thumbs.php?s=images/uploads/'+arrPics[1]+'&w=198">')
					.load(function(){
						$('<img src="/thumbs.php?s=images/uploads/'+arrPics[2]+'&w=198">')
						.load(function(){$('<img src="/thumbs.php?s=images/uploads/'+arrPics[3]+'&w=198">').load(function(){
							$('#'+id+'move').append('<img src="/thumbs.php?s=images/uploads/'+arrPics[0]+'&w=198"><img src="/thumbs.php?s=images/uploads/'+arrPics[1]+'&w=198"><img src="/thumbs.php?s=images/uploads/'+arrPics[2]+'&w=198"><img src="/thumbs.php?s=images/uploads/'+arrPics[3]+'&w=198">');
							$('#'+id).attr('didit','true');
							keepMovePic($('#'+id+'move'));
						});});
					});
				

				});
			}
		},
		function(){clearTimeout(timing);/*$('#bubble').hide();*/$('#'+$(this).attr('id')+'move').stop().animate({left:-1},'fast');}
	);
}

function keepMovePic(mover){
clearTimeout(timing);
p=mover.position();
if(p.left<=-600){clearTimeout(timing);return;};
npos=Math.round(p.left/200)*200-200;
mover.stop().animate({left:npos},400,function(){timing=window.setTimeout(function(){keepMovePic(mover);},950);});
}

function sendLead(frm){
	var strData='';
	if($('#'+frm+' input[name=email]').length && $('#'+frm+' input[name=email]').val().indexOf('@')==-1){alert('Please enter a valid Email address')}
	else{
	
	$('#'+frm+' input, #'+frm+' textarea').each(function(){strData=strData+'&'+$(this).attr('name')+'='+$(this).val();});
	
	$.ajax({
		url:'getlead.php',
		type:'POST',
		cache:false,
		dataType:'html',
		data:'cha=מאובטח'+strData,
		success:function(){
			
			$('#'+frm).html('<center style="color:#000;padding-top:50px;font-size:14px">Thank you for your message.<br>We will get back to you shortly</center>');
			
				var iframe = document.createElement('iframe');
				iframe.style.width = '0px';
				iframe.style.height = '0px';
				document.body.appendChild(iframe);
				iframe.src = 'http://www.tlv2go.com/adwords_conversion.html';
		}
	});
	}
}
