/***********************************************
* CMotion Image Gallery- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for source code
* This copyright notice must stay intact for legal use
* Modified for autowidth and optional starting positions in
* http://www.dynamicdrive.com/forums/showthread.php?t=11839 by jschuer1 8/5/06
***********************************************/

 //1) Set width of the "neutral" area in the center of the gallery.
var restarea = 6;
 //2) Set top scroll speed in pixels. Script auto creates a range from 0 to top speed.
var maxspeed = 3;
 //3) Set to maximum width for gallery - must be less than the actual length of the image train.
var maxwidth = 1000;
 //4) Set to 1 for left start, 0 for right, 2 for center.
var startpos = 0;
 //5) Set message to show at end of gallery. Enter "" to disable message.
var endofgallerymsg = '<span style="font-size: 11px;">End of Gallery</span>';
//
var show_mode                   = 'after';
var global_current_image_number = 0;
var global_first_run = 0;

function enlargeimage(path, optWidth, optHeight){ //function to enlarge image. Change as desired.
    var actualWidth=typeof optWidth!="undefined" ? optWidth : "600px" //set 600px to default width
    var actualHeight=typeof optHeight!="undefined" ? optHeight : "500px" //set 500px to  default height
    var winattributes="width="+actualWidth+",height="+actualHeight+",resizable=yes"
    window.open(path,"", winattributes)
}

////NO NEED TO EDIT BELOW THIS LINE////////////

var iedom = document.all||document.getElementById, scrollspeed=0, movestate='', actualwidth='', cross_scroll, ns_scroll, statusdiv, loadedyes=0, lefttime, righttime;

function ietruebody(){

    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function creatediv(){
    
    statusdiv = document.createElement("div");
    statusdiv.setAttribute("id","statusdiv");
    document.body.appendChild(statusdiv);
    statusdiv = document.getElementById("statusdiv");
    statusdiv.innerHTML = endofgallerymsg;
}

function positiondiv(){
    
    var crossmain = document.getElementById('motioncontainer');
    var mainobjoffset=getposOffset(crossmain, "left"),
    
    menuheight=parseInt(crossmain.offsetHeight),
    mainobjoffsetH=getposOffset(crossmain, "top");
    statusdiv.style.left=mainobjoffset+(menuwidth/2)-(statusdiv.offsetWidth/2)+"px";
    statusdiv.style.top=menuheight+mainobjoffsetH+"px";
}

function showhidediv(what){

    if (endofgallerymsg!="") {
        positiondiv();
        statusdiv.style.visibility=what;
    }
}

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft: what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}


function moveleft(){
if (loadedyes){
movestate="left";
if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-actualwidth)){
cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px";
showhidediv("hidden");
}
else
showhidediv("visible");
}
lefttime=setTimeout("moveleft()",10);
}

function moveright(){
if (loadedyes){
movestate="right";
if (iedom&&parseInt(cross_scroll.style.left)<0){
cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px";
showhidediv("hidden");
}
else
showhidediv("visible");
}
righttime=setTimeout("moveright()",10);
}

