﻿var xmlHttp = null;
var xmlHttpTwitter = null;
var xmlHttpSubscribe = null;
var xmlHttpContact = null;
var xmlHttpClient = null;
var xmlHttpRibbon = null;

function CreateXmlHttpObject() {
    var objXMLHttp = null

    if (window.XMLHttpRequest) {
        objXMLHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp
}

function getPosterous(update) {
    xmlHttp = CreateXmlHttpObject()
    if (xmlHttp == null) {
        alert("Browser does not support AJAX");
        return;
    }

    xmlHttp.onreadystatechange = getPosterous_stateChanged;
    xmlHttp.open("GET", "includes/ajaxresponse.asp?f=getPosterous&update=" + update, true);
    xmlHttp.send(null);
}

function getPosterous_stateChanged() {

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        document.getElementById('posterous').innerHTML = xmlHttp.responseText;
    }
}


function getTwitter(update) {
    xmlHttpTwitter = CreateXmlHttpObject()
    if (xmlHttpTwitter == null) {
        alert("Browser does not support AJAX");
        return;
    }

    xmlHttpTwitter.onreadystatechange = getTwitter_stateChanged;
    xmlHttpTwitter.open("GET", "includes/ajaxresponse.asp?f=getTwitter&update=" + update, true);
    xmlHttpTwitter.send(null);
}

function getTwitter_stateChanged() {

    if (xmlHttpTwitter.readyState == 4 || xmlHttpTwitter.readyState == "complete") {
        document.getElementById('twitter').innerHTML = xmlHttpTwitter.responseText;
    }
}

function menuHover(menuID, state) {
    if (state == 'on') {
        document.getElementById(menuID).style.backgroundImage = "url('../img/" + menuID + "Back.jpg')";
    }
    else {
        document.getElementById(menuID).style.backgroundImage = "";
    }
}

function checkEmail() {

    var email = document.getElementById("subscribeEmail").value;

    var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

    if (pattern.test(email)) {
        xmlHttpSubscribe = CreateXmlHttpObject()
        if (xmlHttpSubscribe == null) {
            alert("Browser does not support AJAX");
            return;
        }

        xmlHttpSubscribe.open("GET", "includes/ajaxresponse.asp?f=subscribe&email=" + email, true);
        xmlHttpSubscribe.send(null);
        document.getElementById("subscribeEmail").value = 'Thanks, you\'re on our list';
    }
    else {
        document.getElementById("subscribeEmail").style.color = 'red';
        document.getElementById("subscribeEmail").value = 'Please enter a valid email address';
    }
}


function checkContact() {

    var name = document.getElementById("contactName").value;
    var email = document.getElementById("contactEmail").value;
    var message = document.getElementById("contactMessage").value;
    var canSend = true;

    var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

    if (name.length == 0) {
        document.getElementById("contactName").style.color = 'red';
        document.getElementById("contactName").value = 'Please enter your name';
        canSend = false;
    }
    if (!pattern.test(email)) {
        document.getElementById("contactEmail").style.color = 'red';
        document.getElementById("contactEmail").value = 'Please enter a valid email address';
        canSend = false;
    }
    if (message.length == 0) {
        document.getElementById("contactMessage").style.color = 'red';
        document.getElementById("contactMessage").value = 'Please enter your message';
        canSend = false;
    }

    if (canSend) 
    {
        xmlHttpContact = CreateXmlHttpObject()
        if (xmlHttpContact == null) {
            alert("Browser does not support AJAX");
            return;
        }

        xmlHttpContact.open("GET", "includes/ajaxresponse.asp?f=contact&email=" + email + "&name=" + encodeURI(name) + "&message=" + encodeURI(message), true);
        xmlHttpContact.send(null);
        document.getElementById("contactLabels").innerHTML = '';
        document.getElementById("contactInputs").innerHTML = 'Thanks for your message.<br /><br />We\'ll respond as soon as we can.';
    }

}

function getClient(clientID) {
    document.getElementById('latestWorkContainer').style.color = 'silver';
    document.getElementById('latestWorkContainerTitle').style.color = 'silver';
    document.getElementById('latestWorkContainerLink').style.color = 'silver';
    xmlHttpClient = CreateXmlHttpObject()
    if (xmlHttpClient == null) {
        alert("Browser does not support AJAX");
        return;
    }

    xmlHttpClient.onreadystatechange = getClient_stateChanged;
    xmlHttpClient.open("GET", "includes/ajaxresponse.asp?f=getClient&id=" + clientID, true);
    xmlHttpClient.send(null);
}

function getClient_stateChanged() {

    if (xmlHttpClient.readyState == 4 || xmlHttpClient.readyState == "complete") {
        document.getElementById('latestWorkContainer').innerHTML = xmlHttpClient.responseText;
        document.getElementById('latestWorkContainer').style.color = '';
        document.getElementById('latestWorkContainerTitle').style.color = '';
        document.getElementById('latestWorkContainerLink').style.color = '';
    }
}

