// JavaScript Document

var is_ie = navigator.userAgent.toLowerCase().indexOf("msie") > -1;

if (is_ie) {
	window.attachEvent("onload", article_onLoad);
} else {
	window.addEventListener("load", article_onLoad, false);
}

function article_onLoad(ev) {
	if (!document.getElementsByTagName) return;
    
    var a = document.getElementsByTagName("a");
	for (var i=0; i<a.length; i++) {
		if (a[i].className == "ltv_embed") {
			a[i].onclick = function(){
                window.open(this.href, '_blank', "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=799, height=670");
                return false;
            };
        }
	}
}

function setMainImage(id) {
    imCount = 3;
    if (id+1>3) {return true;}
    
    if (document.getElementById('main_image_href') && typeof(document.getElementById('main_image_href').href)!='undefined') {
      document.getElementById('main_image_href').href = main_image_href_sample + (id+1) + "/#image";
    }
    
    var loading = document.getElementById('image_loading');
    if (loading) {
        loading.style.display = 'block';
        if (mainImagesList[id][2]) loading.style.height = mainImagesList[id][2] + 'px';
    }
    
    if (mainImagesList[id][0]) {
        var img = document.getElementById('a_m_img');
        if (img) {
            img.src = mainImagesList[id][0];
            if (mainImagesList[id][4] && mainImagesList[id][4] == 'big')
                if (mainImagesList[id][3]) img.style.marginLeft = Math.round((700 - mainImagesList[id][3]) / 2) + 'px';
        }
    
        var img_prev = document.getElementById('a_img_link_prev');
        if (img_prev && id > 0) {
            if (mainImagesList[id][2]) img_prev.style.height = mainImagesList[id][2] + 'px';
            
            img_prev.onclick = function() { setMainImage(id - 1); return false; };
            img_prev.style.display = 'block';
        } else if (img_prev) {
            img_prev.onclick = null;
            img_prev.style.display = 'none';
        }
    
        var img_next = document.getElementById('a_img_link_next');
        if (img_next) {
            if (mainImagesList[id][2]) img_next.style.height = mainImagesList[id][2] + 'px';
        
            if (mainImagesList[id][1]) {
                img_next.href = mainImagesList[id][1];
                img_next.onclick = null;
                img_next.style.display = 'block';
            } else if (id < (mainImagesList.length - 1) && mainImagesList[(id + 1)][0]) {
                img_next.href = '';
                img_next.onclick = function() { setMainImage(id + 1); return false; };
                img_next.style.display = 'block';
            } else {
                img_next.style.display = 'none';
            }
        }
    }
    
    return false;
}

// GALLERY SCROOLL FUNCTIONS 

var galleryObjects = new Array();

function createGallery(num, object, width, numPages, curPage, locked){
    var tmp = new Array();
    tmp[0] = object;
    tmp[1] = width;
    tmp[2] = numPages;
    tmp[3] = curPage;
    tmp[4] = locked;
    galleryObjects[num] = tmp;
}

function galleryBlur(lnk) {
    try {
        lnk.blur();
    } catch(e) {};
}

function galleryNext(lnk, num) {
    galleryBlur(lnk);
    if ((galleryObjects[num][3] < galleryObjects[num][2]) && !galleryObjects[num][4]) {
        gallerySlideLeft(num);
        if (galleryObjects[num][3] == galleryObjects[num][2] && /#$/.test(lnk.href) == true) {
            document.getElementById('img_' + galleryObjects[num][0] + '_next').src = '/images/v5/' + galleryObjects[num][0] + '_rna.gif';
            document.getElementById('gal_link_next').style.cursor = 'default';
        }
    }
    else if (galleryObjects[num][3] == galleryObjects[num][2] && /#$/.test(lnk.href) == false)
        document.location = lnk.href;
    
    if (galleryObjects[num][3] > 1) {
        document.getElementById('img_' + galleryObjects[num][0] + '_prev').src = '/images/v5/' + galleryObjects[num][0] + '_la.gif';
        document.getElementById('gal_link_prev').style.cursor = 'pointer';
    }
        
    return false;
}

function galleryPrev(lnk, num) {
    galleryBlur(lnk);
    if (galleryObjects[num][3] > 1 && !galleryObjects[num][4]) {
        gallerySlideRight(num);
        if (galleryObjects[num][3] == 1 && /#$/.test(lnk.href) == true) {
            document.getElementById('img_' + galleryObjects[num][0] + '_prev').src = '/images/v5/' + galleryObjects[num][0] + '_lna.gif';
            document.getElementById('gal_link_prev').style.cursor = 'default';
        }
    }
    else if (galleryObjects[num][3] == 1 && /#$/.test(lnk.href) == false) 
        document.location = lnk.href;
    
    if (galleryObjects[num][3] < galleryObjects[num][2]) { 
        document.getElementById('img_' + galleryObjects[num][0] + '_next').src = '/images/v5/' + galleryObjects[num][0] + '_ra.gif';
        document.getElementById('gal_link_next').style.cursor = 'pointer';
    }
        
    return false;
}

function galleryLock(dur, num) {
    var galleryLockDur = dur * 100;
    galleryObjects[num][4] = true;
    setTimeout('disableGallery(' + num + ')', galleryLockDur);
}

function disableGallery(num){
    galleryObjects[num][4] = false
}

function gallerySlideLeft(num) {
    galleryLock(3, num);
    for (i=1; i<=galleryObjects[num][2]; i++) {
        new Effect.MoveBy(galleryObjects[num][0] + i, 0, (-1 * parseInt(galleryObjects[num][1])), {duration: 0.1});
    }
    galleryObjects[num][3]++;
}

function gallerySlideRight(num) {
    galleryLock(3, num);
    for (i=1; i<=galleryObjects[num][2]; i++) {
        new Effect.MoveBy(galleryObjects[num][0] + i, 0, parseInt(galleryObjects[num][1]), {duration: 0.1});
    }
    galleryObjects[num][3]--;    
}

var selectVisible = 0;

function selectGalleryPage(obj) {
    selectVisible = Math.abs(selectVisible - 1);
    if (selectVisible)
        document.getElementById(obj).style.display = 'block';
    else
        document.getElementById(obj).style.display = 'none';
    return false;
}
