$(document).ready(prepare);

function prepare()
{
    if ($('#add_news').length == 1)
    {
        $('#add_news').maxlength({
            'feedback' : '.chars_left'
        });
    }

    hover_change_bg('user', '#fff', '#f4f4f4');
    hover_change_bg('theme_item', '#fff', '#f4f4f4');

    $('.since').live('mouseover', function(){ $('#'+this.id+' > .time_since').hide(); $('#'+this.id+' > .full_date').show(); });
    $('.since').live('mouseout', function(){ $('#'+this.id+' > .time_since').show(); $('#'+this.id+' > .full_date').hide(); });

    $('.since_2').live('mouseover', function(){ $('#'+this.id+' > .time_since').hide(); $('#'+this.id+' > .full_date').show(); });
    $('.since_2').live('mouseout', function(){ $('#'+this.id+' > .time_since').show(); $('#'+this.id+' > .full_date').hide(); });

    $('.since_3').live('mouseover', function(){ $('#'+this.id+' > .time_since').hide(); $('#'+this.id+' > .full_date').show(); });
    $('.since_3').live('mouseout', function(){ $('#'+this.id+' > .time_since').show(); $('#'+this.id+' > .full_date').hide(); });

    $('.news_since').live('mouseover', function(){ $('#'+this.id+' > .time_since').hide(); $('#'+this.id+' > .full_date').show(); });
    $('.news_since').live('mouseout', function(){ $('#'+this.id+' > .time_since').show(); $('#'+this.id+' > .full_date').hide(); });

    $('.album_since').live('mouseover', function(){ $('#'+this.id+' > .time_since').hide(); $('#'+this.id+' > .full_date').show(); });
    $('.album_since').live('mouseout', function(){ $('#'+this.id+' > .time_since').show(); $('#'+this.id+' > .full_date').hide(); });

    $('.comment_since').live('mouseover', function(){ $('#'+this.id+' > .time_since').hide(); $('#'+this.id+' > .full_date').show(); });
    $('.comment_since').live('mouseout', function(){ $('#'+this.id+' > .time_since').show(); $('#'+this.id+' > .full_date').hide(); });

    $('.news_item').live('mouseover', function(){ $('#'+this.id+' > .news_content > .item_control').show(); });
    $('.news_item').live('mouseout', function(){ $('#'+this.id+' > .news_content > .item_control').hide(); });

    $('.comment_item').live('mouseover', function(){ $('#'+this.id+' > .comment > .item_info > .item_control').show(); });
    $('.comment_item').live('mouseout', function(){ $('#'+this.id+' > .comment > .item_info > .item_control').hide(); });

    $('.album_item').live('mouseover', function(){ $('#'+this.id+' > .photo_item > .item_control_2').show(); });
    $('.album_item').live('mouseout', function(){ $('#'+this.id+' > .photo_item > .item_control_2').hide(); });

    $('.topic_item').live('mouseover', function(){ $('#'+this.id+' > .topic_control').show(); });
    $('.topic_item').live('mouseout', function(){ $('#'+this.id+' > .topic_control').hide(); });


    // Tour Steps :
    $('#tour_step_sections').live('mouseover', function(){ $('#tour_sections').show(); });
    $('#tour_step_sections').live('mouseout', function(){ $('#tour_sections').hide(); });

    $('#tour_step_news').live('mouseover', function(){ $('#tour_news').show(); });
    $('#tour_step_news').live('mouseout', function(){ $('#tour_news').hide(); });

    $('#tour_step_topic').live('mouseover', function(){ $('#tour_topic').show(); });
    $('#tour_step_topic').live('mouseout', function(){ $('#tour_topic').hide(); });

    $('#tour_step_album').live('mouseover', function(){ $('#tour_album').show(); });
    $('#tour_step_album').live('mouseout', function(){ $('#tour_album').hide(); });

    $('#tour_step_favorite').live('mouseover', function(){ $('#tour_favorite').show(); });
    $('#tour_step_favorite').live('mouseout', function(){ $('#tour_favorite').hide(); });

    $('#tour_step_settings').live('mouseover', function(){ $('#tour_settings').show(); });
    $('#tour_step_settings').live('mouseout', function(){ $('#tour_settings').hide(); });

    $('#tour_step_invite').live('mouseover', function(){ $('#tour_invite').show(); });
    $('#tour_step_invite').live('mouseout', function(){ $('#tour_invite').hide(); });

    $('#tour_step_admin').live('mouseover', function(){ $('#tour_admin').show(); });
    $('#tour_step_admin').live('mouseout', function(){ $('#tour_admin').hide(); });

    $('#tour_step_addpanel').live('mouseover', function(){ $('#add_panel').show(); show_add_new_form('news'); $('#add_news').focus(); });
    // $('#tour_step_addpanel').live('mouseout', function(){ $('#add_panel').hide(); });

    //
}

