﻿$(function() {
    $(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);
    $.ajaxSetup({
        // Disable caching of AJAX responses    
        cache: false
    });


    //$(".divGalleryItem a").lightBox();

    $(".divGalleryItem a").lightBox({
        overlayBgColor: '#000000',
        overlayOpacity: 0.6,
        imageLoading: '/assets/images/lightbox-ico-loading.gif',
        imageBtnClose: '/assets/images/lightbox-btn-close.gif',
        imageBtnPrev: '/assets/images/lightbox-btn-prev.gif',
        imageBtnNext: '/assets/images/lightbox-btn-next.gif',
        containerResizeSpeed: 350,
        txtImage: 'Imagem',
        txtOf: 'en'
    });

});


/* Helper methods*/

function validateEmail(elementValue) {
    var emailPattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/";
    if (emailPattern.test(elementValue) == false) {
        return false;
    }
    return true;
}


// /Date(-2208988800000+0000)/ Represents 01/01/1900 00:00:00, Minimum Date
function ConvertFromWCFDateTime(value) {
    if (IsNullOrEmpty(value)) return "";
    if (value.toString().indexOf("/Date(") == -1) return "";
    if (value == "/Date(-2208988800000+0000)/") return "";

    var string = new String(value.toString());

    while (string.indexOf("/") > -1)
        string = string.replace("/", "");
    $("#divNewMsg").dialog("open");
    return eval("new " + string);
}


function ConvertToWCFDateTime(value) {
    if (IsNullOrEmpty(value)) return "/Date(-2208988800000+0000)/";
    
    return "/Date(" + value.getTime().toString() + "+" + value.getTimezoneOffset().toString() + ")/";
}


function IsNullOrEmpty(value) {
    return value == undefined || value == null || value == "";
}


function IsNumeric(value) {
    if (IsNullOrEmpty(value)) return false;

    var isNumeric = false;
    try {
        isNumeric = !isNaN(value);
    }
    catch (exception) { }
    return isNumeric;
}

/* End helper methods */


$(document).ready(function() {
    $("#txtSignUp").live('click', function() {
        $(this).attr('class', 'TxtForm');
    });
});


function RunPreloader(Id) {
    $(Id).html('<img src="/assets/images/ajax-loader.gif" alt="Loading" />');
}


function StopPreloader(Id) {
    $(Id).html('');
}


function ShowMenu(Id) {
    $('#div' + Id).show();
}


function HideMenu(Id) {
    $('#div' + Id).hide();
}


function ShowProduct(ProductId) {
    $("#divProduct").dialog("close");
    $("#divProduct").dialog({
        width: 700,
        height: 600,
        resizable: false,
        autoOpen: false,
        modal: true,
        closeOnEscape: true
    })

    $("#divProduct").dialog("open");
}


function MsgBox(Title, Message, Width, Height) {

    $("#MsgBox").html(Message);
    $("#MsgBox").attr('Title', Title);

    $("#MsgBox").dialog("close");
    $("#MsgBox").dialog({
        width: Width,
        height: Height,
        resizable: false,
        autoOpen: false,
        modal: true,
        closeOnEscape: true
    })
    $("#MsgBox").dialog("open");
}


function SignUp() {

    if(IsNullOrEmpty($('#txtSignUp').val())){
        $(this).attr('class', 'TxtFormError');        
        return;
    }
    
    var siteAccount = {
        Email: $("#txtSignUp").val()
    }

    $.ajax({
        type: "POST"
		    , url: "/Code/Services/TrapletService.svc/SignUp"
            , contentType: "application/json; charset=utf-8"
		    , dataType: "json"
		    , data: JSON.stringify({ siteAccount: siteAccount })
		    , success: function(data) {
		        // Success
		        MsgBox("Newsletter", "Thank you for signing up to our email newsletters", 380, 150);
		        $("#txtSignUp").html("");
		    }
		    , error: function(data) {
		        alert('Error');
		    }
    });    
}

