function setActive(elid)
{
    addClass(elid);
}

function addClass(elid) {
    jQuery('#'+elid).attr('class',"typeface-js active");
}


jQuery(document).ready(function(){
    try {        
        jQuery(".hover_title").append('<em></em>');
            jQuery(".hover_title").append('<img src="/skin/frontend/default/shelfreliance/images/help.png">');
            jQuery(".hover_title").hover(function() {
              jQuery(this).find("em").animate({opacity: "show"}, "slow");
              var title = jQuery(this).attr('hover_title');
              var box = jQuery(this).find("em");
              box.html(title);              
        }, function() {
          jQuery(this).find("em").animate({opacity: "hide"}, "fast");
        });
        
        jQuery(".more_info").append('<em></em>');            
            jQuery(".more_info").hover(function() {
              jQuery(this).find("em").animate({opacity: "show"}, "slow");
              var title = jQuery(this).attr('more_info');
              var box = jQuery(this).find("em");
              box.html(title);              
        }, function() {
          jQuery(this).find("em").animate({opacity: "hide"}, "fast");
        });


        jQuery('#guest_search_form').submit(function(){
            return validate_guest_search_form(jQuery(this));
        });

        striplisttable();
        jQuery("table.list tbody tr").mouseover(function(){
            jQuery(this).css('background','#f8e3be');

        });
        jQuery("table.list tr").mouseout(function(){
            striplisttable();
        });

        jQuery("#concernslink").click(function(){
            jQuery(".concernspopup").dialog('open');
        });
        jQuery(".concernspopup").dialog({
            autoOpen:false,
            bgiframe:true,
            modal:true,
            dragable:false,
            resizable:false,
            width:660,
            height:610
        });

        jQuery("#popuplink").click(function(){
            jQuery(".popup").dialog('open');
        });
        jQuery(".popup").dialog({
            autoOpen:false,
            bgiframe:true,
            modal:true,
            dragable:false,
            resizable:false,
            width:660,
            height:610
        });

        jQuery(".video").dialog({
            autoOpen:false,
            bgiframe:true,
            position:top,
            modal:true,
            dragable:true,
            resizable:true,
            width:575,
            height:400
        });

        jQuery(".video1").dialog({
            autoOpen:false,
            bgiframe:true,
            position:top,
            modal:true,
            dragable:true,
            resizable:true,
            width:575,
            height:400
        });

        jQuery(".video2").dialog({
            autoOpen:false,
            bgiframe:true,
            position:top,
            modal:true,
            dragable:true,
            resizable:true,
            width:575,
            height:400
        });

        jQuery('#emails').change(function(){
            checkEmail();
        });

        jQuery(".update_status_link").click(function(){
            var form_content = jQuery(this).parent().find('.form_content');
            var status = form_content.find(".status").val();
            var id = form_content.find('.guest_id').val();
            jQuery('.update_popup form #status_'+status).attr('checked',true);
            jQuery('.update_popup form #guest_id').val(id);
            jQuery('.update_popup').dialog('open');
        });
        jQuery(".update_popup").dialog({
            autoOpen:false,
            bgiframe:true,
            modal:true,
            dragable:false,
            resizable:false,
            width:300
        });

        jQuery('#update_status_btn').click(function(){
            updateStatus();
        });
        jQuery('#feedback-tab').click(function(){
            jQuery("#feedback_popup").dialog('open');

        });
        jQuery("#feedback_popup").dialog({
            autoOpen:false,
            bgiframe:true,
            modal:true,
            dragable:false,
            resizable:false,
            width:600
        });

        jQuery('#feedback-submit').click(function(){
            jQuery('#feedback-form-submitting').show();
            jQuery.ajax({
                type:'POST',
                url:'/parties/index/comment',
                dataType: 'json',
                data:{
                    email:jQuery('#feedback-email').val(),
                    comment:jQuery('#feedback-comment').val(),
                    page:jQuery('#feedback-page').val(),
                    user_info:jQuery('#feedback-user-info').val()
                },
                success: function(data){
                    if(data.msg){
                        jQuery('#feedback_message').html(data.msg);
                        jQuery('#feedback_message').show();
                        jQuery('#feedback_form').hide();
                        jQuery('#feedback_error').html('');
                        jQuery('#feedback_error').hide('');
                    }
                    if(data.error){
                        jQuery('#feedback_error').html(data.error);
                        jQuery('#feedback_error').show();
                        jQuery('#feedback-form-submitting').hide();
                    }
                },
                error: function(XMLHttpRequest,textStatus,errorThrown){
                    alert('error submitting the form');
                }
            });
            return false;
        });

        jQuery("#invitation_invite_btn").click(function(){
            jQuery(".invitation_guest_invite").dialog('open');
        });
        jQuery(".invitation_guest_invite").dialog({
            autoOpen:false,
            bgiframe:true,
            modal:true,
            dragable:false,
            resizable:false,
            width:470,
            buttons: {
                'Send Message': function() {
                    jQuery('#invitation_invite_form').submit();
                    jQuery(this).dialog('close');
                },
                Cancel: function() {
                    jQuery(this).dialog('close');
                }
            }
        });

        jQuery(".password").dialog({
            autoOpen:false,
            bgiframe:true,
            modal:true,
            dragable:false,
            resizable:false,
            width:470,
            buttons: {
                'Submit': function() {
                    jQuery('#password_form').submit();
                    jQuery(this).dialog('close');
                },
                Cancel: function() {
                    jQuery(this).dialog('close');
                }
            }
        });
        displayTab('1');
        jQuery('.tabstitle li').click(function(){
           displayTab(jQuery(this).attr('id'));
        });
    } catch(exception) {}
});