function hover_change_bg(item_class, hover_bg, normal_bg)
{
    $('.'+item_class).live('mouseover', function(e){
        $('#'+$(this).attr('id')).css('background', hover_bg);
    });

    $('.'+item_class).live('mouseout', function(e){
        $('#'+$(this).attr('id')).css('background', normal_bg);
    });
}

function hover_show_hide(item_class, id_prefix)
{
    $('.'+item_class).live('mouseover', function(e){
        $('#'+$(this).attr('id')+id_prefix).show();
    });

    $('.'+item_class).live('mouseout', function(e){
        $('#'+$(this).attr('id')+id_prefix).hide();
    });
}

function input_onfocus(e, default_value)
{
    var value = $(e).attr('value');
    if (value == default_value) {
      $(e).attr('value', '');
      $(e).css('color', '#222');
    }
}

function input_onblur(e, default_value)
{
    var value = $(e).attr('value');
    if (value == '') {
      $(e).attr('value', default_value);
      $(e).css('color', '#666');
    }
}

function input_clear(input_id, default_value)
{
    if ($('#'+input_id).val() == default_value)
    {
        $('#'+input_id).val('');
    }

    return true;
}

function check_if_empty(fields, errors, section)
{
    for (i = 0; i < fields.length; i++)
    {
        if ($('#'+fields[i]).val() == '' || $('#'+fields[i]).val() == 0)
        {
            alert('حقل '+errors[i]+' فارغ');
            $('#'+fields[i]).focus();
            if (section != '')
            {
                $('#'+section+'_button').show();
                $('#'+section+'_loading').hide();
            }
            return false;
        }
    }

    return true;
}

function ajax_post(page, data)
{
    $('#ajax_loader_'+page).show();

    $.ajax({
       type: "POST",
       url: group_full_url+'user/'+page+'/',
       data: data,
       success: function(r) {
            eval('var o = '+r);

            $('#ajax_loader_'+page).hide();
            $('#output_'+page).html(o.msg);

            if (o.type == 'error') {
                $('#output_'+page).css('color', '#CC0033');
                $('#output_'+page).prepend('<img src="'+base_url+'images/icons/small_sad_smiley.png" alt="" /> ');
            } else {
                $('#output_'+page).css('color', '#006600');
                $('#output_'+page).append(' <img src="'+base_url+'images/icons/small_smiley.png" alt="" />');
            }

            $('#output_'+page).show();

            if (o.current_time != undefined)
            {
                $('#current_time').html(o.current_time);
            }
       }
     });
}


function ajax_get(page)
{
    return $.ajax({ url: group_full_url+page, type: "GET", async: false }).responseText;
}

function delete_user_pic()
{
    $('#ajax_loader_delete_picture').show();

    ajax_get('user/delete_picture');

    $('#control_user_pic').hide();
    $('#default_user_pic').show();
    $('#ajax_loader_delete_picture').hide();
}

