$(function () {
    $.superbox();
    var curD = new Date();
    var curDay = curD.getDate();


    $("#hotelCheckin").datepicker({
        minDate: new Date(curD.getFullYear(), addZero((parseInt(curD.getMonth()))), addZero(curD.getDate())),
        dateFormat: 'yy-mm-dd', 
        onSelect: function (dateText, inst) {
            var m = new Date($('#hotelCheckin').datepicker('getDate'));
            var s = new Date($('#hotelCheckin').datepicker('getDate'));
            m.setDate(m.getDate() + 1);
            s.setDate(s.getDate() + 1);
            $('#hotelCheckout').datepicker('option', 'minDate', m);
            $('#hotelCheckout').datepicker('setDate', s)
        }
    });
    curD.setDate(curDay + 1);
    $("#hotelCheckout").datepicker({
        minDate: new Date(curD.getFullYear(), addZero((parseInt(curD.getMonth()))), addZero(curD.getDate())),
        dateFormat: 'yy-mm-dd'
    });
    $('#compareHotel').click(function () {

    });

    $('#compareMulHotel').click(function () {
        $("#vienasearchFormMp").submit();
    });


    $("#AddReview").submit(function () {
        //alert("submit");
        var uid = GetUid();
        $("<input type='hidden' name='vienauid' id='vienauid'/>").attr("value", uid).appendTo(this);
        var params = $(this).serialize();
        //return false;
        //$("#reviewdiv").toggle();
        $.post("/AddReview.php", $(this).serialize(), renderResult, "html");
        return false;
    });

    $("#vienasearchForm").submit(function () {
		if ($('#hotelCheckin').val().length == 0 || $('#hotelCheckout').val().length == 0) {
            alert('Choose a date');
            return false;
        }
        //alert("submit");
        var uid = GetUid();
        $("<input type='hidden' name='vienauid' id='vienanuid'/>").attr("value", uid).appendTo(this);
        var params = $(this).serialize();
        //return false;
        //$("input.rateSearchBtn").hide();
        //$("div.loading").show();
        $.post("/SearchSp.php", $(this).serialize(), renderPrice, "json");
        return false;
    });



    $('.rating').each(function (i) {
        var star = $(this).attr('title');
        $(this).text(rating_stars(star));
    });

    // $('.bl_bt_sendRew').click

    /*
	
	
    var startRating = 
    .attr('title')*/

});


function rating_stars(rating) {
    var stars = [];
    for (i = 0; i < 5; i++) {
        if (i < rating) {
            stars += String.fromCharCode(parseInt("2605", 16));
        } else {
            stars += String.fromCharCode(parseInt("2606", 16));
        }
    }
    return stars;
}

function addZero (num){
	if (num.toString().length == 1){
		return '0'+parseInt(num);
	}
	return num;
}

function GetUid() {
    //alert("first uid " + uid);           
    /*var c_uid = $.cookie('uid');
    var temp_uid = "";
    if (c_uid != null) {
        temp_uid = c_uid;
        //alert("old uid " + temp_uid);
        $.cookie('uid', temp_uid, { path: '/', expires: 365 });
    }
    else {*/
        temp_uid = guid();
        //alert("new uid " + temp_uid);
       // $.cookie('uid', temp_uid, { path: '/', expires: 365 });
   // }
    return temp_uid;
}

function renderResult(data, textStatus) {
    if (textStatus == "success") {
        //$(data).insertAfter("#reviewdiv");
        alert('Thank you.');
    }
    else {
        //$("#reviewdiv").append("Thank you for review.");
        alert('error');
    }
    return false;
}

function renderPrice(data, textStatus) {
    if (textStatus == "success") {
        var tmpHref = data.href;
        if (data.label != undefined) {
            tmpHref += '&label='+data.label;
        }
        window.location = tmpHref;
    } else {
        alert("error");
    }
    return false;
}


function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}



