var logger = {
   log: function(hotel_id, type, link) {
      $.ajax({
        url: '/hotels/log?hotel_id='+hotel_id+'&type='+type+'&language='+website_settings.language,
        success: function() {
          if(typeof link !='undefined') {
             location.href = link;
          }
        } 
      });
   }
};

function del_avatar() {           
  if(confirm($('#myzainoo-delavatar').attr('rel'))) {
       $.ajax({
        url:"/de/myzainoo/delavatar",
        success: function() {
            $('#thumb').attr('src', '/images/no-avatar.jpg');
            
            $('#myzainoo-user img').attr('src', '/images/no-avatar.jpg');
        }
       });
    }
}

$(document).ready(function(){
	
	/* parse external lind */
	$('a.ext').click('click', function(){
		window.open(this.href, '_blank'); return false;
	});
	
	/* dropdown regions cities */
	if($('#destination-overview').length){
		var MisAct = false;
		var overlayLink = $('#mitem2').attr('href');
		$('#destination-overview-overlay').attr('href', overlayLink);
		$('#mitem2').mouseenter(function(){
			$('#destination-overview').css({'display':'block'});
			if($('#mitem2').attr('class') == 'active'){
				MisAct = true;
			}else{
				$('#mitem2').addClass('active');
			}
		});
		$('#destination-overview').mouseleave(function(){
			$('#destination-overview').css({'display':'none'});
			if(MisAct == false){
				$('#mitem2').removeClass('active');
			}
		});
	}
	
	/*** top image ***/
	if($(".headimg_wrap img").length){
		$(".headimg_wrap").css({
			
		});
		$(".headimg_wrap img").css({
			'left': 0,
			'margin-left':0,
			'opacity': 0,
			'display':'block'
		});
		$(".headimg_wrap").css({
			'width': $(window).width()+"px"
		});
		$(function() {
		    $(".headimg_wrap img").scaleImage({
		        center: true
		    });
		});
		
		window.setTimeout(function() {
			$(".headimg_wrap img").animate({
				'opacity':1
			},1000);
		}, 1300);


		/** resize top image **/
		$(window).resize(function() {
			$('#topimg').css({
				'width': $(window).width()+"px"
			});
			$("#topimg img").scaleImage({
		        center: true
		    });
		});
	}
	
	
	
	$('#footermenu li a.iframe').each(function(i, el){
		$(el).attr('href', $(el).attr('href')+'?popup=1');
		/*$(el).fancybox({
	  		'speedIn'		:	600, 
	  		'speedOut'		:	200, 
	  		'overlayShow'	:	true,
	  		'overlayColor'	: '#000',
	  		'overlayOpacity': 0.9,
	  		'scrolling'		: 'no',
	  		'width'			: 977,
	  		'height'		: 550
		});*/
	});
	
	$('.tip-link').click(function(){
    $('#tip-window').toggle();
  });
	
	$("a[rel=fancybox]").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayColor'	: '#000',
		'overlayOpacity': 0.9,
		'cyclic'		:	true
	});
	
	$("a.iframe").fancybox({
		'speedIn'		:	500, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayColor'	: '#000',
		'overlayOpacity': 0.9,
		'scrolling'		: 'auto',
		'width'			: 700,
		'height'		: 550
	});
	
	$("#submenu a.iframe").fancybox({
		'speedIn'		:	500, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayColor'	: '#000',
		'overlayOpacity': 0.9,
		'scrolling'		: 'auto',
		'width'			: $(window).width()-120,
		'height'		: $(document).height()-120
	});

	
	$('.pdf-ajax-download').click(function(){
    var loader = $('<img class="ajax-loader" src="/images/ajax-loader.gif"/>');
    $(this).append(loader);
    $.ajax({
       url:$(this).attr('rel'),
       success: function(data) {
            $(loader).remove();
            window.location.href = data;
            
       }
    });
  });
	
	if($('.content-more').length && $('#content-complete').length == 0){
		var ccSpeed = 500;
		$('.content-more').click(function(event){
			event.preventDefault();
			window.setTimeout(function() {
				$('html,body').animate({scrollTop: $('#content-bottom').offset().top-30}, 1000);
			}, 200);
		});
	}
	
  $('#login-link').click(function(){
    	$('#loginmask').toggle();
		$('#myzainoo-infobox').hide();
  });
	$('#myzainoo-link1').click(function(){
		$('#myzainoo-infobox').toggle();
		$('#loginmask').hide();
	});
  
  $('#loginmask').submit(function(e){
     e.preventDefault();
    $.ajax( {
      url: $('#loginmask').attr('action'), 
      data: $('#loginmask').serialize(),
      type: 'post',
      success: function( data ) {
          window.location.reload();
      },
      error: function() {
          alert(website_settings.login_failed_message);
      }
    });
    
    
  });
  
  $('#login-forgot').click(function(){
    $('#login-forgot-window').toggle();
  });
  
  $('#login-forgot-button').click(function(){
    $.ajax({
      url: '/myzainoo/loginforgot',
      data: {language:website_settings.language, email:$('#login-forgot-email').val()},
      success: function(data) {
        alert(data);
        if(data.match(/\!/ig)) {
           $('#login-forgot-email').val('');
           $('#login-forgot-window').hide();
        }
      }
    });
  });
  
  $('#tour-save').click(function(){
     $('#tour-save-window').show();
  });
  
  $('#tour-save-confirm').click(function(){
     $.ajax({
      url:"/myzainoo/saveTour",
      data:{name:$('#tour-save-name').val()},
      success:function(id) {
        $('#tour-save-window').hide();
        $('#tour-save-name').val('');
        
        $.cookie('saved_tour', id, { expires: 3, path:'/' });
        location.reload(); 
      }
     });
  });
    
	//SEARCH
		$('#search_input').bind('keyup', function(){
	     if($(this).val().length>1) {
	       $.ajax({
	        url: '/search/autocomplete/?language='+website_settings.language,
	        data: {terms: $(this).val()},
	        success: function(data) {
	          $('#search_results').html(data);
	          if(data) {
	            $('#search_results').show();
	          } else {
	            $('#search_results').hide();
	          }
	        }
	       });
	     } else {
	       $('#search_results').hide();
	     }
	  });
	  $('#search_input').after('<div id="search_results"></div>');
	
	 /* TIPS */
	 $('.tip-submit').click(function(){
      $.ajax({
        url: '/poi/savetip',
        type: 'post',
        data: {comment:$('#tip-comment').val(), id:poi_settings.id, language:website_settings.language},
        success: function(res) {
          $('#tip-window').html(res);
        }
      });
   });

	/* POI IMG GALLERY */
	if($('#location-gallery-imgs').length){
		$('body').append($('#location-gallery-imgs'));
		$('body').append('<div id="location-gallery-overlay"></div>');
		
		function showContentGallery(){
			var ActivImage = 0;
			$('#location-gallery-overlay').css({'height':$(document).height(),'opacity':0,'display':'block'});
			$('#location-gallery-big').css({'opacity':0,'display':'block'});
			$('#location-gallery-thumbs').css({'top':$(window).height()-120,'width':$(document).width()-180,'opacity':0,'display':'block'});
			$('#location-gallery-overlay').animate({
				'opacity':0.93
			}, 700, function() {
		  	});
			$('#location-gallery-big').animate({
				'opacity':1
			}, 700, function() {
		  	});
			$('#location-gallery-thumbs').animate({
				'opacity':1
			}, 700, function() {
		  	});
			
			/* load imgs */
			function LoadGalleryImg(itemimg,itemnum){
				ActivImage = itemnum;
				$('#location-gallery-big-wrap').html('');
				var myImgSource = itemimg.attr('href');
				$(function () {
				  var myimg = new Image();
				  $(myimg)
					.load(function () {
						$('#location-gallery-big-wrap').html('');
				      	$(this).css('opacity', 0);
				      	$('#location-gallery-big-wrap').append(this);
						if($(this).width() < 600){
							$(this).css({'margin-left':$(this).width()/2});
						}
						$(this).animate({
						    'opacity': 1
						}, 700, function() {
						});
						if(itemimg.attr('title') && itemimg.find('img').attr('title')){
							$('#location-gallery-big-wrap').append('<div id="gallery-title">'+itemimg.attr('title')+', '+itemimg.find('img').attr('title')+'</div>');
						}else if(itemimg.attr('title')){
							$('#location-gallery-big-wrap').append('<div id="gallery-title">'+itemimg.attr('title')+'</div>');
						}
				    })
				    .error(function () {
						alert('error');
				    })
				    .attr('src', myImgSource);
				});
			}
			
			GalleryArray = new Array();
			$('.gallery-thumb').each(function(index){
				GalleryArray.push($(this));
				
				$(this).click(function(event){
					event.preventDefault();
					LoadGalleryImg(GalleryArray[index],index);
				});
			});
			LoadGalleryImg(GalleryArray[0],0);
			
			/* ARROW IMG NAV */
			if(GalleryArray.length > 1){
				$('#location-big-right').click(function(){
					if(ActivImage+1 < GalleryArray.length){
						LoadGalleryImg(GalleryArray[ActivImage+1],ActivImage+1);
					}else{
						LoadGalleryImg(GalleryArray[0],0);
					}
				});
				$('#location-big-left').click(function(){
					if(ActivImage > 0){
						LoadGalleryImg(GalleryArray[ActivImage-1],ActivImage-1);
					}else{
						LoadGalleryImg(GalleryArray[GalleryArray.length-1],GalleryArray.length-1);
					}
				});
			}else{
				$('#location-big-right').css({'display':'none'});
				$('#location-big-left').css({'display':'none'});
			}
			
			
			/* ARROW NAV THUMBS */
			$('#location-gallery-left').css({'opacity':0.2,'cursor':'default'});
			var thumbsPortWidth = $(document).width()-200;
			//$('#location-gallery-thumbs').css({'top':$(window).height()-120,'width':$(document).width()-180,'opacity':0,'display':'block'});
			var thumbsMaxWidth = $('.gallery-thumb').length*80;
			if(thumbsPortWidth < thumbsMaxWidth){
				$('#location-gallery-thumbs-move').css({'width':$('.gallery-thumb').length*80+50});
				$('#location-gallery-right').click(function(){
					var marginStatus = (parseInt($('#location-gallery-thumbs-move').css('margin-left')));
					if((marginStatus-(marginStatus*2))*2 < thumbsMaxWidth){
						$('#location-gallery-thumbs-move').animate({
							'margin-left':'-=480px'
						}, 700, function() {
							if(parseInt($('#location-gallery-thumbs-move').css('margin-left')) < 0){
								$('#location-gallery-left').css({'opacity':1,'cursor':'pointer'});
							}
							if((marginStatus-(marginStatus*2))*2 < thumbsMaxWidth){
								$('#location-gallery-right').css({'opacity':0.2,'cursor':'default'});
							}
					  	});
					}
				});
				$('#location-gallery-left').click(function(){
					if(parseInt($('#location-gallery-thumbs-move').css('margin-left')) < -1){
						$('#location-gallery-thumbs-move').animate({
							'margin-left':'+=480px'
						}, 700, function() {
							if((parseInt($('#location-gallery-thumbs-move').css('margin-left'))) == 0){
								$('#location-gallery-left').css({'opacity':0.2,'cursor':'default'});
							}
							$('#location-gallery-right').css({'opacity':1,'cursor':'default'});
					  	});
					}
				});
			}else{
				$('#location-gallery-right').css({'opacity':0.2,'cursor':'default'});
			}
		}
		
		/* SHOW ELEMENTS FOR GALLERY */
		$('#show-gallery').click(function(event){
			event.preventDefault();
			showContentGallery();
		});
		$('#show-gallery-img').click(function(event){
			event.preventDefault();
			showContentGallery();
		});
		
		$('#location-gallery-overlay').click(function(event){
			event.preventDefault();
			$('#location-gallery-overlay').animate({
				'opacity':0
			}, 700, function() {
				$('#location-gallery-overlay').css({'display':'none'});
		  	});
			$('#location-gallery-thumbs').animate({
				'opacity':0
			}, 700, function() {
				$('#location-gallery-thumbs').css({'display':'none'});
		  	});
			$('#location-gallery-big').animate({
				'opacity':0
			}, 700, function() {
				$('#location-gallery-big').css({'display':'none'});
		  	});
		});
	}
	
	/* INFOBOX BOX */
	if($('#trip-box-info1').length){
		$('.trip-plan').click(function(event){
			event.preventDefault();
			/*$('#trip-box-info1').css({'opacity':0,'display':'block'});
			$('#trip-box-info1').animate({
				'opacity':1
			}, 500, function() {
		  	});*/
			$('#trip-box-info1').css({'display':'block'});
			$('#trip-box-info2').css({'display':'none'});
		});
		$('#trip-box-info1-close').click(function(){
			$('#trip-box-info1').css({'display':'none'});
		});
	}
	if($('#trip-box-info2').length){
		$('.trip-my').click(function(event){
			if($('#tour-count').text() == 0){
				event.preventDefault();
				$('#trip-box-info2').css({'display':'block'});
				$('#trip-box-info1').css({'display':'none'});
			}
		});
		$('#trip-box-info2-close').click(function(){
			$('#trip-box-info2').css({'display':'none'});
		});
	}
	if($('#show-trip-box-infobox').length){
		$('#show-trip-box-infobox').click(function(event){
			event.preventDefault();
			$('#trip-box-info1').css({'display':'block'});
			$('#trip-box-info2').css({'display':'none'});
		});
	}
	
	/* REZEPTE */
	$('.recipe-mail').click(function(){
     $('#recipe-send-mail').toggle();
  });
  
  $('#recipe-send-mail-link').click(function(){
    $.ajax({
      url:'/recipe/mail', 
      data:{id:$('input[name=id]').val(), email:$('input[name=email]').val(), language:website_settings.language}, 
      success: function(data){
        alert(data);
        if(!data.match(/\!/ig)) {
           $('#recipe-send-mail-link input[name=email]').val('');
            $('#recipe-send-mail').hide();
        }
        
      }
    });
  });
  
  /* BEWERTUNG */
  
  $('#setrating b').hover(function() {
    $('#setrating b').removeClass('active');
    $(this).addClass('active');
    $(this).prevUntil('#setrating', 'b').addClass('active');
  }, function(){
    $('#setrating b').removeClass('active');
    $('#setrating b.set').addClass('active');
  });
  
  $('#setrating b').click(function() {
    $('#setrating b').removeClass('set');
    $(this).addClass('set');
    $(this).prevUntil('#setrating', 'b').addClass('set');
    $.ajax({
      url: '/poi/setrating/?id='+poi_settings.id+'&rating='+$(this).attr('rel')
    });
  });

	/* MY ZAINOO LOGIN POPUP */
	if($('#myzainoo-link').length){
		/* TABS */
		function afterMyLoad(){
			$.each($('#myzainoo-menu a'),function(){
				//if($(this).attr('id') != "myz_3"){
					$(this).click(function(event){
						event.preventDefault();
						$.ajax({
						  url: $(this).attr('href')+"?popup=3",
						  success: function(data) {
							$('#myzainoo-content').html(data);
							afterMyForm();
						  }
						});
					});
				//}
			});
			
			
		}
		function afterMyForm(){
			if($('.buttons input').length){
				$.each($('.buttons input'),function(){
					$(this).click(function(event){
						event.preventDefault();
						$('#myzainoo-content').css({'opacity':0.3});
						var SplitUrl = $(this).parent().parent().attr('action').split("=");
						var dataString = 'popup=3';
						$.each($('.form input'),function(){
							if($(this).attr('id')){
								dataString = dataString+"&"+$(this).attr('name')+"="+$(this).val();
							}
						});
						$.each($('.form select'),function(){
							if($(this).attr('id')){
								dataString = dataString+"&"+$(this).attr('name')+"="+$(this).val();
							}
						});
						//alert(dataString);
						$.ajax({  
						  type: "POST",  
						  url: SplitUrl[0],  
						  data: dataString,  
						  success: function(data) {
							$('#myzainoo-content').html(data);
							afterMyForm();
							$('#myzainoo-content').css({'opacity':1});
						  }  
						});
					});
				});
			}
		}
		
		$('body').append('<div id="myzainoo-popup-wrap"></div><div id="myzainoo-popup-overlay"></div>');
		$('#myzainoo-link').click(function(event){
			event.preventDefault();
			$('#myzainoo-popup-wrap').css({'opacity':0,'display':'block'});
			$('#myzainoo-popup-overlay').css({'height':$(document).height(),'opacity':0,'display':'block'});
			
			$('#myzainoo-popup-overlay').animate({'opacity':0.6},400,function(){});
			//$('#myzainoo-link').attr('href') + "?popup=2"
			
      $.ajax({
			  url: "/"+website_settings.language+"/myzainoo/profile?popup=2",
			  success: function(data) {
			    $('#myzainoo-popup-wrap').html(data);
				$('#myzainoo-popup-wrap').animate({'opacity':1},400,function(){});
				afterMyLoad();
				afterMyForm();
			  }
			});
		});
		$('#myzainoo-popup-overlay').click(function(){
			$(this).animate({'opacity':0},400,function(){
				$(this).css({'display':'none'});
			});
			$('#myzainoo-popup-wrap').animate({'opacity':0},400,function(){
				$('#myzainoo-popup-wrap').css({'display':'none'});
			});
		});
		
		/* MENU LEFT */
		if($('.myzainoo-menu-list').length){
			$.each($('.myzainoo-menu-list a'),function(){
				$(this).click(function(event){
					event.preventDefault();
					$('#myzainoo-popup-wrap').css({'opacity':0,'display':'block'});
					$('#myzainoo-popup-overlay').css({'height':$(document).height(),'opacity':0,'display':'block'});
					$('#myzainoo-popup-overlay').animate({'opacity':0.6},400,function(){});
					
					$.ajax({
					  url: $(this).attr('href') + "?popup=2",
					  success: function(data) {
					    $('#myzainoo-popup-wrap').html(data);
						$('#myzainoo-popup-wrap').animate({'opacity':1},400,function(){});
						afterMyLoad();
						afterMyForm();
					  }
					});
				});
			});
		}
		
		
		
		/*$.getScript('/js/myzainoo.js', function(data, textStatus){
		   
		});*/
	}
	
	
  
});
