﻿///<reference path="./jquery/jquery-1.3.2-vsdoc2.js"/>

function namespace() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=window;
        for (j=0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};

(function () {
    
    namespace("IC.Menu");
    
    IC.Menu = function () 
    {
        var _menuMouseOver = function (e)
        {
            var id = this.id;
            if($('#menu').attr('class'))
            {
                var arr = $('#menu').attr('class').split(' ');
                $('#menu').removeClass(arr);
            }
            $('#menu').addClass("sub_" + id);
        };
        
        return {
            init : function ()
            {
                $("#menu_top a").bind("mouseover", _menuMouseOver);
            }
        };
    }();
    
    $(document).ready(IC.Menu.init);
    
})();

(function () {
    
    namespace("IC.Portfolio");
    
    IC.Portfolio = function () 
    {
        var _portfolioItemClose = function (e)
        {
            $(".porfolioItem_thumbContainer").scrollLeft(0);
            $(".mask_item").css("display","none");
            $("#mask").css("display","none");
            $(".porfolioItem_thumbContainer li").unbind();
            $(".porfolioItem_thumbContainer").unbind();
        };
        
        var _porfolioItemTableRowBuild = function (obj, index) {
            var content = "<tr>";
            for (var i = 0; i < 3; i++) {
                if(obj.images[index + i]) {
                    var img = obj.images[index + i];
                    content += '<td><img src="' + img.img + '" /><label>' + img.desc + '</label></td>';
                }
            }
            content += "</tr>";
            return content;
        };
        
        var _portfolioItemTableBuild = function (obj) {
            var content = '<tr><td colspan="3">' + obj.Desc + '</td></tr>';
            for (var i = 0; i < obj.images.length; i += 3)
            {
                content += _porfolioItemTableRowBuild(obj, i);
            }
            return content;
        };
        
        var _portfolioItemListItemBuild = function (obj) {
            var ulList = $('.porfolioItem_thumbContainer ul');
            ulList.empty();
            for (var i = 0; i < obj.images.length; i++)
            {
                ulList.append('<li><img src="' + obj.images[i].img + '"/></li>');
            }
        };
        
        var _portfolioItemOpen = function (e) {
            var target = e.currentTarget;
            if (!target) {
                target = $(e.target).parent("li").get(0);
            }
            if (target)
            {
                var targetId = target.id;
                targetId = targetId.substring(3);
                var obj = IC.Portfolio.Gallery[targetId];
                if(obj)
                {
                    _portfolioItemListItemBuild(obj);
                    $('.portfolioItem_image img').attr('src',obj.images[0].img);
                    $('.portfolioItem_image p').empty().append(obj.Desc);
                    $(".mask_item").css("display","none");
                    $("#mask").css("display","block");
                    $("#portfolioItem_prototype").css("display","block");
                    var lastPos = $('.porfolioItem_thumbContainer li:last').position().left + 130;
                    //console.log("LastPos:" + lastPos);
                    $(".porfolioItem_thumbContainer li").click(
                        function () {
                            $('.portfolioItem_image img').attr('src',$(this).find("img").attr('src'));
                        }
                     )
                     $(".porfolioItem_thumbContainer").mousemove(
                        function (e) {
                            var mouseX = e.pageX;
                            var tcX = $(this).offset().left;
                            var offset = (mouseX - tcX)/620;
                            var realLeft = (lastPos -620) * offset;
                            //console.debug(mouseX + "," + tcX + "," + offset + "," + lastPos + "," + realLeft);
                            $(".porfolioItem_thumbContainer").scrollLeft((realLeft > 0 ? realLeft : 0));
                        }
                     );
                 }
             }
        };
        
        var _portfolioItemGenerate = function (obj, index) {
            $('.portfolio').append('<li id="PI_' + index + '">' +
                    '<img src="' + obj.LinkImg + '" /><label>' + obj.Label + '</label></li>');
        };
        
        return {
            init : function ()
            {
                if (IC.Portfolio.Gallery) {
                    var gal = IC.Portfolio.Gallery;
                    for (var i = 0; i < gal.length; i++)
                    {
                        _portfolioItemGenerate(gal[i], i);
                    }
                }
                $(".btn_close").each(function(i) {
                  $(this).bind('click', _portfolioItemClose);
                });
                $(".portfolio li").each(function(i) {
                  $(this).bind('click', _portfolioItemOpen);
                });

                
            }
        };
    }();
    
    $(document).ready(IC.Portfolio.init);

})();

(function() {

    namespace("IC.Homepage");

    IC.Homepage = function() {
        var _portfolioItemClose = function(e) {
            $(".mask_item").css("display", "none");
            $("#mask").css("display", "none");
        };
        
        var _animateIn = function (el) {
            $(el).children(".home_services_logo").stop().animate({height : "125px" }, 200);
            if (jQuery.browser.msie) {
                $(el).children(".home_services_head_learn").hide();
                $(el).children(".home_services_head_content").show(200);
            } else {
                $(el).children(".home_services_head_learn").stop().animate({opacity : 0},100);
                $(el).children(".home_services_head_content").stop().animate({opacity : 1},250);
            }
        };
        
        var _animateOut = function (el) {
            $(el).children(".home_services_logo").stop().animate({height : "85px" }, 200);
            if (jQuery.browser.msie) {
                $(el).children(".home_services_head_content").hide();
                $(el).children(".home_services_head_learn").show(200);
            } else {
                $(el).children(".home_services_head_content").stop().animate({opacity : 0},100);
                $(el).children(".home_services_head_learn").stop().animate({opacity : 1},250);
            }
        };

        return {
            init: function() {
                if ($("#home_services").length > 0) {
                    $("#home_services_IMS").hover(
                    function() {
                        $(this).stop().css('z-index', 10).animate({
                        width: "350px",
                        height: "240px",
                        top: "-100px"
                        }, 200);
                        _animateIn(this);
                    },
                    function() {
                        $(this).stop().css('z-index', 0).animate({
                        width: "254px",
                        height: "140px",
                        top: "0px"
                        }, 200);
                        _animateOut(this);
                    });

                    $("#home_services_EandT").hover(
                    function() {
                        $(this).stop().css('z-index', 10).animate({
                            width: "350px",
                            height: "240px",
                            top: "-100px",
                            left: "200px"
                        }, 200);
                        _animateIn(this);
                    },
                    function() {
                        $(this).stop().css('z-index', 0).animate({
                            width: "254px",
                            height: "140px",
                            top: "0px",
                            left: "257px"
                        }, 200);
                        _animateOut(this);
                    });

                    $("#home_services_PM").hover(
                    function() {
                        $(this).stop().css('z-index', 10).animate({
                            width: "350px",
                            height: "240px",
                            top: "-100px"
                        }, 200);
                        _animateIn(this);
                    },
                    function() {
                        $(this).css('z-index', 0).stop().animate({
                            width: "254px",
                            height: "140px",
                            top: "0px"
                        }, 200);
                        _animateOut(this);
                    });
                }
            }
        };
    } ();

    $(document).ready(IC.Homepage.init);

})();
