jQuery.noConflict();
document.observe('dom:loaded', function() {
    //a[rel],img[rel]
  $$('img.guide_icon').each(function(element) {
      show_tip(element,element.lang,element.name);
  });
  $$('a[rel]').each(function(element) {
      show_tip(element,element.rel,element.name);
  });

  $$('a.item_info').each(function(element) {

  });

});

function show_tip(element,url,title){
  new Tip(element, {
  ajax: {
    url:  url,
    options: {
      onComplete: function() {
          //alert('ajax content loaded');
      }
    }
  },
  title: title,
				border: 3,
				radius: 3,
				style: 'protoblue'
    });
}
jQuery(document).ready(function() {
  jQuery("abbr.timeago").timeago();
});

function signup(){
    jQuery.fn.colorbox({width:"550px", height:"500px", iframe:true,href:'/signup',onClosed:function(){  }});
}

function login_top(){
    var vals = jQuery("#lform").serialize();
    jQuery.post('/ajax/login',vals,function(data){
        if(data.status){
            window.location.href = '/';
        } else {
            window.location.href = '/login/index/'+data.msg;
        }
    },'json');
    return false;
}


function login(){
    var vals = jQuery("#form").serialize();
    jQuery.post('/ajax/login',vals,function(data){
        if(data.status){
            jQuery("#form_box").hide();
            jQuery("#status").text(data.msg);
            window.location.href = '/';
        } else {
            jQuery("#status").text(data.msg);
        }
    },'json');
    return false;
}

function leave_comment(elem,type,postid){
    jQuery("#comment_status").text("Please wait ...");
    jQuery("#comment_form").hide();
    jQuery("#comment_status").fadeIn(800);
    var comment = jQuery("#"+elem).val();
    jQuery.post('/ajax/comment',{type:type,postid:postid,comment:comment},function(data){
        if(data.status){
            jQuery("#comment_form").text(comment);
            jQuery("#comment_form").fadeIn(800);
            jQuery("#comment_status").hide();
        } else {
            jQuery("#comment_status").text(data.msg);
            jQuery("#comment_form").fadeIn(800);
            jQuery("#comment_status").fadeIn(800);
        }
    },'json');
}

function leave_match_comment(match_id){
    jQuery("#comment_status").text("Please wait ...");
    jQuery("#comment_form").hide();
    jQuery("#comment_status").fadeIn(800);
    var comment = jQuery("#comment").val();
    jQuery.post('/ajax/match_comment',{match_id:match_id,comment:comment},function(data){
        if(data.status){
            jQuery("#comment_form").text(comment);
            jQuery("#comment_form").fadeIn(800);
            jQuery("#comment_status").hide();
        } else {
            jQuery("#comment_status").text(data.msg);
            jQuery("#comment_form").fadeIn(800);
            jQuery("#comment_status").fadeIn(800);
        }
    },'json');
}
function leave_end_match_comment(match_id){
    jQuery("#comment_status").text("Please wait ...");
    jQuery("#comment_form").hide();
    jQuery("#comment_status").fadeIn(800);
    var comment = jQuery("#comment").val();
    jQuery.post('/ajax/match_details_comment',{match_id:match_id,comment:comment},function(data){
        if(data.status){
            jQuery("#comment_form").text(comment);
            jQuery("#comment_form").fadeIn(800);
            jQuery("#comment_status").hide();
        } else {
            jQuery("#comment_status").text(data.msg);
            jQuery("#comment_form").fadeIn(800);
            jQuery("#comment_status").fadeIn(800);
        }
    },'json');
}
function show_leave_comment(){
    jQuery("#comment_status").hide();
    jQuery("#comment_form").fadeIn(800);
    jQuery("#comment").focus();
}
var follow_hide;
function follow(userid){
    clearTimeout(follow_hide);
    jQuery("#follow_btn").hide();
    jQuery("#follow_status").show();
    jQuery("#follow_loader").show();
    jQuery.post('/ajax/follow',{userid:userid},function(data){
        if(data.status){
            jQuery("#unfollow_btn").show();
        } else {
            jQuery("#follow_btn").show();
        }
        jQuery("#follow_status").text(data.msg);
        jQuery("#follow_loader").hide();
        follow_hide = setTimeout(function() {
            jQuery('#follow_status').fadeOut('fast');
        }, 2000);
    },'json');
}

