$(window).load(function(){  
	table_links('#exatq');
});

$(document).ready(function(){
	
	/* Menu hover */ 
	menu_hover('#menu li.sub');
	/* Hotels effect */
	hotels_init("#hotels");  
	/* Images */
	images_init(".imagesBlock"); 
	/* Placeholder */
	placeholders('.placeholder');
	/* Partners Contacts */
	partners('.partners');
	/* Other hotels */
	partners('.bread'); 
	/* Hotel Info */
	hotel_info('.hotelInfo .text', '.hotelInfo .raz');      
	/* Photo Block */
	photoblock_init('.photoBlock .block');    
	/* Galery */
	$("a.galery").fancybox();             
	/* Partners */
	partnersblock_init('#partners');
	/* Content sub */
	content_sub('.hotelInfo .text .wrap .block .sub');

	$('A[href^="http://"], A[href^="https://"]').live('click', function() {
		this.target = '_blank';
	});  
	
	opinion_check('#sendMail');   
	/*  Select Hotel */
	select_hotel('.selecthotel');  
	/* News Greek tpl */
	news_greek();
	/* video */
	video_page();
	/* timer */
	$('.timers .timer1').countdown({
    day: 13,
    mounth: 02,
    year: 2012
  });
  $('.timers .timer2').countdown({
    day: 14,
    mounth: 02,
    year: 2012
  });
});

function video_page () {
  $('.videoUsers .user').click(function() {
    var pop = $(this).find('.popup');
    $(pop).css('margin', '-' + $(pop).height() / 2 + 'px 0px 0px -' + $(pop).width() / 2 + 'px').css('display', 'block');
    return false;
  });
  $('.videoUsers .user a.close').click(function() {
    $(this).parent().css('display', 'none');
    return false;
  });
  $('.videoText a.usl').click(function() {
    if ($(this).hasClass('open')) {
      $('.videoText div.usl').css('display', 'none');
      $(this).removeClass('open');
    } else {
      $('.videoText div.usl').css('display', 'block');
      $(this).addClass('open');
    };
    
    return false;
  });
} 

function table_links (table) {
	var links = $(table).find('.desc h6 a'); 
	var head = $(table).find('.desc h6');
	var string = '';
	$(head).each(function(){       
		if ($(this).find('a').length == 0) { 
			if ($(this).html()=='Aldemar Royal Olympian&nbsp;5*') {
				$(this).html('<a href="royal_olympian.php">Aldemar Royal Olympian&nbsp;5*</a>');
			}
			if ($(this).html()=='Aldemar Royal Mare Village&nbsp;5*') {
				$(this).html('<a href="royal_mare.php">Aldemar Royal Mare Village&nbsp;5*</a>');
			}
			if ($(this).html()=='Aldemar Royal Mare&nbsp;5*') {
				$(this).html('<a href="royal_mare.php">Aldemar Royal Mare&nbsp;5*</a>');
			}
			if ($(this).html()=='Aldemar Royal Olympian&nbsp;5*') {
				$(this).html('<a href="royal_olympian.php">Aldemar Royal Olympian&nbsp;5*</a>');
			}
			if ($(this).html()=='ALDEMAR ROYAL MARE SUITES&nbsp;5*') {
				$(this).html('<a href="royal_mare.php">Aldemar Royal Mare Suite&nbsp;5*</a>');
			}
		}
	});
	$(links).each(function(){
		switch ($(this).html()) {
			case 'ALDEMAR ROYAL MARE': string = '<a href="royal_mare.php">Aldemar Royal Mare&nbsp;5*</a>'; break;
			case 'Aldemar Knossos Royal': string = '<a href="knossos_royal.php">Aldemar Knossos Royal&nbsp;5*</a>'; break;
			case 'Aldemar Paradise Village': string = '<a href="paradise_village.php">Aldemar Paradise Village&nbsp;5*</a>'; break;
			case 'Aldemar Cretan Village': string = '<a href="cretan_village.php">Aldemar Cretan Village&nbsp;4*</a>'; break;
			case 'Aldemar Paradise Mare': string = '<a href="paradise_mare.php">Aldemar Paradise Mare&nbsp;5*</a>'; break;
			case 'Aldemar Olympian Village': string = '<a href="olympian_village.php">Aldemar Olympian Village&nbsp;5*</a>'; break;
			case 'Aldemar Royal Olympian': string = '<a href="royal_olympian.php">Aldemar Royal Olympian&nbsp;5*</a>'; break;
			case 'Aldemar Royal Mare Village': string = '<a href="royal_mare.php">Aldemar Royal Mare Village&nbsp;5*</a>'; break;
			case 'Aldemar Royal Villas': string = '<a href="royal_villas.php">Aldemar Royal Villas&nbsp;5*</a>'; break;
		}
		$(this).parent().html(string);
	});

}             

