function initVideoAjax(){
    var _link = $$('.video ul li a');
    var _destination = $$('.video-block #vid-hld');
    if (_destination.length) _destination = _destination[0];
    if (_link.length && _destination){
        _link.each(function(el, i){
            el.href = 'javascript:;';
            el.onclick = function(){
                startAjax(_destination, this.rel);
                return false;
            }
        ;})
    }
}
function startAjax(_this, param){
    _this.setHTML('<img src="wp-content/themes/hipolicy/images/loading-bar-black.gif" alt="load" class="load-img"/>');
    var url = "index.php?id=" + param+'&ajax=1';
    var flag = true;
    var ajx = new Ajax(url, {
         method: 'get',
         onComplete: function(e){ if (flag) {flag = false; _this.innerHTML = e;}}
    }).request();
    ajx.request();
}

var _slink;
var _div;
var currentImg;
var _duration = 3000;
var big_img = [];
var _timer;
var newFader;
var newTitle;
function initGallery(){
    _slink = $$('.photo-gallery ul li a');
    _div = $('big-pic');
    if (_slink.length && _div){
        _div.innerHTML = '';
        _div.style.position = 'relative';
        _div._length = _slink.length;
        newFader = new Element('div', {'id': 'img-fader', 'styles': {'display': 'block', 'position': 'absolute', 'height':'106px', 'top': '150px', 'width':'448px'}}).injectInside(_div);
        newFader.moo = new Fx.Styles($(newFader), {duration: 350});
        newFader.moo.set({'opacity':0.4});
        newTitle = new Element('div', {'id': 'img-title', 'styles': {'display': 'block', 'position': 'absolute', 'height':'26px', 'top': '16px', 'left': '16px', 'width':'448px'}}).injectInside(newFader);        
        _slink.each(function(el, i){  
            el._href = el.href;
            el._img = el.getElements('img')[0];
            el._title = el.rel;
            el._index = i;
            el.href = 'javascript:;';
            addBigImg(_div, el);
            el.onclick = function(){
                if (_timer) clearTimeout(_timer);
                switchImg(this._index);
            }
        ;});
        if (big_img.length){
            big_img.each(function(_a, _i){
                if (_a.moo){
                    _a.moo.set({'opacity':0});
                }
            ;});  
        }
        switchImg(0);
    }
}
function addBigImg(_holder, _el){
    var newLink = new Element('a', {'href': _el._href, 'styles': {'display': 'block', 'position': 'absolute', 'height':'256px', 'width':'448px'}}).injectInside(_holder);
    var newImg = new Element('img', {'src': _el._img.src, 'styles': {'display': 'block', 'height':'256px', 'width':'448px'}}).injectInside(newLink);
    newLink.moo = new Fx.Styles($(newLink), {duration:700});
    big_img.push(newLink);
}
function switchImg(_curr){
    if (_slink.length && _div){
        if (_curr >= _slink.length){_curr = 0;}
        big_img.each(function(_a, _i){
            if (_a.moo && _curr != _i){
                _a.moo.stop();
                _a.moo.custom({'opacity':[0]});
            }
        ;});
        if (big_img[_curr].moo){
            big_img[_curr].moo.stop();
            big_img[_curr].moo.custom({'opacity':1});
            newTitle.innerHTML = _slink[_curr]._title;
        }
    }
    _curr++;
    _timer = setTimeout('switchImg('+_curr+')', _duration);
}

var one_page_img_count = 7;
var flkr_width = 612;
function initFlkrPager(){
    var page_count = $('count_page');
    var curr_page = $('curr_page');
    var prev_link = $('prev_link');
    var next_link = $('next_link');
    var imgs = $$('.events-holder .events .flickr_badge_image');
    var _holder = $$('.events-holder .events')[0];
    _holder.moo = new Fx.Styles($(_holder), {duration:500});
    if (page_count && curr_page && prev_link && next_link && imgs.length){
        var _count = Math.ceil(imgs.length/one_page_img_count);
        var _curr = 0;
        page_count.innerHTML = _count;
        prev_link.href = 'javascript:;';
        prev_link.onclick = function(){
            if (--_curr < 0) _curr = _count-1;
            _holder.moo.stop();
            _holder.moo.custom({'left':-_curr*flkr_width});
            curr_page.innerHTML = _curr+1;
        }
        next_link.href = 'javascript:;';
        next_link.onclick = function(){
            if (++_curr >= _count) _curr = 0;
            _holder.moo.stop();
            _holder.moo.custom({'left':-_curr*flkr_width});
            curr_page.innerHTML = _curr+1;
        }        
    }
}
function initAllFunction(){
    initVideoAjax();
    initGallery();
    initFlkrPager();
}

if (window.addEventListener)
	window.addEventListener("load", initAllFunction, false);
else if (window.attachEvent && !window.opera)
	window.attachEvent("onload", initAllFunction);