﻿/// <reference path="jquery-1.3.2-vsdoc.js"/>

$(window).bind("load", function() {
    // Set the anchors in position
    positionAnchors();
    var posanchor = self.document.location.hash.substring(1);
    if (!posanchor || posanchor == "home") {
        $("body").stop().scrollTo("#home", 2000);
    } else {
        $("body").stop().scrollTo("#" + posanchor + "anchor", 500);
    }
});

jQuery.event.add(window, "resize", positionAnchors);

$(document).ready(function() {

    positionAnchors();
    document.body.style.overflow = "hidden";
    $(document).ajaxStart(function() { $('#ajax-submit-indicator').show(); }).ajaxStop(function() { $('#ajax-submit-indicator').hide(); });
    // Homepage flash slider
    //var rdmSetId = Math.floor(Math.random() * 3) + 1;
    var flashvars = {};
    var params = { menu: "false", wmode: "transparent", scale: "noscale" };
    var attributes = {};
    swfobject.embedSWF("/set1.swf", "innerflashfream", "874", "444", "9.0.0", "expressInstall.swf", flashvars, params, attributes);

    DoContactUsFormAjaxSubmit($("#contactUsSubmit"));

    $(".scrollToHome").click(function() {
        $("body").stop().scrollTo("#home", 2000);
        return false;
    });

    $(".scrollToOurwork").click(function() {
        $("body").stop().scrollTo("#ourworkanchor", 2000);
        return false;
    });

    $(".scrollToContactus").click(function() {
        $("body").stop().scrollTo("#contactusanchor", 2000);
        return false;
    });

    $(".scrollToOurpeople").click(function() {
        $("body").stop().scrollTo('#ourpeopleanchor', 2000);
        return false;
    });

    $('.jsScrollPanel').jScrollPane({ scrollbarWidth: 8, scrollbarMargin: 10 });

    $('.project a').hover(function() {
        var desc = $(this).prev().html();
        $('#projectSpeech .projectBrief').html(desc);
        $('#projectSpeech').fadeIn('fast');
    }).mouseout(function() {
        $('#projectSpeech').fadeOut('fast');
    });

    $('#aboutus-design-anchor').hover(function() { $('.aboutus-design-speech').fadeIn('fast'); }).mouseout(function() { $('.aboutus-design-speech').fadeOut('fast'); });
    $('#aboutus-design-anchor').click(function() { return false; });
    $('#aboutus-developer-anchor').hover(function() { $('.aboutus-developer-speech').fadeIn('fast'); }).mouseout(function() { $('.aboutus-developer-speech').fadeOut('fast'); });
    $('#aboutus-developer-anchor').click(function() { return false; });
});

function positionAnchors() {
    // position pages based on browser window size
    var winh = $(window).height();
    var winw = $(window).width();

    // homepage
    var homeleftoffset = 0;
    if (winw < 1248 && winw > 843) {
        homeleftoffset = parseInt(227 - (winw - 843) / 2);
    } else if (winw < 843) {
        homeleftoffset = 227;
    }
    var hometopoffset = 230 - parseInt((winh - 560) / 2);
    // case study page
    var ourworkTopOffset = 140 - parseInt((winh - 610) / 2);
    // about us page
    var aboutusTopOffset = 60 - parseInt((winh - 650) / 2);
    // contact us page
    var contactTopOffset = 160 - parseInt((winh - 610) / 2);

    $("#home").css("top", hometopoffset);
    $("#home").css("left", homeleftoffset);
    $("#ourworkanchor").css("top", ourworkTopOffset);
    $("#ourpeopleanchor").css("top", aboutusTopOffset);
    $("#ourpeopleanchor").css("left", "90px");
    $("#contactusanchor").css("top", contactTopOffset);
}

function DoContactUsFormAjaxSubmit(submitElement) {
    $(submitElement).click(function() {
        var $form = $(this).closest('form');

        $.ajax({
            type: "POST",
            url: $form.attr('action'),
            data: $form.serialize(),
            dataType: "text",
            error: function(xhr, status, error) {
                alert("error" + error);
            },
            success: function(response) {
                if (response.toString().indexOf("Succeeded") != -1) {
                    $("#success-stamp").fadeIn("fast");
                    $(".validation-error").removeClass("validation-error");
                } else {
                    var fieldsToValidate = ["Name", "Email", "Message"];
                    //alert(fieldsToValidate);
                    var responseStr = response.toString();
                    for (var i = 0; i < fieldsToValidate.length; i++) {
                        if (responseStr.indexOf("<strong>" + fieldsToValidate[i] + "</strong>") != -1) {
                            //alert("error:" + fieldsToValidate[i]);
                            $("#" + fieldsToValidate[i]).addClass("validation-error");
                        }
                        else {
                            $("#" + fieldsToValidate[i]).removeClass("validation-error");
                        }
                    }
                }
            }
        });
        return false;
    });
}