function news_greek () {
	                   
	//console.log(dataItems);
	
	var tmplItems = [];   
	
	function updateDebugInfo() {
        $('#data_items_length').text(dataItems.length);
        $('#tmpl_items_length').text(tmplItems.length);
    }     

	var options = {
		getNormalDate: function (created_at) {         
			var d = new Date(created_at)
			var mounthRus;
			switch (d.getMonth()) {
				case 0: mounthRus = "января"; break;
				case 1: mounthRus = "февраля"; break;
				case 2: mounthRus = "марта"; break;
				case 3: mounthRus = "апреля"; break;
				case 4: mounthRus = "мая"; break;
				case 5: mounthRus = "июня"; break;
				case 6: mounthRus = "июля"; break;
				case 7: mounthRus = "августа"; break;
				case 8: mounthRus = "сентября"; break;
				case 9: mounthRus = "октября"; break;
				case 10: mounthRus = "ноября"; break;
				case 11: mounthRus = "декабря"; break;
			}    
			var newDate = d.format("dd ") + mounthRus + d.format(" yyyy года в HH:MM");
			return newDate;
		},
        rendered: function (tmplItem) {
            if (window.console) {
                //console.log("rendered: %s", tmplItem.data.title);
            }
        }
    };                                                            

	$('#item_tmpl').tmpl(dataItems, options).insertAfter('#insGreece');
	
}

function select_hotel (wrap) {
	var left_links = $(wrap).find('div.view ul.os li a');  
	var wr = $(wrap).find('div.wrap');   
	var right_links = $(wrap).find('div.place map area');
	
	var left_prev = $(wrap).find('li.prev a.pre');
	
	$(right_links).click(function(){
		var id_layer = $(this).attr('rel');
		var layer = $(wr).find('.col.m .' + id_layer);         
		var layers = $(wr).find('.col.m .layer');
		
		$(layers).css('z-index', 10);
		$(layer).css('z-index', 900);
		$(wr).animate({
			'margin-left': '-8px'
		});
		
		return false;
	});
	
	$(left_prev).click(function(){
		$(left_links).removeClass('active');
		$(wr).animate({
			'margin-left': '-156px'
		});
		return false;
	});
	
	$(left_links).click(function(){    
		$(left_links).removeClass('active');
		$(this).addClass('active');
	    var id_layer = $(this).attr('rel');
		var layer = $(wr).find('.col.l .' + id_layer);         
		var layers = $(wr).find('.col.l .layer');
		
		$(layers).css('z-index', 10);
		$(layer).css('z-index', 900);
		$(wr).animate({
			'margin-left': '-312px'
		});
		
		return false;
	});
	 
}                                 

function opinion_check (form) {
	$(form).submit(function(){
		if ($(form).find('.placeholder').length > 0) {     
			alert("Форма не заполнена");
			return false;
		} else {
			return true;
		}
	});
}          

function content_sub(sub) {
	var label = $(sub).find('.label');
	var text = $(sub).find('.tex');

	$(text).slideToggle();

	$(label).click(function(){
		$(this).parent().find('.tex').slideToggle();
	});
}

