$(document).ready(function() {
    $('a.tooltip [title],span.tooltip [title]').tipsy({trigger: 'hover',gravity:'s',live:true});
    $('a[rel=gallery]').fancybox();
    if($('#login-form .input').length) {
        $('#login-form .input').each(function() {
            $(this).focus(function () {
                if($(this).attr('value')=='Username'||$(this).attr('value')=='Password') {
                    $(this).attr('value','');
                }
            });
            $(this).blur(function () {
                if($(this).attr('value')=='') {
                    if($(this).attr('name')=='username') {
                        $(this).attr('value','Username');
                    }
                    else {
                        $(this).attr('value','Password');
                    }
                }
            });
        });
    }
    if($('#country_change_uk').length) {
        $('#country_change_uk').change(function() {
            if($(this).val()=='UK') {
                $('.nonukhidden').css('display','');
            }
            else {
                $('.nonukhidden').css('display','none');
            }
        });
    }
    if($('#search-bar .input').length) {
        $('#search-bar .input').each(function() {
            $(this).focus(function () {
                if($(this).attr('value')=='Search...') {
                    $(this).attr('value','');
                }
            });
            $(this).blur(function () {
                if($(this).attr('value')=='') {
                    $(this).attr('value','Search...');
                }
            });
        });
    }
    if($('#mile-to-km').length) {
        $('#mile-to-km input').keyup(function() {
            var miles = $(this).val();
            var kilos = '';
            if(miles==parseFloat(miles)) {
                var calcul = eval(miles*1.609344);
                kilos = calcul.toFixed(2);
            }
            else {
                kilos = '0';
            }
            $('#mile-to-km span').html(kilos);
        });
    }
    if($('#slider').length) {
        $('#slider').twirlie({auto:true,duration:3500});
    }
    if($('.ratingselect').length) {
        $('.ratingselect').each(function(i) {
            $(this).change(function() {
                var rating = 0;
                $('.ratingselect').each(function(i) {
                    rating += parseInt($(this).val());
                });
                rating = (rating/90)*100;
                $('#overallrating').text(rating.toFixed(3));
            });
        });
    }
    if($('#user_article_type').length) {
        $('#user_article_type').change(function(data) {
            if($(this).val()!='#') {
                if($(this).val()=='7') {
                    $('.my_events').css('display','none');
                    $('.all_events').css('display','');
                }
                else {
                    $('.my_events').css('display','');
                    $('.all_events').css('display','none');
                }
            }
        });
    }
    if($('form.validateEvent').length) {
        $('form.validateEvent').submit(function(event){
            event.preventDefault();
            $.ajax({
                url: '../validate.php?' +
                 'id=' + $('form.validateEvent input[name=id]').val() +
                 '&level1=0' +
                 '&name=' + encodeURIComponent($('form.validateEvent input[name=name]').val()) +
                 '&country=' + encodeURIComponent($('form.validateEvent select[name=country]').val()) +
                 '&date=' + encodeURIComponent($('form.validateEvent input[name=yy]').val() + '-' + $('form.validateEvent input[name=mm]').val() + '-' + $('form.validateEvent input[name=dd]').val()) +
                 '&location=' + encodeURIComponent($('form.validateEvent input[name=location]').val()) +
                 '&summary=' + encodeURIComponent($('form.validateEvent textarea[name=summary]').val().substr(0,5)) +
                 '&keywords=' + encodeURIComponent($('form.validateEvent textarea[name=keywords]').val().substr(0,5)) +
                 '&hq_address1=' + encodeURIComponent($('form.validateEvent input[name=hq_address1]').val()) +
                 '&hq_address2=' + encodeURIComponent($('form.validateEvent input[name=hq_address2]').val()) +
                 '&hq_town=' + encodeURIComponent($('form.validateEvent input[name=hq_town]').val()) +
                 '&hq_postcode=' + encodeURIComponent($('form.validateEvent input[name=hq_postcode]').val()) +
                 '&entry_opening=' + encodeURIComponent($('form.validateEvent input[name=entry_opening_yy]').val() + '-' + $('form.validateEvent input[name=entry_opening_mm]').val() + '-' + $('form.validateEvent input[name=entry_opening_dd]').val()) +
                 '&entry_closing=' + encodeURIComponent($('form.validateEvent input[name=entry_closing_yy]').val() + '-' + $('form.validateEvent input[name=entry_closing_mm]').val() + '-' + $('form.validateEvent input[name=entry_closing_dd]').val()) +
                 '&entry_max=' + encodeURIComponent($('form.validateEvent input[name=entry_max]').val()) +
                 '&thumbnail=' + encodeURIComponent($('form.validateEvent input[name=thumbnail]').val()),
                success: function(msg) {
                    if (msg) {
                        alert(msg);
                    } else {
                        $('form.validateEvent').unbind('submit');
                        $('form.validateEvent').submit();
                    }
                }
            });
        });
    }
    $('#plookup').autocomplete({
        source: 'http://www.cyclosport.org/admin/lookup.php?type=post',
        html: true,
        appendTo: 'div.ui-autocomplete',
        position: {
            my: 'left top',
            at: 'left bottom',
            of: $('#plookup'),
            offset: '0 -1'
        },
        delay: '0',
        select: function (event, ui) {
            var linksul = $(this).attr('rel');
            var option = ui.item.ident;
            var ul = document.getElementById(linksul);
            var choices = ul.getElementsByTagName('input');
            if (option == "#") {                
                $(this).val('');
                return false;
            }
            for (var i = 0; i < choices.length; i++) {
                if (choices[i].value == option) {
                    $(this).val('');
                    return false;
                }
            }
            var li = document.createElement('li');
            li.innerHTML = ui.item.value;
            var input = document.createElement('input');
            input.name = 'linkId[]';
            input.type = 'hidden';
            input.value = option;
            li.appendChild(input);
            var input2 = document.createElement('input');
            input2.name = 'linkName[]';
            input2.type = 'hidden';
            input2.value = ui.item.value;
            li.appendChild(input2);
            li.setAttribute('onclick', 'removeItem(this);');
            ul.appendChild(li);
            $(this).val('');
            return false;
        },
        open: function (event, ui) {
            $(this).addClass('ui-ac-borders');
        },
        close: function (event, ui) {
            $(this).removeClass('ui-ac-borders');
        }
    });
    $('#e3lookup').autocomplete({
        source: 'http://www.cyclosport.org/admin/lookup.php?type=event',
        html: true,
        appendTo: 'div.ui-autocomplete',
        position: {
            my: 'left top',
            at: 'left bottom',
            of: $('#e3lookup'),
            offset: '0 -1'
        },
        delay: '0',
        select: function (event, ui) {
            var linksul = $(this).attr('rel');
            var option = ui.item.ident;
            var ul = document.getElementById(linksul);
            var choices = ul.getElementsByTagName('input');
            if (option == "#") {                
                $(this).val('');
                return false;
            }
            for (var i = 0; i < choices.length; i++) {
                if (choices[i].value == option) {
                    $(this).val('');
                    return false;
                }
            }
            ul.innerHTML = '';
            var li = document.createElement('li');
            li.innerHTML = ui.item.value;
            var input = document.createElement('input');
            input.name = 'event';
            input.type = 'hidden';
            input.value = option;
            li.appendChild(input);
            var input2 = document.createElement('input');
            input2.name = 'eventName';
            input2.type = 'hidden';
            input2.value = ui.item.value;
            li.appendChild(input2);
            li.setAttribute('onclick', 'removeItem(this);');
            ul.appendChild(li);
            $(this).val('');
            return false;
        },
        open: function (event, ui) {
            $(this).addClass('ui-ac-borders');
        },
        close: function (event, ui) {
            $(this).removeClass('ui-ac-borders');
        }
    });
    if($('select#level1').length) {
        $('select#level1').change(function(event){
            var value = $(this).val();
            if(value=='1') {
                $('.level1').css('display','none');
            }
            else {
                $('.level1').css('display','');
            }
        });
    }
});
function selectLink(select) {
    var option = select.options[select.selectedIndex];
    var ul = document.getElementById("linksUL");
    var choices = ul.getElementsByTagName('input');
    if (option.value == "#") return;
    for (var i = 0; i < choices.length; i++)
    if (choices[i].value == option.value) return;
    var li = document.createElement('li');
    var text = document.createTextNode(option.firstChild.data);
    var input = document.createElement('input');
    input.name = 'linkId[]';
    input.type = 'hidden';
    input.value = option.value;
    li.appendChild(input);
    var input2 = document.createElement('input');
    input2.name = 'linkName[]';
    input2.type = 'hidden';
    input2.value = option.firstChild.data;
    li.appendChild(input2);
    li.appendChild(text);
    li.setAttribute('onclick', 'removeItem(this);');
    ul.appendChild(li);
}
function removeItem(block) {
    if (confirm("Are you sure you want to remove this link?")) {
        block.parentNode.removeChild(block);
    } else {
        return false;
    }
}
function selectLink2(select) {
    var option = select.options[select.selectedIndex];
    var ul = document.getElementById("linksUL2");
    var choices = ul.getElementsByTagName('input');
    if (option.value == "#") return;
    for (var i = 0; i < choices.length; i++)
    if (choices[i].value == option.value) return;
    var li = document.createElement('li');
    var text = document.createTextNode(option.firstChild.data);
    var input = document.createElement('input');
    input.name = 'linkId2[]';
    input.type = 'hidden';
    input.value = option.value;
    li.appendChild(input);
    var input2 = document.createElement('input');
    input2.name = 'linkName2[]';
    input2.type = 'hidden';
    input2.value = option.firstChild.data;
    li.appendChild(input2);
    li.appendChild(text);
    li.setAttribute('onclick', 'removeItem2(this);');
    ul.appendChild(li);
}
function removeItem2(block) {
    if (confirm("Are you sure you want to remove this link?")) {
        block.parentNode.removeChild(block);
    } else {
        return false;
    }
}
