/*!
 * jImageBox the image gallery plugin base in jQuery
 *
 * the default css is support 5 or 8 item images.
 * Copyright (c) 2009 Peter
 * Date: 2009-10-19
 * Revision: 1
 */

function ImageBox(flashId, displayId, size,type) {
    var imgNode;
    var imageFlashId = "#" + flashId;
    var imageDisplayId = "#" + displayId;
    var itemSize = size;  //the default css is support 5 and 8 item images.
    this.init = function() {
        jQuery(imageFlashId).find("img").each(function(i) {
            jQuery(this).click(function() {
                imgNode.removeClass("li_img_selected").addClass("li_img");
                jQuery(this).removeClass("li_img").addClass("li_img_selected");
                imgNode = jQuery(this);

                //-------------------------
                var wgif = _themePath+"/css-img/jImageBox/tran.gif";
                jQuery(imageDisplayId)[0].src = wgif;
                loadImage(jQuery(this).attr("src").replace('_s', type), chUrl, imageDisplayId);
                //-----------------------------------
                return false;
            });
            if (jQuery(this).hasClass("li_img_selected")) imgNode = jQuery(this);
            jQuery(this).hover(function() {
                jQuery(this).removeClass("li_img").addClass("li_img_selected");
            }, function() {
                if (jQuery(this).get(0) != imgNode.get(0)) jQuery(this).removeClass("li_img_selected").addClass("li_img");
            });
        });


        var totalPage = getTotalPage(itemSize);

        var newarr = [0,totalPage,0,itemSize * 54,0,0,("#list_li_ul"),jQuery("#list_text"),0,itemSize - 1,20];
        if (newarr[1] > 0) {
            jQuery("#list_btn_left").click(function() {
                showList(1, newarr);
            });
            jQuery("#list_btn_right").click(function() {
                showList(0, newarr);
            });
        }
        jQuery(newarr[7]).html((parseInt(newarr[2]) + 1) + "/" + (parseInt(newarr[1]) + 1));


    }


    function getTotalPage(pageSize) {
        var totalCount = jQuery("#productFlashBox").find("img").length;
        if (totalCount % pageSize == 0)
            return Math.floor(totalCount / pageSize) - 1;
        else
            return Math.floor(totalCount / pageSize);
    }

    function showList(t, m) {
        if (m[4] == 1) return;
        var x = t == 0 ? -m[3] : m[3];
        if (t == 0 && m[2] == m[1]) {
            rolling(1);
            cycleShow(1);
        } else if (t == 1 && m[2] == 0) {
            rolling(2);
            cycleShow(0);
        } else {
            rolling();
        }
        function rolling(e) {
            m[4] = 1;
            jQuery(m[6]).animate({left:parseInt(jQuery(m[6]).css("left")) + x}, 300, function() {
                pageSet(e);
                m[4] = 0;
            });
        }

        function cycleShow(y) {
            jQuery(m[6]).after(jQuery(m[6]).clone(true).css({left:(y == 1 ? (m[3]) : (-m[3] * (m[1] + 1)))}));
            jQuery(m[6]).next("ul").animate({left:parseInt(jQuery(m[6]).next("ul").css('left')) + x}, 300, function() {
                m[0] = y == 1 ? 0 : -m[3] * m[1];
                jQuery(m[6]).addClass("hide").css({left:m[0]}).next("ul").css({left:m[0]}).end().removeClass("hide").next("ul").remove();
            });
        }

        function pageSet(h) {
            if (h) {
                m[2] = h == 1 ? 0 : m[1];
            } else {
                t == 0 ? m[2]++ : m[2]--;
            }
            jQuery(m[7]).html((parseInt(m[2]) + 1) + "/" + (parseInt(m[1]) + 1));
        }
    }


    function loadImage(url, callback, id) {
        var img = new Image();
        img.src = url;
        if (img.complete) {
            callback.call(img, id);
        } else {
            jQuery(img).bind("load", function() {
                //----2010.0901
                 jQuery(id).attr({width:img.width});
                 jQuery(id).attr({height:img.height});
                //----2010.0901 
                callback.call(img, id);
            });
        }
    }

    function chUrl(id) {
        jQuery(id).attr({src:this.src});
    }


}


ImageBox.loadImage= function(url,id) {
        var img = new Image();
        img.src = url;
        if (img.complete) {
            jQuery(id).attr({src:img.src});
        } else {
            jQuery(img).bind("load", function() {
                //----2010.0901
                 jQuery(id).attr({width:img.width});
                 jQuery(id).attr({height:img.height});
                //----2010.0901
                jQuery(id).attr({src:img.src});
            });
        }
    } ;






