$(window).load(function(){
	var left_side = $('#content').height();
	var right_side = $('#right_side').height();
	var min_height;
	if(right_side >= left_side){min_height = right_side;}
	if(min_height != null && min_height != ''){
		$('#inner_content').css('height',left_side+'px');
	}
});

var i = 1;
function change_carousel(){
	click_on_carousel('#fp_carousel ul li a:eq('+ i +')','auto');
	i++;
	if(i == $('#fp_carousel ul li').size()){i = 0;}
}

$(function() {
	var url = window.location.href;
	if (strpos(url,'/bg') !== false){var lang = 'bg';}
	if (strpos(url,'/en') !== false){var lang = 'en';}
	if (strpos(url,'/en') === false && strpos(url,'/bg') === false){var lang = 'en';}
	if(lang != null){
		$('#language_select .selected').attr('lang',lang).text($('#language_select #'+lang).text());
	}
	
	$('#language_select .hide').live('click',function(){
		var url = window.location.href;
		var val = '/' + $('.selected',$(this).parent()).attr('lang');
		var new_url;
		if (strpos(url,'/bg') !== false){new_url = url.replace('/bg',val);}
		if (strpos(url,'/en') !== false){new_url = url.replace('/en',val);}
		if (strpos(url,'/en') === false && strpos(url,'/bg') === false){new_url = rtrim(url,'/') + val;}
		window.location.href = new_url;
	});
	
	// set min-height on index rooms box
	var max_height = 0;
	$('#rooms_and_prices li').each(function(){
		if(max_height < $(this).height()){max_height = $(this).height();}
	});
	$('#rooms_and_prices li').css('min-height',max_height+'px');
	//	
	
	// set min-height on inner rooms page
	var max_height = 0;
	$('#rooms_page li').each(function(){
		if(max_height < $(this).height()){max_height = $(this).height();}
	});
	$('#rooms_page li').css('min-height',max_height+'px');
	//

	$('#datepicker').datepicker({
		showOn: "button",
		buttonImage: _public+"images/datepicker_icon.jpg",
		buttonImageOnly: true,
		onSelect: function(dateText, inst) {
		
			var day = inst.selectedDay;
			var mounth = inst.selectedMonth+1;
			var year = inst.selectedYear;

			$("#year_select .hide").each(function(){
				if($(this).text() == year){
					$(this).addClass('selected_active');
					$('.selected',$(this).parent()).attr('lang',$(this).attr('id')).html($(this).text());
					$('#year_hidden').val($(this).attr('id'));

				}
			});
	
			$("#month_select .hide").each(function(){
				if($(this).attr('id') == 'month_' + mounth){
					$(this).addClass('selected_active');
					$('.selected',$(this).parent()).attr('lang',$(this).attr('id')).html($(this).text());
					$('#month_hidden').val($(this).attr('id'));
				}
			});		
			
			$("#day_select .hide").each(function(){
				if($(this).text() == day){
					$(this).addClass('selected_active');
					$('.selected',$(this).parent()).attr('lang',$(this).attr('id')).html($(this).text());
					$('#day_hidden').val($(this).attr('id'));
				}
			});
		}
	});
	
	$('#flash_messages').each(function(){
		$(this).css('display', 'none');
		$(this).css('opacity', 0.65);
		$(this).fadeIn('slow');
		$('.close', $(this)).click(function(){ $($(this).parents('div')[0]).fadeOut(function(){ $(this).remove(); }); });
	});
	
	$('.votes li img').bind('click',function(){
		var eq = $(this).parent().children().index(this);
		var src = $(this).attr('src');
		
		var src_default = _public + 'images/star.jpg';
		$(this).parent().children('img').attr('src',src_default).removeClass('active');
		if(strpos(src,'star_active') === false){
			src = src.replace('star','star_active');
		}
		var i;
	
		for (i=1; i<=eq; i++){
			$(this).parent().children().eq(i).attr('src',src).attr('class','active');	
		}	
		
		var hidden = $(this).parent().attr('class');
		$('input[name="' + hidden + '"]').attr('value',eq);
	});
	
	$('.votes li img').hover(function(){
		var eq = $(this).parent().children().index(this);
		var src = $(this).attr('src');

		if($(this).hasClass('active') == false && strpos(src,'star_active') === false){
			src = src.replace('star','star_active');
		}
		
		var i;

		for (i=1; i<=eq; i++){
			$(this).parent().children().eq(i).attr('src',src);	
		}	

	},function(){
		var default_src = _public + 'images/star.jpg';
	
		for (i=1; i<=5; i++){
			if($(this).parent().children().eq(i).hasClass('active') == false){
				$(this).parent().children().eq(i).attr('src',default_src);	
			}	
		}	
	});
	
	function chose_phase_image(){
		var url = window.location.href;
		if(strpos(url,'/bg') !== false){
			$('.phase .slide[alt="slide_down"]').each(function(){
				$(this).parent().attr('style','background:#e7e7e7 url("' + _public + 'images/step_unactive_bg.jpg") no-repeat 0px top;');
			});
		}
	}
	chose_phase_image();
	
	$('.phase').live('click',function(){
		var direction = $(this).attr('title');
		var action = $('img',this).attr('alt');
		
		if(action == 'slide_up'){
			var now = this;
			$('#' + direction).slideUp('slow',function(e){
				var img = $('img',now).attr('src');
				img = img.replace('slide_up','slide_down');
				var img = $('img',now).attr('src',img);
				$('img',now).attr('alt','slide_down');
				$('h5,h5 span',now).css('background','none');
				$(now).attr('style','background:#e7e7e7 url("'+ _public + 'images/step_unactive.jpg") no-repeat left top;');
				chose_phase_image();
			});
		}
		
		if(action == 'slide_down'){
			var img = $('img',this).attr('src');
			img = img.replace('slide_down','slide_up');
			var img = $('img',this).attr('src',img);
			
			var now = this;
			$('#' + direction).slideDown('slow',function(e){
				$('img',now).attr('alt','slide_up');
				$('h5,h5 span',now).attr('style',null);
				$(now).attr('style',null);
			});
		}
	});
	
	$('#rooms_and_prices li,#rooms_page li').hover(
		function(){
			var prev_min_height = $(this).prev().css('min-height');
			$(this).prev().attr('style','background:none;').css('min-height',prev_min_height);
			$(this).css('background','#471f5b').css('color','#ededed');
			$('h2 a',this).attr('style','color:#ededed;');
		},
		function(){
			var prev_min_height = $(this).prev().css('min-height');
			$(this).prev().attr('style',null).css('min-height',prev_min_height);
			var this_min_height = $(this).css('min-height');
			$(this).attr('style',null).css('min-height',this_min_height);
			$('h2 a',this).attr('style',null);
		}
	);
	
	$('#rooms_and_prices li,#rooms_page li').click(function(){
		var url = $('h2 a',this).attr('href');
		window.location.href = url;
	});

	// carousel
	$('#fp_carousel ul li a').click(function(){
		var e = this;
		click_on_carousel(e,'user',int);
	});
	
	// pretty photo plugin
	$("a[id^='big_image']").click(function(){
		$('.pretty:eq(0)').trigger('click');
		return false;
	});
	
	$(".pretty").prettyPhoto();
	
	$('#valute_select li').click(function(){
		if(!$(this).hasClass('selected')){
			var val = $(this).attr('id');
			var url = window.location.href;
			
			if(strpos(url,'valute') === false){
				window.location.href = url+'&valute='+val;
			}else{
				if(strpos(url,'valute=euro') !== false){var new_url = url.replace('valute=euro','valute='+val);}
				if(strpos(url,'valute=usd') !== false){var new_url = url.replace('valute=usd','valute='+val);}
				if(strpos(url,'valute=bgn') !== false){var new_url = url.replace('valute=bgn','valute='+val);}
				
				window.location.href = new_url;
			}
		}
	});
	
	select_from_options('#valute_select');
	
	// CUSTOM SELECT
	
	select('#valute_select');
	select('#language_select');
	select('#year_select','#year_hidden');
	select('#month_select','#month_hidden');
	select('#day_select','#day_hidden',true);
	select('#hour_select','#hour_hidden',true);
	select('#minute_select','#minute_hidden',true);
	select('#header_minute_select','#minute_hidden',true);
	select('#days_select','#days_hidden');
	select('#room_select','#room_hidden');

	var url = get_url_params();
	
	if(url['send'] != null && url['send'] != ''){
		select_from_options('#month_select','#month_hidden');
		select_from_options('#hour_select','#hour_hidden');
		select_from_options('#minute_select','#minute_hidden');
		select_from_options('#day_select','#day_hidden');
		select_from_options('#days_select','#days_hidden');
		select_from_options('#year_select','#year_hidden');
		select_from_options('#room_select','#room_hidden');
		$('#days-text input').val(url['days']);
		$('#people input').val(url['people']);
	}
	
	if(url['selected_room'] != null && url['seselected_room'] != ''){
		var selected_room = url['selected_room'];
		
		$('#response_avaliable .avaliable_options .date input').each(function(){
			$(this).attr('checked',false);
			var value =	$(this).val();
			if(selected_room == value){
				$(this).attr('checked',true);
			}
		});
	}
	
	$('#response_avaliable .avaliable_options').hover(function(){
		$(this).css('background-color','#f4f4f4');
	},function(){
		$(this).attr('style',null);
	});
	
	$('#response_avaliable .avaliable_options').click(function(){
		$('.date input',this).attr('checked',true);
	});
	
	$('#submit_form,.header_book_btn').click(function(){
		var params = $('[name="booking"]').serialize();
		$('[name="booking"]').submit();
	});	
	
	var i = 0;
	$('#book_btn').click(function(){
		i++;
		if(i == 1){
			$.get(_root + 'booking/ajax_check_booking/',{},function(success){
				// on success
				if(success){
					var link = window.location.href;
					link = link.replace('step=2','step=3');
					window.location.href = link;
				}
			});
		}
	});
	
	$('#response_book_btn').click(function(){
		var link = window.location.href;
		
		if(strpos(link,'step')!== false){
			if(strpos(link,'step=3')!== false){link = link.replace('step=3','step=2');}
			if(strpos(link,'step=2')!== false){link = link.replace('step=2','step=2');}
		}else{
			link = link+'&step=2';
		}
		
		var params = $('[name="response_avaliable_room"]').serialize();
		if(strpos(link,'selected_room') !== false){
			link = link.replace('selected_room='+url['selected_room'],params);
			window.location.href = link;
		}else{
			window.location.href = link + '&' + params;
		}
	});
	
	

});