function partnersblock_init (block) {  
	var col = $(block).find('.partner').length;  
	var end = 4;  
	var w = 183;
	var pos = 0;    
	var l = false;
	var r = false; 
	var stop = false;
	var wrap = $(block).find('.wrap');
	if (col<5) {
		$(block).find('a.left').remove()
		.parent().find('a.right').remove();
	} else {
		r = true;
	};
	link_op('a.left', 0.5);     
    
	$(block)
		.mouseenter(function(){
			stop = true;
		})
		.mouseleave(function(){
			auto();
		});

	auto();

	function rot() {
		setTimeout(function() {if (!stop) { auto() };   }, 2000); 
	}

	function auto() {
		setTimeout(function() {
			if (r) {
				to_right();
				check_pos();
			} else {
				to_start();
				check_pos();
			}  
			rot(); 
		}, 2000);
	}

    $(block).find('a.left').click(function(){  
		if (l) {
			to_left();
			check_pos();
		}
		return false;
	});  
	$(block).find('a.right').click(function(){  
		if (r) {
			to_right();
			check_pos();  
		};
		return false;
	});
	     
	function link_op(link, op) {
		$(block).find(link).fadeTo(0, op);
	}	
	function to_right() {
		$(wrap).animate({
			'margin-left': pos - w
		});
		pos = pos - w;     
		end++;
	}                 
	function to_left() {
		$(wrap).animate({
			'margin-left': pos + w
		});
		pos = pos + w; 
		end--;	
	} 
	function to_start() {
		$(wrap).animate({
			'margin-left': 0
		});
		pos = 0; 
		end = 4;	
	}   
	function check_pos() {
		if (end == col) {
			link_op('a.right', 0.5);  
			r = false;
		} else {
			link_op('a.right', 1);  
			r = true;
		}
		if (end > 4) {
			link_op('a.left', 1);  
			l = true;
		} else {
			link_op('a.left', 0.5);  
			l = false;
		}
	}
	

}
  
function photoblock_init (block) {  
	var col = $(block).find('.photo').length;  
	var end = 6;  
	var w = 154;
	var pos = 0;    
	var l = false;
	var r = false;
	var wrap = $(block).find('.wrap');
	if (col<5) {
		$(block).find('a.left').remove()
		.parent().find('a.right').remove();
	} else {
		r = true;
	};
	link_op('a.left', 0.5);     

    $(block).find('a.left').click(function(){  
		if (l) {
			to_left();
			check_pos();
		}
		return false;
	});  
	$(block).find('a.right').click(function(){  
		if (r) {
			to_right();
			check_pos();  
		};
		return false;
	});
	     
	function link_op(link, op) {
		$(block).find(link).fadeTo(0, op);
	}	
	function to_right() {
		$(wrap).animate({
			'margin-left': pos - w
		});
		pos = pos - w;     
		end++;
	}                 
	function to_left() {
		$(wrap).animate({
			'margin-left': pos + w
		});
		pos = pos + w; 
		end--;	
	}    
	function check_pos() {
		if (end == col) {
			link_op('a.right', 0.5);  
			r = false;
		} else {
			link_op('a.right', 1);  
			r = true;
		}
		if (end > 6) {
			link_op('a.left', 1);  
			l = true;
		} else {
			link_op('a.left', 0.5);  
			l = false;
		}
	}
	

}
      
function hotel_info (texts, articles) {
	$(articles).find('.block a').click(function(){      
		var block = $(texts).find(".block[rel='" + $(this).parent().attr('rel') + "']");  
		var len = $(block).prevAll().length;   
		$(texts).find('.wrap').animate({
			'margin-left': len * -653
		});
		$(articles).find('.block.active').removeClass('active');
		$(this).parent().addClass('active');
		return false;
	});
}

function partners(wrap) {
	
	var vis_con = false;

	$(wrap).find('.contactsLink a').click(function(){
		if (vis_con) {
			if ($(this).parent().parent().find('.contacts').hasClass('vis')) {
				$(wrap).find('.vis').fadeOut(100).removeClass('vis');
			} else {
				$(wrap).find('.vis').fadeOut(100).removeClass('vis');
				$(this).parent().parent().find('.contacts').fadeIn(300).addClass('vis');
				vis_con = true;	
			}
			
		} else {
			$(this).parent().parent().find('.contacts').fadeIn(300).addClass('vis');
			vis_con = true;
		}
		return false;
	});

	$(wrap).find('.contacts a.close').click(function(){
		$(this).parent().fadeOut(300).removeClass('vis');
		vis_con = false;
		return false;
	});

}    
   
