﻿/*===============================================================================================================================
Author: Luthium™
Author URL: www.luthium.com
Copyright reserverd: 2011 - Luthium™
===============================================================================================================================*/
function InitializeAccordion() {
    $('.accord-content').hide();
    $('.accord-header').click(function () {
        $(this).next('div.accord-content').css("display", "block").siblings('div.accord-content').css("display", "none");
        $(this).addClass('selected').siblings('div.accord-header').removeClass('selected');
    });
    $('.accord-header:first').addClass('selected');
    $('.accord-content:first').css("display", "block");
}

function ResetContactFields() {
    $(".captcha").css("display", "none");
    $(".send-panel").css("display", "none");
    $("#txtName").val("");
    $("#txtCompany").val("");
    $("#txtEmail").val("");
    $("#txtContactNumber").val("");
    $("#txtMessage").val("");
}

function ResetEmailFields() {
    $(".captcha").css("display", "none");
    $(".send-panel").css("display", "none");
    $("#txtSubscribeName").val("");
    $("#txtSubscribeEmail").val("");
    document.getElementById("chkConfirmSubscribe").checked = false;
}

function ResetEmailFieldsUI() {
    $("#txtSubscribeName").css("background-color", "#1E1D1D");
    $("#txtSubscribeName").css("border", "1px solid #FFFFFF");
    $("#txtSubscribeName").css("color", "#FFFFFF");

    $("#txtSubscribeEmail").css("background-color", "#1E1D1D");
    $("#txtSubscribeEmail").css("border", "1px solid #FFFFFF");
    $("#txtSubscribeEmail").css("color", "#FFFFFF");
}

var targets;
var actuals;
var canvas;

function DrawCaptcha(canvasTarget) {
    targets = new Array();
    actuals = new Array();

    if (canvas != null) {
        $('#' + canvasTarget).empty();
    }

    canvas = Raphael(canvasTarget, 430, 200);
    var circles = new Array();

    circles[0] = canvas.circle(50, 50, 20).animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 8 }, 1000);
    circles[1] = canvas.circle(50, 150, 20).animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 8 }, 1000);
    circles[2] = canvas.circle(125, 100, 20).animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 8 }, 1000);
    circles[3] = canvas.circle(200, 50, 20).animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 8 }, 1000);
    circles[4] = canvas.circle(200, 150, 20).animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 8 }, 1000);
    circles[5] = canvas.circle(275, 100, 20).animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 8 }, 1000);
    circles[6] = canvas.circle(350, 50, 20).animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 8 }, 1000);
    circles[7] = canvas.circle(350, 150, 20).animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 8 }, 1000);

    var randomIndex = 0;
    var randomIndexes = "";
    while (targets.length < 3) {
        randomIndex = Math.floor(Math.random() * 8);
        if (randomIndexes.indexOf(randomIndex + ",") == -1) {
            var target = circles[randomIndex];
            targets[targets.length] = target;
            randomIndexes += randomIndex + ",";
            target.animate({ fill: "#FFFFFF", stroke: "#DAAD28", "stroke-width": 11 }, 1000);
            target.attr({ cursor: "hand" });
            target.click(function () {
                var circle = this;
                var actualsCount = actuals.length;
                if (actualsCount > 0) {
                    for (var index = 0; index < actualsCount; index++) {
                        if (actuals[index] != circle) {
                            AddActual(circle);
                        }
                    }
                }
                else {
                    AddActual(circle);
                }
            });
        }
    }
    $('.captcha').slideDown('fast');
}

function AddActual(object) {
    actuals[actuals.length] = object;
    object.animate({ fill: "#FFFFFF", stroke: "#4E4E4E", "stroke-width": 10 }, 300);
    object.attr({ cursor: "default" });
    if (actuals.length == 3)
        $(".send-panel").slideDown("slow");
}

function InitiateCaptcha() {
    if (VerifyEnquiryFields()) {
        $("#input-form").slideUp('slow', function () { DrawCaptcha('enquirecaptchacanvas'); });
    }
}

function InitiateEmailSubscribe() {
    if (VerifyEmailSubscribeFields()) {
        $("#pnlSubscribeToEmailModal").modal();
        DrawCaptcha('subscribecaptchacanvas');
    }
}