function edit_in_place(edit_page, prefix, id, css_class)
{
    var content = $('#'+prefix+'_'+id+'_content').html();
    content = content.replace(/<br>/gi, "");
    content = content.replace(/<br \/>/gi, "");

    $('#'+prefix+'_'+id+'_content').hide();
    $('#'+prefix+'_'+id+'_status').hide();
    $('#'+prefix+'_'+id+'_textarea').show();

    $('#'+prefix+'_'+id+'_edit').html('<div align="center"><input type="button" value="حفظ" onclick="do_editing(\''+edit_page+'\', \''+prefix+'\', \''+id+'\')" /> <input type="button" value="إلغاء" onclick="cancel_editing(\''+prefix+'\', \''+id+'\')" /></div>');
    $('#'+prefix+'_'+id+'_edit').show();
}


function cancel_editing(prefix, id)
{
    $('#'+prefix+'_'+id+'_edit').hide();
    $('#'+prefix+'_'+id+'_textarea').hide();
    $('#'+prefix+'_'+id+'_content').show();
}

function do_editing(edit_page, prefix, id)
{
    var new_content = $('#'+prefix+'_'+id+'_textarea').val();

    $('#'+prefix+'_'+id+'_content').html(new_content.replace(/\n/gi, "<br />"));

    $('#'+prefix+'_'+id+'_edit').hide();
    $('#'+prefix+'_'+id+'_textarea').hide();
    $('#'+prefix+'_'+id+'_content').show();

    $('#'+prefix+'_'+id+'_status').html('<img src="'+base_url+'images/ajax-loader.gif" alt="" /> <small>جاري التعديل ..</small>');
    $('#'+prefix+'_'+id+'_status').show();

    $.ajax({
       type: "POST",
       url: group_full_url+edit_page,
       data: 'id='+id+'&new_content='+new_content,
       success: function(r) {
            eval('var o = '+r);

            if (o.type == 'error') {
                $('#'+prefix+'_'+id+'_status').html('<small class="required">'+o.msg+'</small>');
            } else {
                $('#'+prefix+'_'+id+'_status').hide();
            }
       }
     });
}

function show_comments(section, id)
{
    $('#'+section+'_'+id+'_clink').append('<img src="'+base_url+'images/ajax-loader.gif" alt="" id="'+section+'_'+id+'_loading" style="padding-right: 4px" />');
    var comments = $.ajax({ url: group_full_url+'comment/show/'+section+'/'+id, type: "GET", async: false }).responseText;

    $('#'+section+'_'+id+'_comments').html('<div class="comments_title">التعليقات</div>');
    $('#'+section+'_'+id+'_comments').append(comments);
    $('#'+section+'_'+id+'_comments').show();

    $('#'+section+'_'+id+'_loading').remove();

    rounded_corners();
}

function add_comment(button, section, id, rand_id)
{
    var comment = $('#newcomment_'+rand_id).val();
    if (comment == '') return;

    $(button).after('<span id="'+section+'_'+id+'_loading" style="padding-right: 4px; font-size: 11px"><i>جاري إضافة التعليق ..</i></div>');

    $.ajax({
       type: "POST",
       url: group_full_url+'comment/add/'+section+'/'+id,
       data: 'comment='+comment,
       success: function(r) {
            if (r != '0')
            {
                if ($('#'+section+'_'+id+'_nocomments').length != 0)
                {
                    $('#'+section+'_'+id+'_nocomments').hide();
                    $('#'+section+'_'+id+'_nocomments').before('<div id="'+section+'_'+id+'_commentslist"></div>');
                }
				
				$('#add_comment_'+rand_id).before(r);
                //$('#'+section+'_'+id+'_commentslist').append(r);

                $('#'+section+'_'+id+'_loading').remove();

                $('#newcomment_'+rand_id).val('');
            }
       }
     });

     rounded_corners();
}

