/*
 * Yegor Shershnev, 2010, GPL
 * $Id: interfaces.py 54359 2010-10-10 16:41:13Z cray $
 *
 * JS-script to treatment simple baseskin based image gallery with index.
 *
 * Url are to rewrite with  /++skinon++jsgallery to use.
 *
 */


var gallery_v1 = "div#gallery a div#view1",
    gallery_v2 = "div#gallery a div#view2",
    gallery_current = null,
    gallery_flag = false,
    gallery_imageHeight=300,
    gallery_imageWidth=400,
    gallery_mousePosition = new Object(),
    gallery_mousePositionFlag = false,
    gallery_prev_end = null,
    gallery_next_end = null;
    

function getOffsetSum(elem) {
    var top=0, left=0;

    while(elem) {
        top = top + parseInt(elem.offsetTop);
        left = left + parseInt(elem.offsetLeft);
        elem = elem.offsetParent;
    }
    
    return {top: top, left: left};
}

gallery_prev_click = function () {
    var links = $("div.contenticons table tr td a");
    var length = links.length;
    var index = links.index(gallery_current);

    if (index < 0)  {
	index = length - 1;
    } else if (index == 0) {
        if (gallery_prev_end && gallery_current) {
	   return gallery_prev_end();
        }
        index = length - 1;
    } else {
        index -= 1;
    }

    $(links[index]).click();
    return false;
}

gallery_next_click = function () {
    var links = $("div.contenticons table tr td a");
    var length = links.length;
    var index = links.index(gallery_current);

    if (index >= length) {
	index = 0;
    } else if (index == length-1) {
        if (gallery_next_end) {
	   return gallery_next_end();
        }
        index = 0;
    } else {
        index += 1;
    }

    $(links[index]).click();
    return false;
}

function gallery_click(event) {

    gallery_current = this;

    try {
      var node = $("div.article div a:contains('Full view')").get(0).parentNode.parentNode;
      $(node).animate({"height":"0px"});
 
    } catch(err) {
      /* alert(err); */ 
    }

    // $("div#msg_loading").css("display","none");


    if(gallery_flag) {
        return false;
    }
    
    gallery_flag = true;
   
    $("div#msg_loading").css({"display":"block"});

    if (!$("div#gallery").size()) {
        $("div.contenticons").before('<div id="gallery" style="overflow:hidden; margin-bottom: 0px; margin-left: 15%; height: 0px; text-align: center; width: 68%; float: left; position: relative;"><div id="msg_loading" style="display: none; position: absolute; z-index: 70; width: 100%; height: 50px; text-align: center; top: ' + (gallery_imageHeight / 2 - 25) + 'px; left: 0px;"><img src="/@@/loading.gif" alt="Loading..." /></div><a><div id="view1" style="position: relative; overflow:hidden; text-align: center; width: ' + gallery_imageWidth +'px; height: ' + gallery_imageHeight +'px; background-color: white;"><img align="center" style=""/></div> <div id="view2" style="position: relative; text-align:center; overflow:hidden; margin-top: -' + gallery_imageHeight + 'px; width: ' + gallery_imageWidth + 'px; height: ' + gallery_imageHeight + 'px; background-color: white;"><img style=""/></div> </a></div>');
        gallery_loadEvent_init();
        $("div#gallery").animate({"height":gallery_imageHeight},1000,function () {
             $("div#gallery").before('<a id="gallery_prev" href="#" style="width: 15%; float: left; margin-top: ' + (gallery_imageHeight / 2 - 70) + 'px; text-align: left;"><img src="/@@/gallery_prev.png" alt="Previous" /></a>');
             if ($("a#gallery_prev").css('display')!='none') {
                 $("div#gallery").css("margin-left","0px");
             }

             $("div#gallery").after('<a id="gallery_next" href="#" style="width: 15%; float: left; margin-top: ' + (gallery_imageHeight / 2 - 70) + 'px; text-align: right;"><img src="/@@/gallery_next.png" alt="Next" /></a>');
             $("a#gallery_prev").click(gallery_prev_click);
             $("a#gallery_next").click(gallery_next_click);
	});
    } 

    var src = $(this).attr("href") + "/%40%40smartimagecontainer/gallery/get";
    $("div#gallery a").attr("href", $(this).attr("href") + "/@@view");

    if (src == $(gallery_v2 + " img").attr("src")) {
        $("div#msg_loading").css({"display":"none"});
        gallery_flag = false;
    } else if (src == $(gallery_v1 + " img").attr("src")) {
        $(gallery_v1 + " img").load(); 
    } else  {
        $(gallery_v1 + " img").attr("src", src); 
    }
    
    return false;
}

function slide(label1,label2) {
    $("div#msg_loading").css({"display":"none"});
    var ml = (($("div#gallery").width() - gallery_imageWidth) / 2).toString() + "px";
    $(label1).css({"z-index": "10", "width": gallery_imageWidth + "px", "height": gallery_imageHeight + "px", "margin-left":ml});
    $(label2).css({"z-index": "20", "width": gallery_imageWidth + "px", "height": gallery_imageHeight + "px", "margin-left":ml});
    $(label2).animate({width: "0px"}, function(){gallery_flag = false;});
    gallery_v1 = label2;
    gallery_v2 = label1;
}


function gallery_activate() {
    $("div.contenticons table tr td a:first").click();
}

gallery_imageSize_init = function(width,height) {
    gallery_imageHeight = height;
    gallery_imageWidth = width;
}

function gallery_loadEvent_init () {
    $("div#gallery a div#view1 img, div#gallery a div#view1 img").unbind();

    $("div#gallery a div#view1 img").load(
        function(){
	    slide("div#gallery a div#view1","div#gallery a div#view2");
        }
    );
    
    $("div#gallery a div#view2 img").load(
        function(){
	    slide("div#gallery a div#view2","div#gallery a div#view1");
        }
    );
}

gallery_init = function (prev_end,next_end){
    galery_v1 = "div#gallery a div#view1";
    galery_v2 = "div#gallery a div#view2";
    galery_flag = false;
    gallery_current = null;
    gallery_prev_end = prev_end;
    gallery_next_end = next_end;

    gallery_loadEvent_init ();

    $("div.contenticons table tr td a").unbind();
    $("div.contenticons table tr td a").click(gallery_click);
    $("div.contenticons").mouseout(
        function(){
            gallery_mousePosition.x = 0;
            gallery_mousePosition.y = 0;
        }
    );
    $("div.contenticons").mousemove(
         function(event) {
            if (!gallery_mousePositionFlag) {
                gallery_mousePositionFlag = true;
                if (gallery_mousePosition.x && gallery_mousePosition.y) {
                    var scalex, scaley,
                        cw, tw, scrollbarwidth;
                    
//                    scaleY = $("div.contenticons table").width() /  $("div.contenticons").height();
//                    scaleX = $("div.contenticons table").width() / $("div.contenticons").width();

                    cw = $("div.contenticons").width();
                    tw = $("div.contenticons table").width();
                    
                    $("div.contenticons").get()[0].scrollLeft = gallery_mousePosition.x / cw * (tw-cw) ;
                
                    gallery_mousePosition.x = 0;
                    gallery_mousePosition.y = 0;
                } else {
                    gallery_mousePosition.x = event.pageX - getOffsetSum($("div.contenticons").get()[0]).left;
                    gallery_mousePosition.y = event.pageY - getOffsetSum($("div.contenticons").get()[0]).top;
                }
                setTimeout(
                    function(){
                        gallery_mousePositionFlag = false;
                    },
                    5
                );
            }
        }    
    );
};

$(document).ready(
    gallery_init
);

