﻿/// <reference path="jquery-1.5.1-vsdoc.js" />
/// <reference path="jquery-1.5.1.js" />
/// <reference path="jquery-ui-1.8.11.js" />

function formatJSONDate(jsonDate) {
    var newDate = new Date(parseInt(jsonDate.substr(6)));
    return newDate;
}

function validateEmail(email) {

    if (email.length == 0) {
        return false;
    }
    
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if( !emailReg.test(email) ) {
	    return false;
	} else {
	    return true;
	}
}

//Execute Page Load

$(document).ready(function () {

    //Disable Right Click
    $(document).bind("contextmenu", function (e) {
        return false;
    });

    //Set Tooltip on all elements with aa attribute tooltip
    $("[tooltip]").tipTip({ attribute: 'tooltip', delay: 200 });

    //Change Menu Buttons On Hover
    $(".menuButton").mouseover(function () {
        var overImage = $(this).attr("overimg");
        $(this).attr("src", overImage);
    }).mouseleave(function () {
        var image = $(this).attr("img");
        $(this).attr("src", image);
    });

    //Homepage Scripts

    $("#btnSubscribe").click(function () {

        var content = '<div style="font-family:verdana; font-size:12px;">';
        var email = $("#txtEmail").val();
        var save = false;

        if (!validateEmail(email)) {
            content += 'Endereço de email inválido!';
        }
        else {
            save = true;
        }

        if (save) {

            var url = 'Subscriber/Subscribe';

            $.post(url,
                { email: email },
                function (data) {

                    if (data == "Error") {
                        content += '<h2>Oops!</h2><p>Ocorreu um erro a registar o seu endereço de email. Por favor tente mais tarde.</p>';
                    }
                    else if (data == "EmailAlreadyExists") {
                        content += '<h2>Oops!</h2><p>O seu endereço já se encontra registado.</p>';
                    }
                    else {
                        content += '<h2>Obrigado!</h2><p>O seu email foi registado com sucesso. A partir de hoje receberá diariamente a nossa newsletter com as melhores anunciantes de Portugal.</p>';
                    }

                    content += '</div>';

                    $.fancybox(
		                        content,
		                        {
		                            'autoDimensions': false,
		                            'width': 350,
		                            'height': 'auto',
		                            'transitionIn': 'elastic',
		                            'transitionOut': 'elastic'
		                        }
	                );

                }
            );
        }
        else {

            content += '</div>';

            $.fancybox(
		                content,
		                {
		                    'autoDimensions': false,
		                    'width': 350,
		                    'height': 'auto',
		                    'transitionIn': 'elastic',
		                    'transitionOut': 'elastic'
		                }
	        );
        }

        $("#txtEmail").focus();

    });

    // Profile Scripts
    var galleryHeight = $(window).height() - 115;
    var galleryWidth = $(window).width();

    var $profileGallery = $('#profileGallery').tn3({
        delay: 5000,
        width: galleryWidth,
        height: galleryHeight,
        skin: 'tn3',
        skinDir: 'Scripts/widget/tn3/skins',
        imageClick: "fullscreen",
        image: {
            maxZoom: 1.5,
            crop: false,
            clickEvent: "click",
            transitions: [{
                type: "grid",
                duration: 460,
                gridX: 1,
                gridY: 8,
                sort: "random",
                method: "scale",
                partDuration: 360
            }]
        }
    });

    $("#btnHighlightVote").click(function () {

        var voteUrl = "Payment/VotarDestaque";

        $.getJSON(voteUrl,
                    { id: $(this).attr("ProfileID") },
                    function (data) {

                        if (data) {

                            var voteResultMessage = '<div style="font-family:verdana; font-size:12px;">';

                            if (data.ResultCode == "0") {
                                voteResultMessage += "<h1>Obrigado!</h1>";
                                voteResultMessage += 'O seu voto foi registado com sucesso!<br /><br />';
                            }
                            else {
                                voteResultMessage += "<h1>Atenção!</h1>";
                                voteResultMessage += data.ErrorMessage + "<br /><br />";
                            }

                            voteResultMessage += "</div>";

                            $.fancybox(
		                            voteResultMessage,
		                            {
		                                'autoDimensions': false,
		                                'width': 350,
		                                'height': 'auto',
		                                'transitionIn': 'elastic',
		                                'transitionOut': 'elastic'
		                            }
	                        );
                        }


                    }
        );

    });


    $(window).resize(function () {

        var newGalleryHeight = $(window).height() - 115;
        var newGalleryWidth = $(window).width();

        $profileGallery.resize(newGalleryWidth, newGalleryHeight);

    });

    $("#btnHideContainer").click(function () {
        $("#divContentRightContainer").animate({ width: 'hide' });
        $("#divContentRightShow").toggle();
    });

    $("#btnShowContainer").click(function () {
        $("#divContentRightContainer").animate({ width: 'show' });
        $("#divContentRightShow").toggle();
    });

    $("#btnPlayVideo").fancybox({
        'width': 650,
        'height': 433,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });

    $(".openMessagesBox").fancybox({
        'width': 780,
        'height': 650,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });

    $(".openMapBox").fancybox({
        'width': 780,
        'height': 650,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });

    //Como Anunciar Page (Prospect)
    $("#btnSendProspect").click(function () {

        var isValid = false;
        var result = "false";
        var prospectName = $("#txtProspectName").val();
        var prospectPhone = $("#txtProspectPhone").val();
        var prospectEmail = $("#txtProspectEmail").val();
        var prospectMessage = $("#txtProspectMessage").val();

        var mailBody = prospectMessage + ". Mensagem enviada por " + prospectName + ". Telefone: " + prospectPhone + ". Email: " + prospectEmail;

        var prospectContentResult = '<div style="font-family:verdana; font-size:12px;">';
        var registerPropspectUrl = 'Mail/Send';

        if (!validateEmail(prospectEmail)) {
            prospectContentResult += 'Endereço de email inválido!';
        }
        else {
            isValid = true;
        }

        if (isValid) {
            $.post(registerPropspectUrl,
                    { from: prospectEmail, to: 'miguel.madeira@gmail.com', subject: 'Novo Contacto do Site Momentos de Prazer.com', body: mailBody },

                    function (data) {

                        if (data == "SUCCESS") {
                            prospectContentResult += '<p><h2>Obrigado!</h2><p>O seu pedido foi registado com sucesso.</p><p>Brevemente irá receber um contacto do nosso departamento comercial que irá esclarecer qualquer questão.</p>';
                        }
                        else {
                            prospectContentResult += '<h2>Oops!</h2><p>Ocorreu um erro a registar o pedido. Por favor tente mais tarde.</p><p>' + data + '</p>';
                        }

                        prospectContentResult += '</div>';

                        $.fancybox(
		                            prospectContentResult,
		                            {
		                                'autoDimensions': false,
		                                'width': 350,
		                                'height': 'auto',
		                                'transitionIn': 'elastic',
		                                'transitionOut': 'elastic'
		                            }
	                    );

                        $("#txtProspectName").val('');
                        $("#txtProspectPhone").val('');
                        $("#txtProspectEmail").val('');
                        $("#txtProspectMessage").val('');

                    }
            );
        }
        else {

            prospectContentResult += '</div>';

            $.fancybox(
		                prospectContentResult,
		                {
		                    'autoDimensions': false,
		                    'width': 350,
		                    'height': 'auto',
		                    'transitionIn': 'elastic',
		                    'transitionOut': 'elastic'
		                }
	        );

            $("#txtProspectEmail").focus();
        }



    });


});