function motionengine(e){
    var crossmain = document.getElementById('motioncontainer');
var mainobjoffset=getposOffset(crossmain, "left"),
dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft,
dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop,
curposy=window.event? event.clientX : e.clientX? e.clientX: "";
curposy-=mainobjoffset-dsocx;
var leftbound=(menuwidth-restarea)/2;
var rightbound=(menuwidth+restarea)/2;
if (curposy>rightbound){
scrollspeed=(curposy-rightbound)/((menuwidth-restarea)/2) * maxspeed;
clearTimeout(righttime);
if (movestate!="left") moveleft();
}
else if (curposy<leftbound){
scrollspeed=(leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed;
clearTimeout(lefttime);
if (movestate!="right") moveright();
}
else
scrollspeed=0;
}

function contains_ns6(a, b) {
if (b!==null)
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function stopmotion(e){

    var crossmain = document.getElementById('motioncontainer');
    if (!window.opera||(window.opera&&e.relatedTarget!==null)) {
        if ((window.event&&!crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))){
            clearTimeout(lefttime);
            clearTimeout(righttime);
            movestate="";
        }
    }
}


fillup = function (id_motiongallery) {
	
	if (global_first_run == 0) {
    	getHtmlForScrollingImagesByMode('after'); 
		global_first_run = 1;
	}
    //if (iedom) {
        var crossmain = document.getElementById('motioncontainer');

        //if (typeof crossmain.style.maxWidth !== 'undefined') {
            crossmain.style.maxWidth = maxwidth + 'px';
            menuwidth                = crossmain.offsetWidth;
            cross_scroll             = document.getElementById(id_motiongallery);
            actualwidth              = document.getElementById('trueContainer_' + show_mode).offsetWidth; //mojet ostavit toka truecontainer???
            
            if (startpos) {
                cross_scroll.style.left = (menuwidth - actualwidth) / startpos + 'px';
            }
            
            crossmain.onmousemove = function(e) {
                motionengine(e);
            }

            crossmain.onmouseout = function(e) {
                stopmotion(e);
                showhidediv("hidden");
            }
        //}
        loadedyes = 1;
        if (endofgallerymsg != "") {
            creatediv();
            positiondiv();
        }
        onresize(id_motiongallery);
}


onresize = function(id_motiongallery) {

    var motioncontainer = document.getElementById('motioncontainer');
    
    if (typeof motioncontainer !== 'undefined') {
        motioncontainer.style.width = "0";
        motioncontainer.style.width = "";
        motioncontainer.style.width = Math.min(motioncontainer.offsetWidth, maxwidth) + 'px';
    }

    menuwidth = motioncontainer.offsetWidth;
}



showPic = function (element, number_from_array) {
    
    global_current_image_number = number_from_array
    var url_to_large_pic        = element.href;
    
    document.getElementById('image_placeholder').src = url_to_large_pic;
    
    // if "before" exists - show label
    if (show_mode == 'after') {
        if (a_large_images_before[number_from_array] != '') {
            document.getElementById('b_a_img').style.display = 'block';
            document.getElementById('link_to_before_after_mode').onclick = function() { largePicOnClick(); return false; };        
        }
        else {
            document.getElementById('b_a_img').style.display = 'none';
            document.getElementById('link_to_before_after_mode').onclick = function() { return false; };
        }
    }
  
    return false;
}


largePicOnClick = function () {
    
    //document.getElementById('motiongallery_' + show_mode).style.display = 'none';
    
    if (show_mode == 'after') {
        show_mode = 'before';
        document.getElementById('b_a_img').style.display = 'none';
        document.getElementById('a_b_img').style.display = 'block';
    }
    else {
        show_mode = 'after';
        document.getElementById('b_a_img').style.display = 'block';
        document.getElementById('a_b_img').style.display = 'none';
    }
    
    //document.getElementById('motiongallery_' + show_mode).style.display = 'block';
    
       
    //fillup('motiongallery_' + show_mode);
    
    document.getElementById('image_placeholder').src = getFullImageNameWithoutModeAndExtension() + '_' + show_mode + '.jpg';
}


getHtmlForScrollingImagesByMode = function (mode) {
    
    var html      = '';
    var container = document.getElementById('trueContainer_' + mode);

    if (container != null) {
        if (mode == 'after') {
            var a_thumb_images = a_thumb_images_after;
            var a_large_images = a_large_images_after;
        }
        else {
            var a_thumb_images = a_thumb_images_before;
            var a_large_images = a_large_images_before;
        }
      
        if (a_thumb_images.length == a_large_images.length) {
            for (i = 0; i < a_thumb_images.length; i++) {
                if (a_thumb_images[i] != '' && a_large_images[i] != '') {
                    html += '<a id="' + getSmallImageLinkIdByNumber(i, mode) + '" onclick="return showPic(this, ' + i + ')" href="' + a_large_images[i] + '"><img src="' + a_thumb_images[i] + '" /></a>';
                }
            }
        }
        else {
            alert('error 002');
        }
    }
    else {
        alert('error 003');
    }

    container.innerHTML = html;
}


getSmallImageLinkIdByNumber = function (number, mode) {
    
    return 'small_image_link_' + number + '_' + mode;
}


getFullImageNameWithoutModeAndExtension = function () {
    
    var full_image_name_without_mode_and_extension = '';
    
    var element         = document.getElementById('image_placeholder');
    var full_image_name = element.src;
    if (element != null && full_image_name != null) {
        var a_parts_of_image_name = full_image_name.split('_');
    
        if (a_parts_of_image_name.length >= 2) {
            for (i = 0; i < (a_parts_of_image_name.length - 1); i++) {
                if (full_image_name_without_mode_and_extension == '') {
                    full_image_name_without_mode_and_extension += a_parts_of_image_name[i];
                }
                else {
                    full_image_name_without_mode_and_extension += '_' + a_parts_of_image_name[i];
                }
            }
        } 
        
    }
    else {
        alert('error 001');
    }
    
    return full_image_name_without_mode_and_extension;
}


// input: "image_1_before.gif"
// out: "image_1"
getImageNameWithoutExtensionByFullName = function (full_image_name) {
    
    var image_name = '';
    
    var a_parts_of_image_name = full_image_name.split('/');
    
    if (a_parts_of_image_name.length >= 1) {
        a_parts_of_image_name = a_parts_of_image_name[(a_parts_of_image_name.length - 1)].split('_');
    
        if (a_parts_of_image_name.length >= 2) {
            for (i = 0; i < (a_parts_of_image_name.length - 1); i++) {
                if (image_name == '') {
                    image_name += a_parts_of_image_name[i];
                }
                else {
                    image_name += '-' + a_parts_of_image_name[i];
                }
            }
        }
    }
    
    return image_name;
}


setPreviousLargeImage = function() {
    
    var show_mode ='after';
    
    if (global_current_image_number != 0) {
        global_current_image_number = global_current_image_number - 1;
    }
    else {
        global_current_image_number = a_large_images_after.length - 1;
    }
    
    var element = document.getElementById(getSmallImageLinkIdByNumber(global_current_image_number, show_mode));
    
    // show before - after label
    document.getElementById('b_a_img').style.display = 'block';
    document.getElementById('a_b_img').style.display = 'none';
    
    showPic(element, global_current_image_number);
    
    // show the scroller
    //document.getElementById('motiongallery_' + 'before').style.display  = 'none';
    document.getElementById('motiongallery_' + show_mode).style.display = 'block';
}


setNextLargeImage = function() {
    
    show_mode ='after';
    
    if (global_current_image_number < (a_large_images_after.length - 1)) {
        global_current_image_number = global_current_image_number + 1;
    }
    else {
        global_current_image_number = 0;
    }
    
    var element = document.getElementById(getSmallImageLinkIdByNumber(global_current_image_number, show_mode));
    
    // show before - after label
    document.getElementById('b_a_img').style.display = 'block';
    document.getElementById('a_b_img').style.display = 'none';
    
    showPic(element, global_current_image_number);
    
    // show the scroller
    //document.getElementById('motiongallery_' + 'before').style.display  = 'none';
    document.getElementById('motiongallery_' + show_mode).style.display = 'block';
}
