/**********************************************************************
* Copyright 2008 Amblique :: www.amblique.com :: All Rights Reserved *
**********************************************************************/
jQuery(document).ready(function() {

    // IE6 transparent png fix, browser independent
    try {
        if (typeof (supersleight) != "undefined") {
            if ($.isFunction(supersleight.run)) {
                supersleight.run();
            }
        }
    } catch (e) { }

    // PopupWindow :: http://plugins.jquery.com/project/PopupWindow
    var profiles = {
        popupToolsPrint: {
            height: 500,
            width: 600,
            center: 1,
            scrollbars: 1,
            status: 0,
            resizable: 1
        },
        popupToolsEmail: {
            height: 400,
            width: 500,
            center: 1,
            scrollbars: 1,
            status: 0,
            resizable: 1
        }
    };
    $(".tool").popupwindow(profiles);

    // # click protection
    $('a[href=#]').click(function() {
        return false;
    });

    // smooth scroll
    $('a[href=#top]').bind("click", function() {
        var $target = $(this.hash);
        $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
        if ($target.length) {
            var targetOffset = $target.offset().top;
            $('html,body').animate({
                scrollTop: targetOffset
            }, 1000);
        }
        return false;
    });

    // Hide & Show definition lists
    $('.toggle-a dl dd').hide();
    $('.toggle-a dl dt a').click(function() {
        if ($(this).parent().parent().attr("class") == "active") {
            $(this).parent().parent().removeClass("active");
            $(this).parent().parent().find('dd').hide(400);
        }
        else {
            $(this).parent().parent().addClass("active");
            $('.toggle-a dl.active dd').show(400);
        }
        return false;
    });

    // enable all toggle-switches (fade in/out effect)
    $(".toggle-switch").click(function() {
        var speed = 400; // fade in/out speed
        var active = "active"; // active state css class
        var togglee = "." + $(this).toggleClass(active).attr("rel");
        if ($(this).hasClass(active))
            $(togglee).fadeIn(speed);
        else
            $(togglee).fadeOut(speed);
    });


    // Hide & Show definition lists
    $('.elementToggle .row .area').hide();
    $('.elementToggle .row h3 a').click(function() {
        var bActive = false;
        if ($(this).parent().parent().hasClass("row-active")) {
            bActive = true;
        }
        $('.elementToggle .row').removeClass("row-active");
        $('.elementToggle .row .area').hide();
/*
        if ($(this).parent().parent().hasClass("row-active")) {
            $(this).parent().parent().removeClass("row-active");
            $(this).parent().parent().find('.area').hide(400);
        }
        else {
*/
        if (!bActive)   {
            $(this).parent().parent().addClass("row-active");
            $('.elementToggle .row-active .area').show(400);
        }
        return false;
    });


    // enable all toggle-switches (fade in/out effect)
    $(".toggle-switch").click(function() {
        var speed = 400; // fade in/out speed
        var active = "active"; // active state css class
        var togglee = "." + $(this).toggleClass("row-active").attr("rel");
        if ($(this).hasClass("row-active"))
            $(togglee).fadeIn(speed);
        else
            $(togglee).fadeOut(speed);
    });


    /* Flash Embed */
    $('div#flashShowcase').flashembed({
        src: 'flash/flashShowcase.swf',
        wmode: 'transparent',
        height: 358
    });
});


/**********************************
* Other scripts
**********************************/
Cufon.replace('h2.contentTitle', {
    textShadow: '0 0 rgba(0, 0, 0, 0)'
});
Cufon.replace('.elementProjectDetail h3 a', {
    textShadow: '0 0 rgba(0, 0, 0, 0)'
});


if (window.attachEvent)
    window.attachEvent("onload", setListeners);

function setListeners() {
    inputList = document.getElementsByTagName("INPUT");
    for (i = 0; i < inputList.length; i++) {
        inputList[i].attachEvent("onpropertychange", restoreStyles);
        inputList[i].style.backgroundColor = "";
    }
    selectList = document.getElementsByTagName("SELECT");
    for (i = 0; i < selectList.length; i++) {
        selectList[i].attachEvent("onpropertychange", restoreStyles);
        selectList[i].style.backgroundColor = "";
    }
}

function restoreStyles() {
    if (event.srcElement.style.backgroundColor != "" && event.srcElement.style.backgroundColor != "#a0d0ff") {
        event.srcElement.style.backgroundColor = "#a0d0ff"; /* color of choice for AutoFill */
        document.all['googleblurb'].style.display = "block";
    }
}

function clearText(thefield, clearType) {
    if (thefield.defaultValue == thefield.value) {
        thefield.value = ""
    }
}

// javascript version of the string.Format method
// 'hello {0}. my name is {1}, and you are {0}'.format('David', 'Jeff');
String.prototype.format = function() {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}