﻿if (typeof String.prototype.trim !== 'function') {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/, '');
    }
}

function toStr(val) {
    var result = "";

    if (val.length > 0) {
        for (var i = 0; i < val.length - 1; i++) {
            result += val[i] + "@@@";
        }

        result += val[i];
    }

    return result;
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + toStr(value) +
            ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}

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 "";
}

jQuery.fn.extend({
    fadeHtml: function (content) {
        var element = this;
        this.fadeOut(function () { element.html(content); element.fadeIn(); });
    }
});

function mdb_love(id) {
    $.ajax({
        url: '/ajax/artists/love',
        type: 'POST',
        data: "artistId=" + id,
        success: function (response) {
            // update status element
            $('#loveb').fadeHtml(response);
        }
    });
    mdb_subscribe(id);
}

function mdb_subscribe(id) {
    $.ajax({
        url: '/ajax/artists/subscribe',
        type: 'POST',
        data: "artistId=" + id,
        success: function (response) {
            // update status element
            //$('#subscribeb').fadeHtml(response);
        }
    });
}

function mdb_love_venue(id) {
    $.ajax({
        url: '/ajax/venues/love',
        type: 'POST',
        data: "venueId=" + id,
        success: function (response) {
            // update status element
            $('#loveb').fadeHtml(response);
        }
    });
}

function mdb_attend_event(id) {
    $.ajax({
        url: '/ajax/events/attend',
        type: 'POST',
        data: "eventId=" + id,
        success: function (response) {
            // update status element
            $('#loveb').fadeHtml(response);
        }
    });
}

$(document).ready(function () {
    var triggers = $(".modalInput").overlay({
        expose: {
            color: '#333',
            loadSpeed: 200,
            opacity: 0.9
        },
        relative: true,
        closeOnClick: true
    });
	
	if ($("#submitcomment").length != 0) {
	$("#submitcomment").click(function (e) {
	   var commentval = $("#addcomment > textarea[name=comment]").val();
	   var parentIdVal = $("#addcomment > input[name=parent]").val();

		   $.ajax({
			   url: $("#submitcomment").attr("postUrl"),
			   type: 'post',
			   dataType: 'json',
			   data: { comment: commentval, parentId: parentIdVal },
			   success: function (data) {
				   $('#submitCommentResult').html(data.msg + "<br /><br />");
			   }
		   });
	   });
	}

    /*if ($("#notificationtoolbar").html().trim() != "") {
        if ($.browser.msie) {
            $("#notificationtoolbar").show();
        } else {
            $("#notificationtoolbar").fadeIn(700);
        }
    }

    $("#notificationtoolbar > div.btn").click(function () { $("#notificationtoolbar").fadeOut(700); });*/
});

function geo_success(p) {
    if ($("#locationKnown").text() == "no") {
        document.location.href = "/location/SetLocation?lat=" + p.coords.latitude + "&lon=" + p.coords.longitude;
    }
}

function geo_error() {
}

if (geo_position_js.init()) {
    geo_position_js.getCurrentPosition(geo_success, geo_error);
}