function show_post(section, id, i)
{
    var post_div = $('#post_'+section+'_'+id+'_'+i);
    $('#'+section+'_'+id+'_'+i+'_clink').append('<img src="'+base_url+'images/ajax-loader.gif" alt="" id="'+section+'_'+id+'_'+i+'_loading" style="padding-right: 4px" />');

    if (post_div.html() == '')
    {
        var post = $.ajax({ url: group_full_url+section+'/show/'+id, type: "GET", async: false }).responseText;
        post_div.html(post);
    }
    else
    {
        post_div.html('');
    }

    $('#'+section+'_'+id+'_'+i+'_loading').remove();
}

function show_photo(id)
{
    /*$('.selected_photo').removeClass('selected_photo');
    $('#photo_'+id).addClass('selected_photo');*/

    var photo = $.ajax({ url: group_full_url+'album/show/'+id+'/full', type: "GET", async: false }).responseText;
    $('#display_photo').html(photo);

    rounded_corners();
}

function show_add_new_form(section)
{
    $('li[class*="add_new_tab_selected"]').removeClass('add_new_tab_selected');
    $('#'+section+'_tab').addClass('add_new_tab_selected');
    $('#add_forms > div').hide();
    $('#add_'+section+'_form').show();
}

function add_post(section, page, data)
{
    $('#ajax_loader_'+section).show();

    $.ajax({
       type: "POST",
       url: group_full_url+section+'/'+page,
       data: data,
       success: function(r) {
            eval('var o = '+r);

            if (o.error != '')
            {
                $('#'+section+'_error').html(o.error);
                $('#'+section+'_error').show();
                $('#ajax_loader_'+section).hide();

                if (section == 'news')
                {
                    $('#news_loading').hide();
                    $('#news_button').show();
                }
            }
            else
            {
                window.location = group_full_url+section;
            }
       }
     });
}


function get_more_posts(section, page, user)
{
    if ($('#the_end').is(":visible"))
        return;

    $('#more_waiting').show();
    $('#the_end').hide();

    if (user != '')
        request = group_full_url+section+'/more/'+page+'/'+user;
    else
        request = group_full_url+section+'/more/'+page;

    $.ajax({
       type: "GET",
       async: false,
       url: request,
       success: function(r)
       {
            if (r == '')
                $('#the_end').show();
            else
                $('#posts').append(r);

            $('#more_waiting').hide();
            rounded_corners();
       }
    });
}

function hide_notice(notice_id)
{
    var response = $.ajax({ url: group_full_url+'user/hide_notice/'+notice_id, type: "GET", async: false }).responseText;

    if (response == 'ok')
    {
        $('#'+notice_id).fadeOut();
    }
}

function rounded_corners()
{
    if ($.browser.msie == true)
        return;

    $('.notice').corner('5px');
    $('#tour').corner('5px');
    $('#tips').corner('4px');
    $('.news_item').corner('left 5px');
    $('.news_content').corner('left 5px');
    $('.topic_item').corner('5px');
    $('.album_item').corner('5px');
    $('.info').corner('bottom 5px');
    $('#photos .item').corner('3px');
    $('.photo_comments').corner('2px');
    $('.comment').corner('left 5px');
    $('.comment_2').corner('left 5px');
    $('.show_type').corner('5px');
    $('.add_comment').corner('5px');
    $('.right_button').corner('right 3px');
    $('.left_button').corner('left 3px');
    $('.commentslist').corner('bottom 5px');
    $('.comments_title').corner('bottom 3px');
    $('.theme_item').corner('3px');
    $('.theme_item_selected').corner('3px');
}

function embed_youtube_video(video_id)
{
    $('#youtube_'+video_id).html('<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/'+video_id+'&hl=en_US&fs=1&rel=0&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+video_id+'&hl=en_US&fs=1&rel=0&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>');
}

function embed_vimeo_video(video_id)
{
    $('#vimeo_'+video_id).html('<iframe src="http://player.vimeo.com/video/'+video_id+'?autoplay=1" width="560" height="340" frameborder="0"></iframe>');
}
