function initVideo(){
    var _btn = $('ul.video-list > li > a');

    _btn.click(function(){
        var _ind = _btn.index($(this));
        _btn.parent('li').show();
        _btn.eq(_ind).parent('li').hide();
        $('ul.videolist > li').css('top', -9999);

        var _href = $(this).attr('href');
        $('ul.videolist > li').find('object').hide();
        $(_href).show();
        
        $(_href).parent('li').css('top', 0);

        try{
            var _video_id = _href.replace(/#/g, "");
            document.getElementById(_video_id).sendEvent("PLAY","true");
        }catch(e){
        }

        return false;
    });
}

function playerReady(obj) {
    var player = document.getElementById(obj['id']);
    player.addModelListener("TIME","timeListener");
    if($('#'+obj.id).css('display') != "none")  document.getElementById(obj['id']).sendEvent("PLAY","true");
}

function timeListener(obj) {
    if($('#'+obj.id).parent('li').css('top') != '0px')  document.getElementById(obj['id']).sendEvent("STOP","true");
}


function initLabels(){
    var change_speed = 400;
    var _box = $('div.input-box');

    _box.each(function(){
        var _pop = $(this).find('label');
        _pop.show();

        var _h = _pop.outerHeight();
        _pop.css('top', _h);

        $(this).mouseover(function(){
            _pop.animate({
                top: 0
            }, {
                queue:false,
                duration:change_speed
            });
        }).mouseout(function(){
            _pop.animate({
                top: _h
            }, {
                queue:false,
                duration:change_speed
            });
        });
    });
}

function initFields(){
    for (i=0; i<=conf_error_fields.length; i++){
        var field = conf_error_fields[i];
        $('#'+field).addClass('error').focusin(function(){
            $(this).removeClass('error');
        });
    }
}

function initHideForm(){
    var _hold;
    var _count = 0;

    var _yes = $('input.yes');
    var _box = $('div.area');
    if(_yes.attr('checked') == true){
        _box.fadeIn();
    } else {
        _box.fadeOut();
    }

    var _rads = $('input.rad');
    _rads.change(function(){
        if(_yes.attr('checked') == true){
            _box.fadeIn();
        } else {
            _box.fadeOut();
        }
    });

    var _cloneBtn = $('a.clone');
    _cloneBtn.click(function(){
        _hold = $(this).parents('div.drop');
        cloneThis(_hold);
        return false;
    });

    function cloneThis(_holder){
        _count++;

        var _holdNew = _holder.clone();
        _box.append(_holdNew);

        var _inp = _holdNew.find('input.company-field');
        _inp.attr('id', 'company-input'+_count);
        //changed by BBL _inp.attr('name', 'company-input'+_count);
        _inp.attr('name', 'companies[]');
        _inp.val('Company names');

        var _newBtn = _holdNew.find('a.clone');
        _newBtn.click(function(){
            _hold = $(this).parents('div.drop');
            cloneThis(_hold);
            return false;
        });
    }
}

function initPopup(){
    var _opener = $('div.box-map');
    $('ul.videolist > li').find('object').hide();

    _opener.click(function(){
        var _href = $('ul.video-list > li.active').find('a').attr('href');
        $('ul.video-list > li').show();
        $('ul.video-list > li.active').find('a').parent('li').hide();
        $(_href).show();
        $('ul.videolist > li').removeClass('active');
        $(_href).parents('li').addClass('active');
        $(_href).parents('li').css('top', 0);

        var change_speed = 1000;
        var _fader = $('<div id="jquery-overlay"></div>');
        var _box = $('div.video');
        _box.css('zIndex','999');

        $('body').append(_fader);

        var _height = 0;
        var _width = 0;
        var _page = $('body > div:eq(0)');
        var _minWidth = _page.outerWidth();

        if (window.innerHeight) {
            _height = window.innerHeight;
            _width = window.innerWidth;
        } else {
            _height = document.documentElement.clientHeight;
            _width = document.documentElement.clientWidth;
        }

        if (_width < _minWidth) {
            _fader.css('width',_minWidth);
        } else {
            _fader.css('width','100%');
        }
        
        if (_height > _page.innerHeight()){
            _fader.css('height',_height);
        } else {
            _fader.css('height',_page.innerHeight());
        }

        _fader.css({
            'position':'absolute',
            'top':0,
            'left':0,
            'backgroundColor':'#000',
            'zIndex':'890',
            'opacity':'0'
        });

        _fader.animate({
            opacity: 0.6
        }, {
            queue:false,
            duration:change_speed
        });

        var _select = $('select');
        var _labels = $('label');
        _select.css('visibility', 'hidden');
        _labels.css('visibility', 'hidden');
        _box.css('top', 120);

        $(window).resize(function(){
            if (window.innerHeight) {
                _height = window.innerHeight;
                _width = window.innerWidth;
            } else {
                _height = document.documentElement.clientHeight;
                _width = document.documentElement.clientWidth;
            }

            if (_width < _minWidth) {
                _fader.css('width',_minWidth);
            } else {
                _fader.css('width','100%');
            }
            
            if (_height > _page.innerHeight()) {
                _fader.css('height',_height);
            } else {
                _fader.css('height',_page.innerHeight());
            }
        });

        var _closer = $('a.btn-close');
        _closer.click(function(){
            _fader.fadeOut().delay(800).queue(function () {
                _fader.remove()
            });
            _select.css('visibility', 'visible');
            _labels.css('visibility', 'visible');
            _box.css('top', -9999);
            $('ul.videolist > li').find('object').hide();
            return false;
        });

        _box.mouseover(function(){
            $(this).addClass('over');
        }).mouseout(function(){
            $(this).removeClass('over');
        });

        $('body').click(function(){
            if (!_box.hasClass('over')) {
                _fader.fadeOut().delay(800).queue(function () {
                    _fader.remove()
                });
                _select.css('visibility', 'visible');
                _labels.css('visibility', 'visible');
                _box.css('top', -9999);
                $('ul.videolist > li').find('object').hide();
            }
        });

        try{
            var _video_id = _href.replace(/#/g, "");
            document.getElementById(_video_id).sendEvent("PLAY","true");
        }catch(e){
        }

        return false;
    });
}

function initMap(){
    var _opener = $('div.box-route-map');

    _opener.click(function(){
        var change_speed = 1000;
        var _fader = $('<div id="jquery-overlay"></div>');
        var _box = $('div.map');
        _box.css('zIndex','999');
    
        $('body').append(_fader);

        var _height = 0;
        var _width = 0;
        var _page = $('body > div:eq(0)');
        var _minWidth = _page.outerWidth();

        if (window.innerHeight) {
            _height = window.innerHeight;
            _width = window.innerWidth;
        } else {
            _height = document.documentElement.clientHeight;
            _width = document.documentElement.clientWidth;
        }

        if (_width < _minWidth) {
            _fader.css('width',_minWidth);
        } else {
            _fader.css('width','100%');
        }
        
        if (_height > _page.innerHeight()) {
            _fader.css('height',_height);
        } else {
            _fader.css('height',_page.innerHeight());
        }

        _fader.css({
            'position':'absolute',
            'top':0,
            'left':0,
            'backgroundColor':'#000',
            'zIndex':'890',
            'opacity':'0'
        });

        _fader.animate({
            opacity: 0.6
        }, {
            queue:false,
            duration:change_speed
        });

        var _select = $('select');
        var _labels = $('label');
        _select.css('visibility', 'hidden');
        _labels.css('visibility', 'hidden');
        _box.css('top', 120);

        $(window).resize(function(){
            if (window.innerHeight) {
                _height = window.innerHeight;
                _width = window.innerWidth;
            } else {
                _height = document.documentElement.clientHeight;
                _width = document.documentElement.clientWidth;
            }

            if (_width < _minWidth) {
                _fader.css('width',_minWidth);
            } else {
                _fader.css('width','100%');
            }
            
            if (_height > _page.innerHeight()) {
                _fader.css('height',_height);
            } else {
                _fader.css('height',_page.innerHeight());
            }
        });

        var _closer = $('a.btn-close');
        _closer.click(function(){
            _fader.fadeOut().delay(800).queue(function () {
                _fader.remove()
            });
            _select.css('visibility', 'visible');
            _labels.css('visibility', 'visible');
            _box.css('top', -9999);
            return false;
        });

        _box.mouseover(function(){
            $(this).addClass('over');
        }).mouseout(function(){
            $(this).removeClass('over');
        });

        $('body').click(function(){
            if (!_box.hasClass('over')) {
                _fader.fadeOut().delay(800).queue(function () {
                    _fader.remove()
                });
                _select.css('visibility', 'visible');
                _labels.css('visibility', 'visible');
                _box.css('top', -9999);
            }
        });

        return false;
    });
}

function closeLayer(){
    $('a.btn-close').click();
}

$(document).ready(function(){
    initLabels();
    initFields();
    initHideForm();
    initVideo();
    initMap();
    initPopup();

    $("#datepicker").datepicker({
        showWeek: true,
        firstDay: 1,
        showOn: 'button',
        buttonImage: 'images/link-calendar.gif',
        buttonImageOnly: true
    });
});