function select_from_options(selector,hidden){
	var url = get_url_params();
	var i = strpos(selector,'_');
	i--;
	var str = selector.substr(1,i);
	var value = url[str];
	
	if(value != null && value != ''){
		$(selector+" .hide").each(function(){
			var success = false;
			
			if($(this).attr('id') == value){
				success = true;
			}else{		
				if($(this).attr('id') == str+'_'+value){
					success = true;
				}
			}
			
			if(success == true){
				$(this).addClass('selected_active');
				$('.selected',$(this).parent()).attr('lang',$(this).attr('id')).html($(this).text());
				$(hidden).val($(this).attr('id'));
			}
		});	
	}
}

function get_url_params(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

	for(var i = 0; i < hashes.length; i++){

		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function select(selector,hidden,overflow){
	$(selector + ' .selected').click(function(){
		
		// close before opened drop downs
		var id = selector.replace('#','');
		$('ol').each(function(){
			if($(this).attr('id') != id){
				$('li',this).each(function(){
					if($(this).hasClass('active')){
						$(this).removeClass('active');
						$(this).addClass('hide');
					}
				});
				$('.selected',this).attr('style',null);
				$('.hide',this).attr('style',null);
				$(this).attr('style',null);
			}
		});	
		// end
				
		$(this).parent().attr('style','z-index:10000;');
				
		if(overflow == true){
			$(selector).attr('style','overflow:scroll;overflow-x:hidden;height:140px;z-index:10000;');
		}
		
		// set separator
		$(this).attr('style','border-bottom:1px solid #B0B0B0;');
		// end
		$(selector + ' .hide').removeClass('selected_active');
		
		// show drop down
		$('.hide',$(this).parent()).each(function() {
			$(this).removeClass('hide');
			$(this).addClass('active');
			
			// check if has selected and mark up 
			if($(selector + ' .selected').attr('lang') == $(this).attr('id')){
				$(this).addClass('selected_active');
			}
			// end
		});
		// end
		
		// animate on show
		$(selector + ' .active').hide().fadeIn('fast');
		// end
		
		// set selected params on click
		$(selector + ' .active').click(function (){
			var val = $(this).attr('id');
			$(selector + ' .selected').html($(this).text());
			$(hidden).val(val); 
			$(selector + ' .selected').attr('lang',val)
			
			// on complete hide drop down
			$(selector + ' .active').each(function(){
				$(this).removeClass('active');
				$(this).addClass('hide');
				$(selector).attr('style',null);
				$(selector + ' .hide').attr('style',null);
			});
			// end
			
			// remove bottom border
			$(selector + ' .selected').attr('style',null);
			// end
		});
		// end
		
		// hover animation
		$(selector + ' .active').hover(function(){
			$(this).css('background','#ccc');
		},function(){
			$(this).attr('style',null);
		});
		// end
	});
	
	// close drop downs if click on body
	$('body').click(function(e){
		if(e.target.id != $(selector + ' .selected').attr('id')){
			$(selector + ' .active').each(function(){
				$(this).removeClass('active');
				$(this).addClass('hide');
			});
			$(selector + ' .selected').attr('style',null);
			$(selector + ' .hide').attr('style',null);
			$(selector).attr('style',null);
		}
	});// end
}

function click_on_carousel(e,type,int){
	if(type == 'user'){
		int=window.clearInterval(int);
	}
	
	var page = $(e).text();
	$.get(_root + 'application/get_carousel/',{page: page},function(result){

		for(n in result){
			
			var title = result[n]['title'];
			var url = result[n]['url'];
			var description = result[n]['description'];
			var image = result[n]['image'];
			var page = result[n]['page'];
			
			$('#fp_carousel p').text(description);
			$('#fp_carousel .image').attr('href',url);
			$('#fp_carousel h4 a').text(title).attr('href',url);
			
			
			$('#fp_carousel a img.before').attr('src',image).removeClass('before').addClass('tmp').hide();
			$('#fp_carousel a img.active').addClass('before').removeClass('active');
			$('#fp_carousel a img.tmp').addClass('active').removeClass('tmp').fadeIn('medium');
			
	
			$("#fp_carousel ul li a").removeClass('active');
			$("#fp_carousel ul li a:contains('"+ page +"')").addClass('active');
			
		}
	},'json');
}

function strpos (haystack, needle, offset) {
    var i = (haystack + '').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}
function rtrim (str, charlist) {
    charlist = !charlist ? ' \\s\u00A0' : (charlist + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\\$1');
    var re = new RegExp('[' + charlist + ']+$', 'g');    return (str + '').replace(re, '');
}
