﻿///<reference path="jquery.intellisense.js"/>


jQuery(document).ready(function() {
    $("#caNnsearchForm").submit(function() {
        //alert("submit");
        var uid = GetUid();
        $("<input type='hidden' name='caNnuid' id='caNnuid'/>").attr("value", uid).appendTo(this);
        var params = $(this).serialize();
        //return false;
        $("input.rateSearchBtn").hide();
        $("div.loading").show();
        $.post("/SearchSp.php", $(this).serialize(), renderPrice, "html");
        return false;
    });

    $("#caNnAddReview").submit(function() {
        //alert("submit");
        var uid = GetUid();
        $("<input type='hidden' name='caNnuid' id='caNnuid'/>").attr("value", uid).appendTo(this);
        var params = $(this).serialize();
        //return false;
        $("#reviewdiv").toggle();
        $.post("/AddReview.php", $(this).serialize(), renderResult, "html");
        return false;
    });
    $("#caNnsearchFormMp").submit(function() {
        //alert("submit");
        var uid = GetUid();
        $("<input type='hidden' name='caNnuid' id='caNnuid'/>").attr("value", uid).appendTo(this);
        var params = $(this).serialize();
        //return false;
        $("input.searchHotelsButton").hide();
        $("div.loading").show();
        $.post("/SearchMp.php", $(this).serialize(), renderHotels, "json");
        return false;
    });

    $("#reviewLink").click(function() {        
        $("#reviewdiv").toggle();
        return false;
    });

}
);

function renderResult(data, textStatus){
    if (textStatus == "success") {
        $(data).insertAfter("#reviewdiv");
    }
    else {
        $("#reviewdiv").append("Thank you for review.");
    }
    return false;
}
function renderHotels(data, textStatus) {
    if (textStatus == "success") {
        if (data.status == "ok") {
            document.location = "/bookhotels/index.php?" + data.href;
        } else {
            alert(data.text);
        }
    } else {
        alert("error");
    }
    $("div.loading").hide();
    $("input.searchHotelsButton").show();    
    return false;
}

function renderPrice(data,textStatus) {
    if (textStatus == "success") {
        //alert("ok");
        $("#hotelPrice").find("div.bodyBlock").html(data);
        $("#hotelPrice").slideDown("slow");
    } else {
        alert("error");
    }
    $("div.loading").hide();
    $("input.rateSearchBtn").show();    
    return false;
}
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 S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

/**
* Cookie plugin
*/
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
jQuery.fn.center = function() {
    var w = $(window);
    this.css("position", "absolute");
    this.css("top", (w.height() - this.height()) / 2 + w.scrollTop() + "px");
    this.css("left", (w.width() - this.width()) / 2 + w.scrollLeft() + "px");
    return this;
}