function displayTab(id){
    jQuery('.tabstitle li').removeClass('active');
    jQuery('.tabbody').hide();
    jQuery('.tabstitle li#'+id).addClass('active');
    jQuery('.tabsbody #b'+id).show();
}

function validate_guest_search_form(thisform){
    with(thisform){
        var fields = new Array();        
        fields[1] = new field('','search_email','',new Array('2'));
        return validatefields(thisform,fields);
        }
}

function checkEmail(){
    var emails = jQuery("#emails").val();
    var emailarray = new Array();
    emailarray = emails.split(',');

    var validemails = "";
    var invalidemails = "";
    var invalidemailtitle = "Invalid emails \n";
    for(var i = 0; i < emailarray.length; i++){

        if(checkEmailString(emailarray[i])){
            var comma = (validemails)?",":"";
            validemails += comma + emailarray[i];
        }else{
            invalidemails += emailarray[i]+"\n";
        }
    }

    if(invalidemails.length > 1){
        alert(invalidemailtitle+invalidemails);
    }

    jQuery("textarea#emails").val(validemails);

}
function striplisttable(){
    jQuery("table.list tbody tr:odd").css('background','#eefafa');
    jQuery("table.list tbody tr:even").css('background','#fff');
}
function removeGuest(id){
    if(confirm('Are you sure?')){        
        var ajax_loading = jQuery('tr#'+id).find('.ajax-loading');
        ajax_loading.show();
       
        jQuery.ajax({
            type:'POST',
            url:'/parties/party/deleteguest',
            dataType: 'json',
            data:{
                guest_id:id
            },
            success: function(data){
                var html = '';                
                if(data.msg){
                    html = '<div class="messageblock">'+data.msg+'</div>';
                    jQuery('tr#'+id).remove();                    
                }
                if(data.error){
                    html = '<div class="errorblock">'+data.error+'</div>';
                    jQuery('#message').html(data.error);                                        
                }

                jQuery('#message').html(html);
                jQuery('#message').show();
                ajax_loading.hide();
            },
            error: function(XMLHttpRequest,textStatus,errorThrown){
                alert('error deleting the guest');
                ajax_loading.hide();
            }
        });
       
    }
    return false;
}
function updateStatus(){
    var ajax_loading = jQuery('.update_popup .ajax-loading');
    ajax_loading.show();
    var g_id = jQuery('.update_popup form #guest_id').val();
   

    jQuery.ajax({
        type:'POST',
        url:'/parties/party/acceptajax',
        dataType: 'json',
        data:{
            guest_id:g_id,
            acceptance:jQuery('.update_popup form input[name=status]:checked').val(),
            notguest:true
        },
        success: function(data){
            var html = '';
            if(data.status){
                html = '<div class="messageblock">Status has been successfully updated</div>';
                var rsvp_box = jQuery('tr#'+g_id).find('#rsvp');
                var status_field = jQuery('tr#'+g_id).find('.status');
                status_field.val(data.status);
                rsvp_box.html(data.rsvp);
            }
            if(data.error){
                html = '<div class="errorblock">'+data.error+'</div>';
            }

            jQuery('#message').html(html);
            jQuery('#message').show();
            ajax_loading.hide();
            jQuery('.update_popup').dialog('close');
        },
        error: function(XMLHttpRequest,textStatus,errorThrown){
            alert('error updating the guest');
            ajax_loading.hide();
            jQuery('.update_popup').dialog('close');
        }
    });
}

function ajaxLoad(parent_id,msg){
    jQuery('.ajax_loading').remove();
    jQuery('#'+parent_id).prepend('<div class="ajax_loading">'+msg+'</div>');
    jQuery('.ajax_loading').show();
}

function ajaxUnload(){
    jQuery('.ajax_loading').remove();
}

function ajaxLoading(buttonid,msg){
    jQuery('#'+buttonid).hide();
    jQuery('#'+buttonid).parent().append('<div class="ajax_loading">'+msg+'</div>');   
    jQuery('.ajax_loading').show();
}
function ajaxUnLoading(buttonid){
    jQuery('#'+buttonid).show();
}


