﻿//delay plugin
jQuery.timer = function (time, func, callback) {
    var a = { timer: setTimeout(func, time), callback: null }
    if (typeof (callback) == 'function') { a.callback = callback; }
    return a;
};

jQuery.clearTimer = function (a) {
    clearTimeout(a.timer);
    if (typeof (a.callback) == 'function') { a.callback(); };
    return this;
};

//cookie plugin
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        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(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        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 { // only name given, get cookie
        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]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
/*
* jQuery Color Animations
* Copyright 2007 John Resig
* Released under the MIT and GPL licenses.
*/

(function (jQuery) {
    // We override the animation for all of these color styles
    jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function (i, attr) {
        jQuery.fx.step[attr] = function (fx) {
            try {
                if (fx.state == 0) {
                    fx.start = getColor(fx.elem, attr);
                    fx.end = getRGB(fx.end);
                }
            } catch(e) { 
                fx.start = getColor(fx.elem, attr);
                fx.end = getRGB("#ffffff");
            }
            if(fx.end==undefined) {
                fx.end = getRGB("#ffffff");
            }
            try {
                fx.elem.style[attr] = "rgb(" + [
				    Math.max(Math.min(parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				    Math.max(Math.min(parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				    Math.max(Math.min(parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			    ].join(",") + ")";
            } catch(e) {     
            }
        }
    });

    // Color Conversion functions from highlightFade
    // By Blair Mitchelmore
    // http://jquery.offput.ca/highlightFade/

    // Parse strings looking for color tuples [255,255,255]
    function getRGB(color) {
        var result;

        // Check if we're already dealing with an array of colors
        if (color && color.constructor == Array && color.length == 3)
            return color;

        // Look for rgb(num,num,num)
        if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
            return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

        // Look for rgb(num%,num%,num%)
        if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
            return [parseFloat(result[1]) * 2.55, parseFloat(result[2]) * 2.55, parseFloat(result[3]) * 2.55];

        // Look for #a0b1c2
        if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
            return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)];

        // Look for #fff
        if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
            return [parseInt(result[1] + result[1], 16), parseInt(result[2] + result[2], 16), parseInt(result[3] + result[3], 16)];

        // Otherwise, we're most likely dealing with a named color
        //return colors[jQuery.trim(color).toLowerCase()];
    }

    function getColor(elem, attr) {
        var color;

        do {
            color = jQuery.curCSS(elem, attr);

            // Keep going until we find an element that has color, or we hit the body
            if (color != '' && color != 'transparent' || jQuery.nodeName(elem, "body"))
                break;

            attr = "backgroundColor";
        } while (elem = elem.parentNode);

        return getRGB(color);
    };
})(jQuery);

//hoverintent plugin
(function($) {
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);

$(document).ready(function () {
    var l_intColor = currentcolorcounter("#topmenu #work");
    ColorFade("#topmenu .fade", l_intColor, true, false);
    ColorFade("#topmenu #work", l_intColor, false, true);
    ColorFade("#footer.fade", l_intColor, true, false);
    if ($("#submenu .fade").length > 0) {
        ColorFade("#submenu .fade", l_intColor, true, false);
    }
    Cufon.replace('#menu a', { fontFamily: 'Normal' });
    Cufon.replace('#footercontent .footertitel', { fontFamily: 'Normal' });

    $('#footercontent .contacttitle').click(function () {
        _gaq.push(['_trackEvent', 'Contact', 'Open']);
        $(this).hide(1000);
        $(this).next().show(1000);
    });
});
$(window).load(function () {
    var l_intMaxHeight = 0;
    $("#worklist li").find('a img').each(function () {
        if (l_intMaxHeight < $(this).height()) {
            l_intMaxHeight = $(this).height();
        }
    });
    $("#worklist .worktext").css("top", l_intMaxHeight + 70 + "px");
    $("#onbottom #workline").css("top", l_intMaxHeight + 110 + "px");
});
function randomFromTo(from, to) {
    return Math.floor(Math.random() * (to - from + 1) + from);
}
function currentcolorcounter(p_strSelector) {
    var l_intColor = randomFromTo(0, m_arrColor.length - 1);
    
    if($.cookie('current_color' + p_strSelector)) {
        l_intColor = $.cookie('current_color' + p_strSelector);
    }
    return parseInt(l_intColor);
}

var m_arrColor = ["#f87d12", "#f87d12", "#f05c2e", "#e8416c", "#dd4493", "#bb50bc", "#647bcd", "#3dacc4", "#3dc4b3", "#4bc97d", "#9ac64e", "#f3c034", "#faa62c"]

function ColorFade(p_strSelector, p_intCounter, p_blnBackground, p_blnCufon) {
    var l_strCurrentContent = '';
    if(p_blnCufon) {
        Cufon.replace(p_strSelector, { fontFamily: 'Medium' });
    }
    $(p_strSelector).each(function () {
        if (!p_blnBackground) {
            $(this).css("position", "relative");
            $(this).css("color", m_arrColor[p_intCounter]);
        } else {
            $(this).css("background-color", m_arrColor[p_intCounter]);
        }
        ColorFadeRecursive(p_intCounter, $(this), 10000, p_blnBackground, p_strSelector, p_blnCufon);
    });
}
function ColorFadeRecursive(p_intCounter, p_objThis, p_intSpeed, p_blnBackground, p_strSelector, p_blnCufon) {
    var l_intCounter = p_intCounter;
    var l_objThis = $(p_objThis);
    $.cookie('current_color' + p_strSelector, l_intCounter, { path: '/' });

    if (p_blnBackground) {
        l_objThis.animate({
            backgroundColor: m_arrColor[l_intCounter]
        }, {
            duration: p_intSpeed,
            queue: false,
            complete: function () {
                if (l_intCounter >= m_arrColor.length - 1) {
                    l_intCounter = -1;
                }
                $("#onbottom").css("background-color", m_arrColor[l_intCounter]);
                ColorFadeRecursive(l_intCounter + 1, p_objThis, p_intSpeed, p_blnBackground, p_strSelector, p_blnCufon);
            }
        });
    } else {
        l_objThis.animate({
                color: m_arrColor[l_intCounter]
            },{
                step: function () {
                    Cufon.refresh(p_strSelector);
                },
                duration: p_intSpeed,
                queue: false,
                complete: function () {
                    if (l_intCounter >= m_arrColor.length - 1) {
                        l_intCounter = -1;
                    }
                    ColorFadeRecursive(l_intCounter + 1, p_objThis, p_intSpeed, p_blnBackground, p_strSelector, p_blnCufon);
                }   
        });
    }
}
var m_blnOpen = false;

function openwork() {
    var l_blnCheck = false;
    $('#workfilter input').each(function (i, p_objItem) {
        if ($(this).val() == $.cookie('workfiltercat' + $(this).val())) {
            $(this).attr('checked', 'checked');
            l_blnCheck = true;
        }
    });
    if (l_blnCheck) {
        filterwork(false);
    } else {
        if ($.cookie('workmoveleft')) {
            if ($.cookie('workmoveleft') == 0) {
                $('#workbtnleft').hide();
            }
            $('#worklist').css('left', $.cookie('workmoveleft') + 'px');
        } else {
            $('#workbtnleft').hide();
        }
    }
    var l_objParent = $("#menu .work");
    if (m_blnOpen) {
        _gaq.push(['_trackEvent', 'Work', 'Open']);
        l_objParent.css("background-image", "url('/images/global/WorkBtn.png')");
        l_objParent.children('a').css("color", "#000");
        $("#onbottom").animate({ "top": "-=540px" }, {
            duration: "slow",
            queue: false,
            easing: "swing",
            complete: function () {
                m_blnOpen = false;
            }
        });
    } else {
        _gaq.push(['_trackEvent', 'Work', 'Close']);
        l_objParent.css("background-image", "url('/images/global/WorkBtnOver.png')");
        l_objParent.children('a').css("color", "#fff");
        $("#onbottom").animate({ "top": "+=540px" }, {
            duration: "slow",
            queue: false,
            easing: "swing",
            complete: function () {
                m_blnOpen = true;
            }
        });
    }
    Cufon.refresh('#menu a');
}
function filterwork(p_blnLeft) {
    var l_strCat = '';
    var l_strContains = '';
    $('#workfiltertag input').each(function () {
        l_strCat = $(this).val();
        if ($(this)[0].checked) {
            if (l_strCat != '') {
                l_strContains += ":contains('" + l_strCat + "'),";
                $.cookie('workfiltercat' + l_strCat, l_strCat, { path: '/' });
            }
        } else {
            $.cookie('workfiltercat' + l_strCat, '', { path: '/' });
        }
    });
    _gaq.push(['_trackEvent', 'Work', 'Filter', l_strContains]);
    var l_intCounter = 0;
    $("#worklist").children().each(function () {
        $(this).removeClass("listleft");
        if (l_intCounter % 4 == 0 || l_intCounter == 0) {
            $(this).addClass("listleft");
        }
        if ($(this).children(".tags" + l_strContains).length > 0) {
            $(this).show();
            l_intCounter++;
        } else {
            $(this).hide();
        }
    });
    if (l_intCounter < 5) {
        $('#workbtnright').hide();
    } else {
        $('#workbtnright').show(500);
    }
    if (p_blnLeft == null) {
        $("#worklist").animate({ "left": "0px" }, "fast", function () {
            $('#workbtnleft').hide();
        });
    }
    if ($("#worklist").css('left') == '0px') {
        $('#workbtnleft').hide();
    }
}
var l_blnMoving = false;

function workmoveleft() {
    if (!l_blnMoving) {
        _gaq.push(['_trackEvent', 'Work', 'Move', 'Left']);
        $('#workbtnright').show(500);
        l_blnMoving = true;
        var l_objMover = $("#worklist");
        var l_intMoverChildren = l_objMover.find('img:visible').size();
        if (l_intMoverChildren > 4) {
            if (l_objMover.css('width') == "auto") {
                l_objMover.css('width', (l_intMoverChildren * 170) + 'px');
            }
            if (l_objMover.position().left < 0) {
                l_objMover.animate({ "left": "+=925px" }, {
                    duration: "slow",
                    queue: false,
                    easing: "swing",
                    complete: function () {
                        l_blnMoving = false;
                        var l_intLeft = l_objMover.position().left;
                        $.cookie('workmoveleft', l_intLeft, { path: '/' });
                        if (l_intLeft == 0) {
                            $('#workbtnleft').hide(500);
                        }
                    }
                });
            } else {
               l_blnMoving = false;
               
            }
        } else {
            l_blnMoving = false;
        }
    }
}
function workmoveright() {
    if (!l_blnMoving) {
        _gaq.push(['_trackEvent', 'Work', 'Move', 'Right']);
        $('#workbtnleft').show(500);
        l_blnMoving = true;
        var l_objMover = $("#worklist");
        var l_intMoverChildren = l_objMover.find('img:visible').size();
        if (l_intMoverChildren > 4) {
            if (l_objMover.css('width') == "auto" || l_objMover.css('width') == "948px") {
                l_objMover.css('width', ((l_intMoverChildren / 4) * 948) + 'px');
            }
            if (-l_objMover.position().left + 1 < (l_intMoverChildren * 170)) {
                l_objMover.animate({ "left": "-=925px" }, {
                    duration: "slow",
                    queue: false,
                    easing: "swing",
                    complete: function () {
                        l_blnMoving = false;
                        var l_intLeft = l_objMover.position().left;
                        $.cookie('workmoveleft', l_intLeft, { path: '/' });
                        if ((l_intLeft - 925) == -(Math.ceil(l_intMoverChildren / 4) * 925)) {
                            $('#workbtnright').hide(500);
                        }
                    }
                });
            } else {
                l_blnMoving = false;
            }
        } else {
            l_blnMoving = false;
        }
    }
}

function w3slive(p_objThis, p_intItemCount) {
    if(p_objThis.position().top <= -p_objThis.height()) {
        p_objThis.css('top', '24px');   
    } 
    p_objThis.delay(5000).animate({ "top": "-=24px" }, "slow", function () {
        w3slive(p_objThis);
    });
}