function placeholders(els) {
	$(els).each(function(){
		if ($(this).tagName = 'input') {
			$(this).focus(function(){
				if ($(this).attr('value')==$(this).attr('placeholder')) {
					$(this).attr('value', '');
					$(this).removeClass('placeholder');
				}
			});
			$(this).blur(function(){
				if ($(this).attr('value')=='') {
					$(this).attr('value', $(this).attr('placeholder'));
					$(this).addClass('placeholder');	
				}
				
			});
		} else if ($(this).tagName = 'textarea') {
			$(this).focus(function(){
				if ($(this).html()==$(this).attr('placeholder')) {
					$(this).html('');
					$(this).removeClass('placeholder');
				}
			});
			$(this).blur(function(){
				if ($(this).html()=='') {
					$(this).html($(this).attr('placeholder'));
					$(this).addClass('placeholder');
				}
			});
		}
 	});
}

function images_init (block) {   
	var timer;  
	var vis = false;
	var start_index = 5000;
	$(block)
		.mouseenter(function(){  
			timer = setTimeout(function() {
				$(block)
					.find(".th").fadeTo('fast', 0.8)
					.parent().find(".pointer").fadeTo('fast', 0);
				vis = true;
			}, 100);
		})
		.mouseleave(function(){
			if (timer) {
				clearTimeout(timer);
			}
			if (vis) {
				$(block).find(".th").fadeOut('fast', 0); 
				setTimeout(function() {
		    		$(block).find(".pointer").fadeTo('fast', 1); 
				}, 10000);
			};
		})
		.find(".th img").click(function(){
			var index = $(this).attr('rel');
			$(block).find('.bigs').find("img[rel=" + index + "]").css("z-index", start_index);
			start_index++;
		});
}

function menu_hover (sub) {

	$(sub)
		.mouseenter(function(){
			$(this).find("ul").fadeIn(100);
		})
		.mouseleave(function(){
			$(this).find("ul").fadeOut(100);
		});        

}

function hotels_init (wrap) {
	var bigs = $(wrap).find(".bigs");
	var items = $(wrap).find(".thumbnails .item");
	var hover = false;
	
	$(bigs).find('.big').css('display', 'none');
	$(bigs).find('.active').css('display', 'block');
	                          
	$(items).dblclick(function(){
		window.location = $(this).attr('ahref');
	});
	
	$(items).click(function(){
			
		var id = $(this).attr("hotel_id");  
		var href = $(this).attr('ahref');
		$('.thumbnails').find('.active').removeClass('active');
		$(this).addClass('active');
		
		$(bigs).find('.big').each(function(){
			
			if ($(this).attr("hotel_id") == id) {
				
				if ($(this).hasClass('active')) {
				
					window.location = href;
					
				} else {
				
					$(this)
						.css('display', 'block')
						.css('z-index', '9700')
						.find('.main').css('display', 'none').parent()
						.find('.stars').css('display', 'none').parent()
						.find('.desc').css('display', 'none').parent()
						.find('.images img').css('top', '-500px').parent()
						.find('img.bg1').animate({top: '0px'}, 150).parent()
						.find('img.bg2').animate({top: '0px'}, 300).parent()
						.find('img.bg3').animate({top: '0px'}, 450).parent()
						.find('img.bg4').animate({top: '0px'}, 600, function(){
							$(this).parent().parent()
							.find('.main').fadeIn(600).parent()
							.find('.stars').fadeIn(600).parent()
							.find('.desc').fadeIn(600).parent();
							$(bigs).find('.active').removeClass('active').css('display', 'none');
							$(this).parent().parent().addClass('active').css('z-index', '1');
						});
					
				}
   
			}
			
		});
		
	});

	$('#hotels')
		.mouseenter(function(){ hover = true;  })
		.mouseleave(function(){ hover = false; });
	$('#menu')
		.mouseenter(function(){ hover = true;  })
		.mouseleave(function(){ hover = false; });	

	timer();

	function next() {
	 	if (!hover) {
	 		var th = $('.thumbnails').find('.active');
	 		if ($(th).attr('hotel_id') != 8) { 
	 			$('.thumbnails').find('.active').next().click(); 
	 		} else {
	 			$('.thumbnails .item:first').click();	
	 		} 
	 	}
	 	timer();
	}

	function timer() {
		setTimeout(function(){
			next();
		}, 6000);	
	}
	
}