function unfollow(userid){
    clearTimeout(follow_hide);
    jQuery("#unfollow_btn").hide();
    jQuery("#follow_status").show();
    jQuery("#follow_loader").show();
    jQuery.post('/ajax/unfollow',{userid:userid},function(data){
        if(data.status){
            jQuery("#follow_btn").show();
        } else {
            jQuery("#unfollow_btn").show();
        }
        jQuery("#follow_status").text(data.msg);
        jQuery("#follow_loader").hide();
        follow_hide = setTimeout(function() {
            jQuery('#follow_status').fadeOut('fast');
        }, 2000);
    },'json');
}

function submit_clan(){
    var vals = jQuery("#c_form").serialize();
    jQuery("#diss_status").fadeIn(500);
    jQuery("#diss_status").attr('class','status_normal');
    jQuery("#diss_status").text('Please wait...');
    jQuery.post('/ajax/create_clan',vals,function(data){
        jQuery("#diss_status").text(data.msg);
        if(data.status){
            jQuery("#form_box").hide();
            jQuery("#diss_status").attr('class','status_success');
            jQuery("#diss_status").css('padding-left','0px');
            jQuery("#diss_status").css('text-align','center');
        } else jQuery("#diss_status").attr('class','status_error');
    },'json');
    return false;
}

function submit_clan_settings(){
    var vals = jQuery("#g_form").serialize();
    jQuery("#form_status").fadeIn(500);
    jQuery("#form_status").attr('class','status_normal');
    jQuery("#form_status").text('Please wait...');
    jQuery.post('/ajax/update_clan',vals,function(data){
        jQuery("#form_status").text(data.msg);
        if(data.status){
            jQuery("#form_box").hide();
            jQuery("#form_status").attr('class','status_success');
            jQuery("#form_status").css('padding-left','0px');
            jQuery("#form_status").css('text-align','center');
        } else jQuery("#form_status").attr('class','status_error');
    },'json');
    return false;
}
function submit_clan_invite(){
    var vals = jQuery("#g_form").serialize();
    jQuery("#form_status").fadeIn(500);
    jQuery("#form_status").attr('class','status_normal');
    jQuery("#form_status").text('Please wait...');
    jQuery.post('/ajax/clan_invite',vals,function(data){
        jQuery("#form_status").text(data.msg);
        if(data.status){
            jQuery("#form_box").hide();
            jQuery("#form_status").attr('class','status_success');
            jQuery("#form_status").css('padding-left','0px');
            jQuery("#form_status").css('text-align','center');
        } else jQuery("#form_status").attr('class','status_error');
    },'json');
    return false;
}
function show_form_invite(){
    jQuery("#form_invite").hide();
    jQuery("#form_box").fadeIn(500);
}



function leave_message(user_id){
    jQuery("#comment_status").text("Please wait ...");
    jQuery("#comment_form").hide();
    jQuery("#comment_status").fadeIn(800);
    var message = jQuery("#message").val();
    jQuery.post('/ajax/leave_message',{user_id:user_id,message:message},function(data){
        if(data.status){
            jQuery("#comment_status").text(data.msg);
            jQuery("#message").val('');
        } else {
            jQuery("#comment_status").text(data.msg);
            jQuery("#comment_form").fadeIn(800);
            jQuery("#comment_status").fadeIn(800);
        }
    },'json');
}

function vote_poll(diss_id,poll_id){
    jQuery("#poll_status").text('Casting vote...please wait...');
    jQuery.post('/ajax/vote_poll',{diss_id:diss_id,poll_id:poll_id},function (data){
        jQuery("#poll_status").text(data.msg);
    },'json');
}

