﻿var wallItems;
var imgQueue;
var activeItems;
var queuePos;
var currentThumb;
var videoPlaying = true;
var isReady = true;

$(document).ready(function() {
    if (isReady) {
        $(".main").css("background", "#6B727A url('/App_Themes/default/images/bg.png') repeat-x 0px -42px");
        var params = {
            wmode: "transparent"
        };
        /*if($.cookie("firstVisit")!="false"){
        swfobject.embedSWF("/shared/flash/home.swf", "IntroHolder", "996", "561", "9.0.0", false, null, params,null,noFlash);
        
        $.cookie("firstVisit","false",{path:'/'});
        }else{
        videoComplete();
        }*/

        queuePos = 0;
        videoComplete();

        loadWallItems();
        isReady = false;
    }

});

function noFlash(e) {
    if (!e.success) {
        videoComplete();
    } else {
        window.IntroHolder = document.forms[0].IntroHolder;
        setTimeout(function() {
            if ($("#IntroHolder").length == 0) {
                videoComplete();
            }
        }, 500);
    }
}

function loadWallItems() {
    $.ajax({
        type: "POST",
        data: "{'action':'getwalldata'}",
        url: "/Services/ProjectWall.asmx/GetWallItems",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            var parser;
            try {
                parser = new ActiveXObject("Microsoft.XMLDOM");
                parser.async = "false";
                parser.loadXML(response);
                response = parser;
            } catch (e) {
                response = new DOMParser().parseFromString(response, "text/xml");
                response = response.firstChild;
            }
            wallItems = new Array();
            imgQueue = new Array();
            activeItems = new Array();
            
            var holder = $("#wallHolder");
            var links = holder.find("a");
            $(response).find("t").each(function() {
                var project = $(this).parent();
                var wallItem = new Object();
                wallItem.category = project.find("c").text();
                wallItem.description = project.find("d").text();
                wallItem.thumbnail = project.find("i").text();
                wallItem.title = $(this).text();
                wallItem.client = project.find("l").text();
                wallItem.associatedLink = links[wallItems.length];
                wallItems.push(wallItem);
            });

            buildImageQueue();
        },
        failure: function(msg) {
            alert("Error retrieving data. Please try again later.");
        }
    });
}

var isLoaded = false;
function buildImageQueue() {
    if (isLoaded == true) {
        removeItemFromWall();
    }

    // Load new items

    for (var i=0; i<6; i++ ) {
        var wallItem = wallItems[queuePos];
        imgQueue.push(wallItem);

        queuePos++
        if (queuePos >= wallItems.length) {
            queuePos = 0;
        }        
    }
    
    // Add queue item to the wall...
    if (!isLoaded) {
        addItemToWall();
    }
    
    // Set isLoaded
    isLoaded = true;
}

function removeItemFromWall() {
    var currentIndex = Math.floor(activeItems.length * Math.random());
    var wallItem = activeItems[currentIndex];

    // Remove the item from the queue
    // alert(activeItems.length);
    activeItems.splice(currentIndex, 1);
    
    var link = $(wallItem.associatedLink);
    var img = $("img", link);

    $(link).css("background-color", "transparent");
    $(img).fadeOut(500, function() {
        $(link).css("display", "none");
        $(this).remove();
    });
    
    //$(img).fadeOut(500).remove();

    if (activeItems.length > 0) {
        setTimeout(removeItemFromWall, 1);
    } else {
        for (var wi = 0; wi < imgQueue.length; wi++) {
            var wallItem = imgQueue[wi];
            $(wallItem.associatedLink).css("display", "block");
        }
        setTimeout(addItemToWall,500);
    }
}

function addItemToWall(){
    var currentIndex = Math.floor(imgQueue.length * Math.random());
    var wallItem = imgQueue[currentIndex];

    activeItems.push(wallItem);
    imgQueue.splice(currentIndex, 1);
    
    var img = $('<img src="' + wallItem.thumbnail + '" alt="' + wallItem.title + '"/>');
    $(wallItem.associatedLink).append(img);
    
    img.load(function() {
        $(this).parent().css("background-color", "#ffffff");
        $(this).fadeOut(0).css("visibility", "visible").fadeIn(500);
    });

    if (imgQueue.length > 0) {
        setTimeout(addItemToWall, 100);
    } else {
        setTimeout(buildImageQueue, 10000); 
    }
}

function getWallSize() {
    return $("#wallHolder").find("a").length;
}

function videoComplete() {
    videoPlaying = false;
    $("#IntroWrapper").remove();
}

function videoStart() {
    $("#IntroWrapper").css("background", "none");
}

function getWallLink(idx) {
    return $("#wallHolder").find("a:eq(" + idx + ")").attr("href");
}

function showDetails(idx) {
    if (wallItems != null) {
        insertDetails(wallItems[idx].title,wallItems[idx].client,wallItems[idx].description);
    }
}

/*function hoverCat(obj) {
    $("#wallHolder").css("background-image","none");
    if (wallItems != null && !videoPlaying) {
        var count = 0;
        var cat = obj.innerText.toLowerCase();
        insertDetails(cat,$("#"+cat+"Copy").html());
        $("#wallHolder").find("a").each(function() {
            if (wallItems[count].category.indexOf(cat)<0) {
                $(this).css("background-color","transparent").find("img").stop(true).fadeTo(500, .1);
            }
            count++;
        });
    }
}

function hoverOffCat(obj) {
    if (wallItems != null && !videoPlaying) {
        var count = 0;
        var cat = obj.innerText.toLowerCase();
        resetText();
        $("#wallHolder").find("a").each(function() {
            if (wallItems[count].category.indexOf(cat)<0) {
                $(this).find("img").stop(true).fadeTo(500, 1);
            }
            count++;
        });
    }
}*/

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
