/**
 * Get specific cookie value by key
 * @return null | string
 */
function getCookie(c_name){
	if (document.cookie.length>0){
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1){
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}

/**
 * Set cookie
 */
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/**
 * Set cookies
 * @param string c_key cookies key
 * @param string c_value cookies value
 */
function executeCookie(c_key, c_value) {
    var myDomain = '/';
    var largeExpDate = new Date ();
    largeExpDate.setTime(largeExpDate.getTime() + (7 * 24 * 3600 * 1000));
    setCookie(c_key, c_value, largeExpDate, myDomain);
}

function addToFavorites(title, url) {

	if (window.sidebar) { // Mozilla
			window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE
		window.external.AddFavorite( url, title);
	}
	
}


function search_tabs_switcher() {
	
    var tab_active = getCookie('main_tab');

    if (tab_active == 'hotels') {
       $('#search_tab_hotels').attr('class', 'search_tab_hotels_on');
       $('#search_tab_holidays').attr('class', 'search_tab_holidays_off');
       $('#search_type_hotels').attr('checked', 'checked');
       $('#search_type_holidays').removeAttr('checked');
       $('#form_package').hide();
       $('#form_hotel').show();
    } else if (tab_active == 'holidays') {
       $('#search_tab_holidays').attr('class', 'search_tab_holidays_on');
       $('#search_tab_hotels').attr('class', 'search_tab_hotels_off');
       $('#search_type_holidays').attr('checked', 'checked');
       $('#search_type_hotels').removeAttr('checked');
       $('#form_hotel').hide();
       $('#form_package').show();
    }

}


function initialise_colorbox(){
	// ColorBox
	$(".iframe").colorbox({
		iframe:true,
		innerWidth:425,
		innerHeight:344
	});
	
	$(".fancy").colorbox({
	
	});	
}

$(function(){
		   

		   
	//Simple tabs
    function isTab(el_id) {
        var v = el_id.split('-');
        try {
            if (v[0] == 'tab') {
                var c = $('#tab-' + v[1] + '-content').get(0);
                //first time only
                var a = $('#simple-tabs').children('li').filter(':first').children('a').get(0);
                var firstv = a.id.split('-');
                if (!isTab.inittab_in_container) {
                   isTab.inittab_in_container = true;
                   $('#tab-' + firstv[1] + '-content').html($('#simple-tabs-body').html());
                }
                return c != null;
            } else {
                return false;
            }
        } catch(e) {}
    }
    isTab.inittab_in_container = false;

    $('#simple-tabs a').click(function(e){
        is_tab = isTab(e.target.id);
        if (is_tab != true) {
            return true;
        }

        //Disactivate all tabs
        $('#simple-tabs').children('li').each(function() {
            try {
                var cl = $(this).attr('class');
                var cl_words = cl.split('_');
                $(this).attr('class', cl_words[0] + '_' + cl_words[1] +  '_' +  cl_words[2] + '_off');
                var link = $(this).children('a[id^=tab]').get(0);
                $(link).removeAttr('class');
            }catch(e){
                alert(e.message);
            }
        });

        //Activate only clicked tab
        $(e.target).attr('class', 'hotel_tab_active');
        var tab_li_class = $(e.target).closest('li').attr('class');
        var li = tab_li_class.split('_');
        $(e.target).closest('li').attr('class', li[0] + '_' + li[1] +  '_' +  li[2] + '_on');

        //Set content
        var container = e.target.id + '-content';
        $('#simple-tabs-body').html($('#' + container).html());
		
		// Colorbox
		initialise_colorbox();
		
        return false;
		
    });

		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
    //Reads cookie and shows active tab at page loading
    search_tabs_switcher();

    //Search tab click  - hotels/holidays
	$('.box_search_tabs :input[name=search_type]').click(function(e){
        var tab_type = e.target.id;
        var tab_active = getCookie('main_tab');
		try {
			if (tab_type == 'search_type_hotels') {
                tab_active != 'hotels' ? executeCookie('main_tab', 'hotels') : false;
                search_tabs_switcher();
			} else {
                tab_active != 'holidays' ? executeCookie('main_tab', 'holidays') : false;
                search_tabs_switcher();
			}
		} catch(e) {
		}
	});
	
	
	// Show hide toggle number of rooms
    $('#no_rooms').change(function(e){
        var v = $(e.target).val();
        try {
            switch(v) {
                case '1':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1').css({'display':'block'});
                break;
                case '2':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1, #room-2').css({display:'block'});
                break;
                case '3':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1, #room-2, #room-3').css({'display':'block'});
                break;
                case '4':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1, #room-2, #room-3, #room-4').css({'display':'block'});
                break;
                case '5':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1, #room-2, #room-3, #room-4, #room-5').css({'display':'block'});
                break;
            }
        } catch(e) {}

    });

	
	
	
	

    //Datepickers set up
    try {
        //hotels picker
		var hotels_day = $('#hotels-date-day').val();
		var hotels_my = $('#hotels-date-month').val();
        var m_y_pair = hotels_my.split('-');
        var hotels_month = m_y_pair[0];
        var hotels_year = m_y_pair[1];
	    $("#datepicker-hotels").val(hotels_day + '.' + hotels_month + '.' + hotels_year);

        //holidays
  		var holidays_day = $('#holidays-date-day').val();
		var holidays_my = $('#holidays-date-month').val();
        var m_y_pair = holidays_my.split('-');
        var holidays_month = m_y_pair[0];
        var holidays_year = m_y_pair[1];
	    $("#datepicker-holidays").val(holidays_day + '.' + holidays_month + '.' + holidays_year);
	}catch(e){}

    function mindate(id_el_day, id_el_my) {
		var day_el = $('#' + id_el_day).get(0);
		var my_el  = $('#' + id_el_my).get(0);

        try {
            var day = day_el.options[0].value;
            var my  = my_el.options[0].value;
            var m_y_pair = my.split('-');
            var month    = m_y_pair[0] - 1;
            var year     = m_y_pair[1];
        } catch(e) {};

        return {d:day, m:month, y:year};
    }

    function maxdate(id_el_day, id_el_my) {
		var day_el = $('#' + id_el_day).get(0);
		var my_el  = $('#' + id_el_my).get(0);

        try {
            var day = day_el.options[day_el.options.length-1].value;
            var my  = my_el.options[my_el.options.length-1].value;

            var m_y_pair = my.split('-');
            var month    = m_y_pair[0] - 1;
            var year     = m_y_pair[1];
        } catch(e){};
        return {d:day, m:month, y:year};
    }

    function pickerValueSetter(picker) {
        var day = $("#" + picker + "-date-day").val();
        var my = $("#" + picker + "-date-month").val();
        var m_y_pair = my.split('-');
        var month    = m_y_pair[0];
        var year     = m_y_pair[1];

        $("#datepicker-" + picker).val(day + '.' + month + '.' + year);
    }

    var min_hot_date =  mindate('hotels-date-day', 'hotels-date-month');
    var max_hot_date =  maxdate('hotels-date-day', 'hotels-date-month');
    var min_hol_date =  mindate('holidays-date-day', 'holidays-date-month');
    var max_hol_date =  maxdate('holidays-date-day', 'holidays-date-month');

	$("#datepicker-hotels").datepicker({
		showOn: 'button',
		buttonImage: '/images/icon_calendar.png',
		buttonImageOnly: true,
		buttonText: 'Calendar', //'alt' and 'title' attributes
		defaultDate: new Date(hotels_year, hotels_month, hotels_day),
		minDate: new Date(min_hot_date.y, min_hot_date.m, min_hot_date.d),
		maxDate: new Date(max_hot_date.y, max_hot_date.m, max_hot_date.d),
		dateFormat: 'dd.mm.yy',

		onSelect: function(dateText) {
			var d =	dateText.split('.');
            try {
    			$('#hotels-date-day').children('[value=' + d[0] + ']').attr('selected', 'selected');
        		$('#hotels-date-month').children('[value=' + d[1] + '-' + d[2] + ']').attr('selected', 'selected');
            } catch (e) {
            }
		},

		//updates calendar 'start' date
		beforeShow: function(input) {
			var d =	input.value.split('.');
            try {
                $("#datepicker-hotels").datepicker('setDate', new Date(d[2], d[1]-1, d[0]));
            } catch (e) {
            }
		}
	});

    $('#hotels-date-day, #hotels-date-month').change(function(e){
        pickerValueSetter('hotels');
	});

        //holidays picker

	$("#datepicker-holidays").datepicker({
		showOn: 'button',
		buttonImage: '/images/icon_calendar.png',
		buttonImageOnly: true,
		buttonText: 'Calendar', //'alt' and 'title' attributes
		defaultDate: new Date(holidays_year, holidays_month, holidays_day),
		minDate: new Date(min_hol_date.y, min_hol_date.m,  min_hol_date.d),
		maxDate: new Date(max_hol_date.y, max_hol_date.m,  max_hol_date.d),
		dateFormat: 'dd.mm.yy',

		onSelect: function(dateText) {
			var d =	dateText.split('.');
            try {
    			$('#holidays-date-day').children('[value=' + d[0] + ']').attr('selected', 'selected');
        		$('#holidays-date-month').children('[value=' + d[1] + '-' + d[2] + ']').attr('selected', 'selected');
            } catch (e) {
            }
		},

		//updates calendar 'start' date
		beforeShow: function(input) {
			var d =	input.value.split('.');
            try {
                $("#datepicker-holidays").datepicker('setDate', new Date(d[2], d[1]-1, d[0]));
            } catch (e) {
            }
		}
	});
    $('#holidays-date-day, #holidays-date-month').change(function(e){
        pickerValueSetter('holidays');
	});
	
	
	// Colorbox
	initialise_colorbox();
	
	// Review textarea character count
	$('#review').simplyCountable({
		counter: '#charcount',
		maxCount: 900,
		strictMax: true
	});
	
});