function show_book_tip(content){
    new Tip('bookmark_status',content, {
        title : 'Bookmark',
        hook: {tip: 'rightMiddle' },
        offset: { x: 0, y: 6 },
        border: 3,
        radius: 3,
        hideOn: false,
        showOn: false,
        closeButton: true,
        style: 'protoblue'
    });
}

function bookmark(id,type){
    show_book_tip('please wait...');
    $('bookmark_status').prototip.show();
    //jQuery("#bookmark_status").show();
    //jQuery("#bookmark_status").text("please wait...");
    jQuery.post('/ajax/bookmark',{id:id,type:type},function(data){
        //jQuery("#bookmark_status").text(data.msg);
        $('bookmark_status').prototip.remove();
        show_book_tip(data.msg);
        $('bookmark_status').prototip.show();
        if(data.status){
            jQuery("#a_bookmark_on").hide();
            jQuery("#a_bookmark_off").show();
        }
    },'json');
}

function unbookmark(id,type){
    show_book_tip('please wait...');
    $('bookmark_status').prototip.show();
    jQuery.post('/ajax/unbookmark',{id:id,type:type},function(data){
        $('bookmark_status').prototip.remove();
        show_book_tip(data.msg);
        $('bookmark_status').prototip.show();
        if(data.status){
            jQuery("#a_bookmark_on").show();
            jQuery("#a_bookmark_off").hide();
        }
    },'json');
}
function challenge(clan_id){
    jQuery.fn.colorbox({width:"550px", height:"300px", iframe:true,href:'/clan/invite_match/'+clan_id,onClosed:function(){  }});
}
function delete_clan(clan_id){
    jQuery.fn.colorbox({transition:"none",width:"450px", height:"150px", iframe:true,href:'/sajax/verify_clan_remove/'+clan_id,onClosed:function(){window.location.href = window.location.href;}});
}
function remove_fav(id,type){
    jQuery.fn.colorbox({transition:"none",width:"450px", height:"150px", iframe:true,href:'/sajax/verify_fav_remove/'+id+'/'+type,onClosed:function(){window.location.href = window.location.href;}});
}
function accept_mem(id){
    jQuery.fn.colorbox({transition:"none",width:"450px", height:"150px", iframe:true,href:'/sajax/verify_accept_mem/'+id,onClosed:function(){window.location.href = window.location.href;}});
}
function delete_mem(id){
    jQuery.fn.colorbox({transition:"none",width:"450px", height:"150px", iframe:true,href:'/sajax/verify_delete_mem/'+id,onClosed:function(){window.location.href = window.location.href;}});
}
function join_clan(id){
    jQuery.fn.colorbox({transition:"none",width:"450px", height:"220px", iframe:true,href:'/sajax/verify_join_clan/'+id,onClosed:function(){  }});
}
function show_video_add(){
    jQuery.fn.colorbox({transition:"none",width:"550px", height:"250px", title:"Add a video", iframe:true,href:'/videos/add/',onClosed:function(){  }});
}
function close_pb(){
    jQuery.colorbox.close();
}
function switch_box(elem){
    jQuery(elem).parent().find("a").each(function(){
        jQuery(this).removeClass("selected");
        var related = jQuery(this).attr('related');
        jQuery("#"+related).hide();
    });
    jQuery(elem).addClass("selected");
    jQuery("#"+jQuery(elem).attr("related")).show();
}
function match_decline(id){
    var x = confirm("Are you sure you want to decline this match invitation?");
    if(x){
    jQuery.post("/ajax/match_decline",{id:id},function(data){
        if(data.status){
            window.location.href = '/account/clan';
        } else alert(data.msg);
    },'json');
    }
}
function show_masteries(id){
    jQuery.fn.colorbox({width:"890px", height:"670px", title:'Masteries page', iframe:true,href:'/guide/masteries_view/'+id,onClosed:function(){  }});
}
function show_runes(id){
    jQuery.fn.colorbox({width:"770px", height:"370px", title:'Runes page', iframe:true,href:'/guide/runes_view/'+id,onClosed:function(){  }});
}
