$(function(){

$("#colorboxtest").colorbox({width:680, height:"80%", iframe:true, href:"/sites"});
$("#colorboxtest1").colorbox({width:680, height:"80%", iframe:true, href:"/sites"});
$("#colorboxtest2").colorbox({width:680, height:"80%", iframe:true, href:"/sites"});
$("#colorboxtest3").colorbox({width:680, height:"80%", iframe:true, href:"/sites"});
$("#colorboxtest4").colorbox({width:680, height:"80%", iframe:true, href:"/sites"});


    tabs = $(".block-tabs");
    if (tabs) {
        $('a', tabs).bind('click', null, function(){
            $('.block-home-top > div:visible').hide();
            $($(this).attr('href')).show();
            $('li', '.block-tabs').removeClass('active');
            $(this).parent().addClass('active');
            return false;
        });
    }

    $('#feedback').ajaxForm({
        success: function() {
           $('#feedback-form').hide();
           $('#feedback-video').show();

           el = $("input[name='name']");
           el.val(null);
           el.blur();

           el = $("input[name='video']");
           el.val(null);
           el.blur();

           el = $("textarea[name='comment']");
           el.val(null);
           el.blur();
        }
    });
    
    $('.notify_clone_block input, .notify_clone_block select').each(function(){
        $(this).attr('name', $(this).attr('name') + '[]') ;
    })
    
    notify_bind();

});

var update_remove_marker = function() {
    $('a.remove-this').show();
    if($('.block-notification-panel .remove-this').length == 1) {
        $('a.remove-this:first').hide();
    }
    
    $('a.remove-this').unbind('click');
    $('a.remove-this').click(function(){
       $(this).parents('.notify_clone_block').remove();
       update_remove_marker();
       update_clone_marker();
       return false;
    });
}

var update_clone_marker = function() {
    $('.block-notification-panel .add-another').hide();
    
    if($('.notify_clone_block').length < 5) {
        $('.block-notification-panel .add-another:last').show();
    }
}

var update_autocomlete = function() {
    
    function format(mail) {
        return mail.name;
    }
    
    $(".au_complete").each(function(){
        var context = $(this);
        
        if(!$(this).data('is_autocomplete')) {
            $(this).autocomplete("/assoc/get-values-ajax/", {
                dataType: "json",
                parse: function(data) {
                    return $.map(data, function(row) {
                        return {
                            data: row,
                            value: row.id,
                            result: row.name
                        }
                    });
                },
                formatItem: function(item) {
                    return format(item);
                },
                extraParams: {type: function() {            
                        return $('.form-item:first option:selected', context.parents('.notify_clone_block')).val();
                    }
                }
            }).result(function(e, item) { /* */});
            
            $(this).data('is_autocomplete', true);
        }
    });
}

var notify_bind = function() {
	$('a.notify_clone_assoc').unbind('click').click(function(){
		if($('.notify_clone_block').length > 4) {
			alert('Maximum 5 associations are allowed');
			return false;
		}
		
        var new_node = $('.notify_clone_block:first').clone().insertAfter('.notify_clone_block:last');

        $('select:last', new_node).appendTo('.block-notification-panel .div-select:last');
        
        $('.comboboxContainer', new_node).remove();
        
        $("select", new_node).combobox(
        {
            comboboxContainerClass: "comboboxContainer",
            comboboxValueContentContainerClass: "comboboxValueContainer",
            comboboxValueContentClass: "comboboxValueContent",
            comboboxDropDownClass: "comboboxDropDownContainer",
            comboboxDropDownButtonClass: "comboboxDropDownButton",
            comboboxDropDownItemClass: "comboboxItem",
            comboboxDropDownItemHoverClass: "comboboxItemHover",
            comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader",
            comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer"
        },
        {
            animationType: "fade"
        });
  
        $('.form-item:last label', new_node).text('The Name of Your Highschool');
        $('.form-item:last input', new_node).val('');
        
        notify_bind();
        
        return false;
    });
    
    update_remove_marker();
    update_autocomlete();
    update_clone_marker();
};

var change_my_pair = function(cb){
    var clone_block = $(cb).parents('.notify_clone_block');
    var label = $('.form-item:last label', clone_block);

    var selectedType = $('option:selected', cb).text();
    if (selectedType != 'Other') {
      var new_label = 'The Name of Your ' + $('option:selected', cb).text().substr(3);
    } else {
      var new_label = 'The ' + selectedType+' Name';
    }
    
    $(label).text(new_label);
    
    $('.form-item:last input', clone_block).val('');
    $('.au_complete').flushCache();
}; 

function reloadParent(name) {
    window.location.href = name;
}

function getNames() {
    var names = new Object();
    names['first_name'] = $('#buildFirstName').val();
    names['last_name'] = $('#buildLastName').val();
    return names;
}


