function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
var verIE = getInternetExplorerVersion();
// PRELOADING IMAGES
jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}
$(document).ready(function(){
// FUNCTION FOR ROUNDED CORNERS
	$(function(){		
			$('.corners').corner({
			  tl: { radius: 5 },
			  tr: { radius: 5 },
			  bl: { radius: 5 },
			  br: { radius: 5 },
			  antiAlias: true,
			  autoPad: true,
			  validTags: ["div"] });
			$('.corners_acc').corner({
			  tl: { radius: 5 },
			  tr: { radius: 5 },
			  bl: { radius: 5 },
			  br: { radius: 5 },
			  antiAlias: true,
			  autoPad: true,
			  validTags: ["div"] });
			$('.extras_info').corner({
			  tl: { radius: 5 },
			  tr: { radius: 5 },
			  bl: { radius: 5 },
			  br: { radius: 5 },
			  antiAlias: true,
			  autoPad: true,
			  validTags: ["div"] });
			$('#alert').corner({
			  tl: { radius: 5 },
			  tr: { radius: 5 },
			  bl: { radius: 5 },
			  br: { radius: 5 },
			  antiAlias: true,
			  autoPad: true,
			  validTags: ["div"] });
	});


// FUNCTIONS FOR GALLERY	
	$('.gallery').hover(function(){
		$(this).animate({ opacity:'1'}, 150);
	}, function(){
			if($(this).css('cursor')!='wait' && $(this).css('cursor')!='default')
			{
				$(this).animate({ opacity:'0.5'}, 150);
			}
	});
	
	 $('.gallery').click(function() {
		$('.gallery').css({'opacity':'0.5'});
		$(this).css({'cursor':'wait','opacity':'1'});
		var img_css=$(this);
		var old_img_src=$('#gallery_large').attr("src");
		var current_src=$(this).attr("id").substr(7);
		$(".gallery[src='"+$('#gallery_large').attr("src")+"']").animate({ opacity:'0.5'}, 150);
			$('#gallery_large').fadeOut(300, function(){
				$(this).attr('src','../media/'+current_src).fadeIn(600, function(){
					$('.gallery').css({'opacity':'0.5','cursor':'pointer'});
					img_css.css({'opacity':'1','cursor':'default'});
				});
			}); 
	     });
// INITIALIZE DATE PICKER AND ACCORDION		
	if(document.getElementById("datepicker"))
	{
		$('#datepicker').datepicker({
			firstDay: 1,
			dateFormat: 'dd-mm-yy',
			hideIfNoPrevNext: true,
			minDate: +2,
			altField: '#choosen_date', 
			altFormat: 'yy-mm-dd',
			duration: '',
			beforeShow: function(){
				if(document.getElementById("break_response"))
				{
			      $('#break_list').css({ 'display':'none'});
			      $('#star_list').css({ 'display':'none'});
			      $('#county_list').css({ 'display':'none'});
				}	
			}
		});
	}
	if(document.getElementById("accordion"))
	{
		  $('#accordion ul').hide();
		  $('.corners_acc').click(
		    function() {
		        $(this).next().slideToggle('fast');	
		      }
		    );
	}

	$('.corners_acc').hover(function(){
		$(this).css({'text-decoration' : 'underline'});
	}, function(){
		$(this).css({'text-decoration' : 'none'});
	});

// FUNCTIONS FOR REPLACING NORMAL SELECT BOX	
	
	function rebindBreaktype()
	{
		$("input[name='break_type']").attr('checked', false);
		$("input[name='break_price']").attr('checked', false);
		$('#break_response').text('SELECT BREAK...');
		$('#star_response').text('SELECT PRICE...');
		$('.break_response').hover(function(){
			$(this).css({ 'background-color':'#275c84', 'color':'#ffffff'});
		}, function(){
			$(this).css({ 'background-color':'#ffffff', 'color':'#666666'});
		});	
			$('.break_response').click(function () {	
			var levels = $(this).text().substr(0,18);
			$('#break_response').text(levels);
			$('#break_list').toggle('fast');
	  		$('#star_response').text('SELECT PRICE...');
	    });    
		$("input[name='break_type']").change(function(){
			var given_date=document.getElementById("choosen_date").value;
			var choosen_package_id=$("input[name='break_type']:checked").attr('value');
			$.get("include/search_ajax.php", { choosen_date: given_date, after_break: 'yes', package_id: choosen_package_id},function(data){
		    $("#star_list").html(data);
			rebindBreakprice();
			});
		});
	}
	function rebindBreakprice()
	{
		$("input[name='break_price']").attr('checked', false);
		$('.star_response').hover(function(){
			$(this).css({ 'background-color':'#275c84', 'color':'#ffffff'});
		}, function(){
			$(this).css({ 'background-color':'#ffffff', 'color':'#666666'});
		});
			$('.star_response').click(function () {	
			var levels = $(this).text().substr(0,18);
			  $('#star_response').text(levels);
			  $('#star_list').toggle('fast');
	    });    
	}	

	$('.response').hover(function(){
		$(this).css({backgroundPosition: '0 -35px'})
	}, function(){
		$(this).css({backgroundPosition: '0 0'})
	});

// SELECT BREAK TYPE
	$('#break_response').click(function () {
		if($('#choosen_date').attr('value')=='')
		{
	        $('#star_list').css({ 'display':'none'});
	        $('#county_list').css({ 'display':'none'});
			$('#alert_text').html('<strong>Message</strong><hr class="hr">You need to choose desired <strong>Break Date</strong> before you can choose the <strong>Break Type</strong>.<br /><br /><a id="close">CLOSE THIS MESSAGE BOX</a>');
			$('#alert_wrap').css({ 'display':'block'});
			$('#close').click(function(){
				$('#alert_wrap').css('display','none');
			});
		}
		else
		{
			var size = $("#break_list > li").size() * 22;
			$('.radiolist#break_list').css({ 'height':size+'px'});
		    $('#star_list').css({ 'display':'none'});
		    $('#county_list').css({ 'display':'none'});
		    $('#break_list').toggle('fast');		
		}
    });    
// SELECT STAR RATING
	$('#star_response').click(function () {
		if($("input[name='break_type']:checked").size() == 0)
		{
	        $('#county_list').css({ 'display':'none'});
	        $('#break_list').css({ 'display':'none'});
			$('#alert_text').html('<strong>Message</strong><hr class="hr">You need to choose desired <strong>Break Type</strong> before you can choose the <strong>Break Price</strong>.<br /><br /><a id="close">CLOSE THIS MESSAGE BOX</a>');		
			$('#alert_wrap').css({ 'display':'block'});
			$('#close').click(function(){
				$('#alert_wrap').css('display','none');
			});
		}
		else
		{
		   var size = $("#star_list > li").size() * 22;
	   	   $('.radiolist#star_list').css({ 'height':size+'px'});
	       $('#county_list').css({ 'display':'none'});
	       $('#break_list').css({ 'display':'none'});
	       $('#star_list').toggle('fast');
		} 
    });    
// SELECT COUNTY
	$('.county_response').hover(function(){
		$(this).css({ 'background-color':'#275c84', 'color':'#ffffff'});
	}, function(){
		$(this).css({ 'background-color':'#ffffff', 'color':'#666666'});
	});

	$('#county_response').click(function () {
		  var size = $("#county_list > li").size() * 22;
		  if($("#county_list > li").size() > 10){
			size=10 * 22;
		  }
		  $('.radiolist#county_list').css({ 'height':size+'px'});
	      $('#star_list').css({ 'display':'none'});
	      $('#break_list').css({ 'display':'none'});
	      $('#county_list').toggle('fast');
    });    
	$('.county_response').click(function () {	
		var levels = $(this).text().substr(0,18);
		  $('#county_response').text(levels);
		  $('#county_list').toggle('fast');
    });    
	rebindBreaktype();
	rebindBreakprice();
// WAITING FOR RESULTS EFFECT		

	$('#searching_bg_trans').css({ 'display':'none'});
	$('#searching_img').css({ 'display':'none'});
// EXTRAS / SPECIALS INFORMATION ROLL OVER
	$('.extras_icon').hover(function(e){
		var y = e.pageY - 390;
		$(this).next('.extras_info').css({ 'position':'absolute', 'top':y+'px', 'display' :'inline'});
		}, function(){
		$(this).next('.extras_info').css({'display' :'none'});
	});

// ACTION AFTER USER CHANGES THE DATE- AJAX CHOOSING AVAILABLE BREAK TYPES
	$('#datepicker').change(function(){
		var given_date=document.getElementById("choosen_date").value;
			$.get("include/search_ajax.php", { choosen_date: given_date, after_date: 'yes'},function(data){
		    $("#break_list").html(data);
			rebindBreaktype();
			});
	});
// VALIDATE SEARCH
	$('#search_button').click(function(){
		if($("#datepicker").attr('value') == '' || $("#choosen_date").attr('value') == ''){
			$('#alert_text').html('<strong>Message</strong><hr class="hr">To proceed with your search you need to choose all the fields - <strong>Date, Break Type, Break Price, Region</strong>.<br /><a id="close">CLOSE THIS MESSAGE BOX</a>');		
			$('#alert_wrap').css({ 'display':'block'});
			$('#close').click(function(){
				$('#alert_wrap').css('display','none');
			});
			return false;
		}
		if($("input[name='break_type']:checked").size() == 0){
			$('#alert_text').html('<strong>Message</strong><hr class="hr">To proceed with your search you need to choose all the fields - <strong>Date, Break Type, Break Price, Region</strong>.<br /><a id="close">CLOSE THIS MESSAGE BOX</a>');		
			$('#alert_wrap').css({ 'display':'block'});
			$('#close').click(function(){
				$('#alert_wrap').css('display','none');
			});
			return false;
		}
		if($("input[name='break_price']:checked").size() == 0){
			$('#alert_text').html('<strong>Message</strong><hr class="hr">To proceed with your search you need to choose all the fields - <strong>Date, Break Type, Break Price, Region</strong>.<br /><a id="close">CLOSE THIS MESSAGE BOX</a>');		
			$('#alert_wrap').css({ 'display':'block'});
			$('#close').click(function(){
				$('#alert_wrap').css('display','none');
			});
			return false;
		}
		if($("input[name='break_area']:checked").size() == 0){
			$('#alert_text').html('<strong>Message</strong><hr class="hr">To proceed with your search you need to choose all the fields - <strong>Date, Break Type, Break Price, Region</strong>.<br /><a id="close">CLOSE THIS MESSAGE BOX</a>');		
			$('#alert_wrap').css({ 'display':'block'});
			$('#close').click(function(){
				$('#alert_wrap').css('display','none');
			});
			return false;
		}	
	});
// JUMP MENU TO CHANGING SEARCH PAGES	
	$(".page_jump").change(function(){
			window.location = $(this).val();
    });
	$(".page_jump").css({'visibility':'visible'});
/*	
	$("*", document.body).click(function(){
	        $('#star_list').css({ 'display':'none'});
	        $('#break_list').css({ 'display':'none'});
	        $('#county_list').css({ 'display':'none'});
	});
*/	

});

