var imgs = typeof(imgs) != 'undefined' ? imgs : '';
var downloads = function() {
    var links = document.getElementsByTagName('a');
    var llen = links.length;
    if (llen > 0) {
        for (var i = 0; i < llen; i++) {
            var lnk = links[i];
            if (lnk.getAttribute('href').match(/(pdf|wmv)/)) {
                lnk.setAttribute('target', '_blank');
            };
        };
    };
};
function slide() {
    if (imgs.length <= 1 || !document.getElementById || !document.getElementsByTagName || !document.getElementById('gallery')) return;
    
    var gallery = document.getElementById('gallery');
    var slide = document.getElementById('slide');
    var current_img = document.getElementById('gallery').getElementsByTagName('img')[0];
    
    for (i = imgs.length - 1; i > -1; i--) {
        var temp = document.createElement('img');
        var p = imgs[i]; 
        temp.setAttribute('src','/images/' + p);
        temp.setAttribute('alt','[slika ' + p + ']');
        temp.className = temp.getAttribute('src') == current_img.getAttribute('src') ? 'showcase active' : 'showcase';
        if (temp.getAttribute('src') == current_img.getAttribute('src')) {
            var sel_img = i;
            var pre_sel_img = sel_img;
        };
        gallery.insertBefore(temp,slide.nextSibling);
    };
    
    current_img.parentNode.removeChild(current_img);
    
    imgs_tmp = document.getElementById('gallery').getElementsByTagName('img');
    imgs = new Array();
    for (i = 0; i < imgs_tmp.length; i++) {
        if (imgs_tmp[i].className.indexOf('showcase') != -1) {
            imgs.push(imgs_tmp[i]);
        }
    }
    
    var slide_prev = document.getElementById('slide_prev');
    var slide_next = document.getElementById('slide_next');
    var thumbs = document.getElementById('gallery_nav').getElementsByTagName('a');
    
    slide_prev.onclick = function() {
        pre_sel_img = sel_img;
        sel_img = sel_img > 0 ? sel_img - 1 : imgs.length - 1;
        switch_class();
        return false;
    };
    slide_next.onclick = function() {
        pre_sel_img = sel_img;
        sel_img = sel_img < imgs.length - 1 ? sel_img + 1 : 0;
        switch_class();
        return false;
    };
    
    var thumbs = document.getElementById('gallery_nav').getElementsByTagName('a');
    
    for (i = 0; i < thumbs.length; i++) {
        thumbs[i].onclick = function() {
            pre_sel_img = sel_img;
            sel_img = parseInt(this.getElementsByTagName('img')[0].getAttribute('alt') - 1);
            switch_class();
            return false;
        };
    };
    
    function switch_class() {
        imgs[pre_sel_img].className = imgs[pre_sel_img].className.replace(RegExp(' active', 'i'), '');
        thumbs[pre_sel_img].parentNode.className = '';
        imgs[sel_img].className += ' active';
        thumbs[sel_img].parentNode.className = 'active';
    }
    

};


var ifl = function(eI,oI,m,w,h,scale,salign,menu) {
    var prnt = document.getElementById(eI);
    if (!prnt) return;
    var output = '<object id="' + oI + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + w + '" height="' + h + '">\n'
        + '<param name="wmode" value="transparent" />\n'
        + '<param name="allowScriptAccess" value="sameDomain" />\n'
        + '<param name="movie" value="' + m + '" />\n'
        + '<param name="quality" value="high" />\n'
        + (menu == 'false' ? '<param name="menu" value="false" />\n' : '')
        + '<param name="Loop" value="-1" />\n'
        + '<param name="Src" value="' + m + '" />\n'               
        + '<param name="FlashVars" value="" />\n'
        + '<param name="Play" value="0" />\n'
        + '<param name="Loop" value="-1" />\n'
        + '<param name="SAlign" value="' + (salign == 'lt' ? 'lt' : '') + '" />\n'
        + '<param name="Base" value="" />\n'
        + '<param name="Scale" value="' + (scale == 'noscale' ? 'noscale' : 'ShowAll') + '" />\n'
        + '<param name="DeviceFont" value="0" />\n'
        + '<param name="EmbedMovie" value="0" />\n'
        + '<param name="BGColor" value="" />\n'
        + '<param name="SWRemote" value="" />\n'
        + '<param name="MovieData" value="" />\n'
        + '<param name="SeamlessTabbing" value="1" />\n'
        + '<embed src="' + m + '" quality="high" id="' + oI + '" name="' + oI + '" scale="' + scale + '" salign="' + salign + '" menu="' + menu + '" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="' + w + '" height="' + h + '" />\n'
        + '</object>';
    prnt.innerHTML = output;
};
window.onload = function() {
    downloads();
    if (imgs) slide();
};