function showTab(tabName) {
    document.getElementById('tabConText').style.display = 'none';
    document.getElementById('tabMarText').style.display = 'none';
    document.getElementById('tabWebText').style.display = 'none';

    document.getElementById('tabCon').style.backgroundImage = 'url(img/tabBack.jpg)';
    document.getElementById('tabMar').style.backgroundImage = 'url(img/tabBack.jpg)';
    document.getElementById('tabWeb').style.backgroundImage = 'url(img/tabBack.jpg)';

    document.getElementById('tabCon').style.color = '#005985';
    document.getElementById('tabMar').style.color = '#005985';
    document.getElementById('tabWeb').style.color = '#005985';

    document.getElementById(tabName + 'Text').style.display = 'block';
    document.getElementById(tabName).style.backgroundImage = 'url(img/' + tabName + 'Selected.jpg)';
    document.getElementById(tabName).style.color = 'white';
}

function albumScroller() {

    $(function () {
        buildThumbs();

        function buildThumbs() {
            $('div.album').each(function () {
                var width = 0;

                var wrapper = $(this).find('.st_thumbs_wrapper');
                wrapper.find('.st_thumbs a').each(function () {
                    width += $(this).outerWidth(true);
                });

                var thumbs = $(this).find('.st_thumbs');
                thumbs.css('width', width + 'px');

                makeScrollable(thumbs, wrapper);
            });
        }

        function makeScrollable(thumbs, wrapper) {
            var width = wrapper.innerWidth();

            wrapper.scrollLeft(0);

            var leftBuffer = 100;
            var rightBuffer = 100;

            //When user move mouse over menu
            wrapper.unbind('mousemove').bind('mousemove', function (e) {
                var xPos = e.pageX - wrapper.offset().left - leftBuffer;
                var xMax = wrapper.innerWidth() - rightBuffer;
                if (xPos > 0 && xPos < xMax) {
                    var perc = xPos / (xMax - leftBuffer);
                    var scrollAmt = thumbs.outerWidth(true) - wrapper.innerWidth();
                    wrapper.scrollLeft(perc * scrollAmt);
                }
            });
        }
    });
}

function getWorkRibbon() {

    service = encodeURI(document.getElementById('selService').value);
    sector = encodeURI(document.getElementById('selSector').value);
    search = encodeURI(document.getElementById('selSearch').value);
    var firstWork = '0';

    if (document.getElementById('firstWork')) {
        firstWork = document.getElementById('firstWork').value;
    }

    xmlHttpRibbon = CreateXmlHttpObject()
    if (xmlHttpRibbon == null) {
        alert("Browser does not support AJAX");
        return;
    }

    xmlHttpRibbon.onreadystatechange = getWorkRibbon_stateChanged;
    xmlHttpRibbon.open("GET", "includes/ajaxresponse.asp?f=getWorkRibbon&service=" + service + "&sector=" + sector + "&search=" + search + "&firstWork=" + firstWork, true);
    xmlHttpRibbon.send(null);
}

function getWorkRibbon_stateChanged() {

    if (xmlHttpRibbon.readyState == 4 || xmlHttpRibbon.readyState == "complete") {
        document.getElementById('workRibbon').innerHTML = xmlHttpRibbon.responseText;
        //document.getElementById('workCard').innerHTML = xmlHttpRibbon.responseText;
        setTimeout("getWork(document.getElementById('firstWork').value);",150)
        setTimeout("document.getElementById('firstWork').value = '';",300)
        setTimeout("albumScroller();", 500);
    }
}

function getWork(workID) {
    xmlHttpClient = CreateXmlHttpObject()
    if (xmlHttpClient == null) {
        alert("Browser does not support AJAX");
        return;
    }

    xmlHttpClient.onreadystatechange = getWork_stateChanged;
    xmlHttpClient.open("GET", "includes/ajaxresponse.asp?f=getWork&workID=" + workID, true);
    xmlHttpClient.send(null);
}

function getWork_stateChanged() {

    if (xmlHttpClient.readyState == 4 || xmlHttpClient.readyState == "complete") {
        document.getElementById('workCard').innerHTML = xmlHttpClient.responseText;
    }
}

function expandBlogFrame() {
    if (typeof blogSmallT != "undefined") clearTimeout(blogSmallT);

    document.getElementById('blogBack').style.visibility = 'visible';

    blogBigT = setTimeout("blogBig();", 1000);
}

function retractBlogFrame() {
    if (typeof blogBigT != "undefined") clearTimeout(blogBigT);

    document.getElementById('blogBack').style.visibility = 'hidden';

    blogSmallT = setTimeout("blogSmall();", 1000);
}

function blogSmall() {
    document.getElementById('menuStrip').style.zIndex = '100';
    document.getElementById('footer').style.zIndex = '100';
    document.getElementById('blogPage').style.position = 'absolute';
    document.getElementById('blogFrame').style.top = '346px';
    document.getElementById('blogFrame').style.height = '471px';
    $("body").css("overflow", "visible");
}

function blogBig() {
    document.getElementById('menuStrip').style.zIndex = '0';
    document.getElementById('footer').style.zIndex = '0';
    document.getElementById('blogPage').style.position = 'fixed';
    document.getElementById('blogFrame').style.top = '0px';
    document.getElementById('blogFrame').style.height = '100%';
    $("body").css("overflow", "hidden");
}