function VerifyEnquiryFields() {
    if (!ValidateInputValue($("#txtName")))
        return false;

    if (!ValidateInputValue($("#txtEmail")))
        return false;

    return true;
}

function VerifyEmailSubscribeFields() {

    ResetEmailFieldsUI();

    if (!ValidateInputValue($("#txtSubscribeName")))
        return false;

    if (!ValidateInputValue($("#txtSubscribeEmail")))
        return false;

    if (!document.getElementById("chkConfirmSubscribe").checked) {
        return false;
    }

    return true;
}

function ValidateInputValue(control) {
    if ((control.val() == "") || (control.val() == "Not Selected")) {
        control.css("background-color", "#F1E4E4");
        control.css("border", "2px solid #FF0000");
        control.css("color", "#121212");
        control.focus();
        return false;
    }
    return true;
}

function SendRequest() {
    $(".sending").fadeIn("slow");
    $(".send-enquiry").css("display", "none");

    var subject = $("#txtSubject").html();
    var name = $("#txtName").val();
    var company = $("#txtCompany").val();
    var email = $("#txtEmail").val();
    var contactNumber = $("#txtContactNumber").val();
    var message = $("#txtMessage").val();

    SuccessBrokers.Web.services.ServiceInterface.SendEnquiry(subject, name, company, email, contactNumber, message, OnSendRequestDone);
}

function OnSendRequestDone() {
    $(".sending").css("display", "none");
    $(".thanks").fadeIn("slow");
    $('#enquirecaptchacanvas').empty();
    window.setTimeout("$('#pnlEnquiryModal').fadeOut('slow', function(){ResetContactFields(); $.modal.close(); $('.send-enquiry').css('display', ''); $('.thanks').css('display', 'none');})", 2000);
}

function SubscribeToEmailBase() {
    $(".sending").fadeIn("slow");
    $(".send-enquiry").css("display", "none");

    var name = $("#txtSubscribeName").val();
    var email = $("#txtSubscribeEmail").val();

    SuccessBrokers.Web.services.ServiceInterface.SubscribeToEmailBase(name, email, OnSubscribeToEmailBaseDone);
}

function OnSubscribeToEmailBaseDone() {
    $(".sending").css("display", "none");
    $(".thanks").fadeIn("slow");
    $('#subscribecaptchacanvas').empty();
    window.setTimeout("$('#pnlSubscribeToEmailModal').fadeOut('slow', function(){ResetEmailFields(); ResetEmailFieldsUI(); $.modal.close(); $('.send-enquiry').css('display', ''); $('.thanks').css('display', 'none');})", 2000);
}

//=====================================================================================================================================================================================================
function slideShow() {

    //Set the opacity of all images to 0
    $('#gallery a').css({ opacity: 0.0 });

    //Get the first image and display it (set it to full opacity)
    $('#gallery a:first').css({ opacity: 1.0 });

    //Set the caption background to semi-transparent
    $('#gallery .caption').css({ opacity: 0.9 });

    //Resize the width of the caption according to the image width
    $('#gallery .caption').css({ width: $('#gallery a').find('img').css('width') });

    //Get the caption of the first image from REL attribute and display it
    $('#gallery .content').html($('#gallery a:first').find('img').attr('rel'))
	.animate({ opacity: 0.9 }, 400);

    //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval('gallery()', 6000);

}

function gallery() {

    //if no IMGs have the show class, grab the first image
    var current = ($('#gallery a.show') ? $('#gallery a.show') : $('#gallery a:first'));

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $('#gallery a:first') : current.next()) : $('#gallery a:first'));

    //Get next image caption
    var caption = next.find('img').attr('rel');

    //Set the fade in effect for the next image, show class has higher z-index
    next.css({ opacity: 0.0 })
	.addClass('show')
	.animate({ opacity: 1.0 }, 1000);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000)
	.removeClass('show');

    //Set the opacity to 0 and height to 1px
    $('#gallery .caption').animate({ opacity: 0.0 }, { queue: false, duration: 0 }).animate({ height: '1px' }, { queue: true, duration: 300 });

    //Animate the caption, opacity to 0.9 and heigth to 100px, a slide up effect
    $('#gallery .caption').animate({ opacity: 0.9 }, 100).animate({ height: '50px' }, 500);

    //Display the content
    $('#gallery .content').html(caption);
}
