$(document).ready(function() {
    $(".auto-populated-field").blur(function(){
        var default_value = $(this).attr("rel");
        if ($(this).val() == ""){
                $(this).val(default_value);
        }
    })
    $(".auto-populated-field").focus(function(){
        var default_value = $(this).attr("rel");
        if ($(this).val() == default_value){
                $(this).val("");
        }
    });
    $('.show-lightbox-link').click(function(){
        if(!$('.lightbox-link').is(":visible")){
            showLightboxLink();
        }else{
            $('.lightbox-link').fadeOut(250);
        }
        return false;
    });
    $('.show-lightbox-link').mouseover(function(){
        if(!$('.lightbox-link').is(":visible")){
            showLightboxLink();
        }
    });
    $('.show-ftp').click(function(){
        $('.ftp-box').fadeToggle(250);
        return false;
    });
    $('.show-ftp').mouseover(function(){
        $('.ftp-box').fadeIn(250);
    });

    bookmarkUpdateCounter();

    $('*').ieffembedfix();
});

function showLightboxLink(){
    $('.lightbox-link').fadeIn(250);
    $('.lightbox-link').html('Loading...');
    $.ajax({
        type: 'POST',
        url: '/test-bookmarks/',
        dataType: 'json',
        data:{
            method:'retrieveId'
        },
        success: function(json){
            $('.lightbox-link').html('iloura.com/lightbox/#' + json.id);
        }
    });
}

function processSearchForm(){
    var searchType = 'all';
    if(window.location.href.indexOf('/film/') != -1){
        searchType = 'film';
    }else if(window.location.href.indexOf('/commercial/') != -1){
        searchType = 'commercial';
    }
    window.location = '/' + searchType + '/#' + $('#search-field').val();
    return false;
}

function searchForm(){
    return processSearchForm();
}

function bookmarkTogglePage(pageId){
    $.ajax({
        type: 'POST',
        url: '/test-bookmarks/',
        dataType: 'json',
        data:{
            method:'togglePageBookmark',
            page_id:pageId
        },
        success: function(json){
            $('.items-in-box').text(json.length);
            bookmarkRefreshToggles(json);
        }
    });
    if(departmentName == 'lightbox'){
        $.bbq.pushState();
    }
    return false;
}
function bookmarkRefreshToggles(json){
    //remove all '-'
    $('.lightbox-toggle-article').each(function(index){
        $(this).removeClass('remove');
    });
    $('.lightbox-toggle-image').each(function(index){
        $(this).removeClass('remove');
    });
    $('.lightbox-toggle-video').each(function(index){
        $(this).removeClass('remove');
    });

    //add '-' where an object is already bookmarked
    for(var i in json){
        if(json[i].objectType == 'page'){
            $('.lightbox-toggle-article-' + json[i].pageId).addClass('remove');
        }else{
            $('.lightbox-toggle-' + json[i].objectType + '-' + json[i].pageId + '-' + json[i].additionalData.replace(/[^A-Za-z0-9]/g, '')).addClass('remove');
        }
    }
}
function bookmarkToggleMedia(pageId, type, additionalData){
    $.ajax({
        type: 'POST',
        url: '/test-bookmarks/',
        dataType: 'json',
        data:{
            method:'toggleMediaBookmark',
            page_id:pageId,
            objectType:type,
            additionalData:additionalData
        },
        success: function(json){
            $('.items-in-box').text(json.length);
            bookmarkRefreshToggles(json);
        }
    });
    if(departmentName == 'lightbox'){
        //alert('lightbox');
        $.bbq.pushState();
    }
    return false;
}

function bookmarkUpdateCounter(){
    $.ajax({
        type: 'POST',
        url: '/test-bookmarks/',
        dataType: 'json',
        data:{
            method:'retrieveBookmarks'
        },
        success: function(json){
            $('.items-in-box').text(json.length);
        }
    });

    return false;
}
