/*!
 * jQuery JavaScript Library v1.4.4
 * http://jquery.com/
 *
 * Copyright 2010, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 * Copyright 2010, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: Thu Nov 11 19:04:53 2010 -0500
 */
 /*
 *
 * 表格隔行换色
 *
 *
 */
$(document).ready(function(){
							  // Your code here...
								$(".table tbody tr:odd").addClass("color"); 
								$(".table tbody tr").hover(
														   function () {
																$(this).addClass("chose");
															},
															function () {
																$(this).removeClass("chose");
															}
															);
								$('#show_course').toggle(
														function () {
															$('#select_box').slideDown('fast');
														},
														function () {
															$('#select_box').slideUp('fast');
														}
														);
								clear_message();
							}); 

/*
*
*
*
* 全选复选框
*
*
*
*/
$(function() {
				$("#checkedAll").click(function() {
					if ($(this).attr("checked")) {
						$("input[type=checkbox]").each(function() {
							$(this).attr("checked", true);
						});
					} else {
						$("input[type=checkbox]").each(function() {
							$(this).attr("checked", false);
						});
					}
				});
			});

/*
*
*
*
* 批量删除时，判断至少有一个元素被选
*
*
*
*/
$(function() {
					$("#deleteAll").bind("click", function(){
						//判断是否有元素被选
						var falg = 0; 
						$("input[name='items[]']:checkbox").each(function(){
							if ($(this).attr("checked")) {
								falg =1;
							}
						});
						if(falg){
							return true;
						} else {
							alert('请至少选择一个删除的对象');
							return false;
						}
					});
				});

/*
*
*
*
* reload_captcha_image
*
*
*
*/
function reload_captcha(){
	$.ajaxSetup ({
		cache: false //关闭AJAX相应的缓存
	});

	$.get("/auth/reload_captcha_image", function(data){
		//替换验证码图片
		$("img").replaceWith(data);
	});
}

/*
*
*
*
* 状态按钮异步刷新
*
*
*
*/
function published(obj,links,i){
	
	links1 = links+'/'+i;

	$.get(links1, function(data){
		if(i){
			$(obj).replaceWith("<a href='javascript:void(0)' onclick='published(this,\""+links+"\",0)'>启用</a>");
			message = '<div class="tips_green">启用成功</div>';
		} else {
			$(obj).replaceWith("<a href='javascript:void(0)' onclick='published(this,\""+links+"\",1)'>禁用</a>");
			message = '<div class="tips_red">禁用成功</div>';
		}
		if($('.tips_green')[0]==undefined&&$('.tips_red')[0]==undefined){
			$('div.page_top').after(message);
		} else {
			$('.tips_green').replaceWith(message); 
			$('.tips_red').replaceWith(message); 
		}
	});	
	clear_message();
}

/*
*
*
*
* 课程显示按钮异步刷新
*
*
*
*/
function show(obj,links,i){
	
	links1 = links+'/'+i;

	$.get(links1, function(data){
		if(i){
			$(obj).replaceWith("<a href='javascript:void(0)' onclick='show(this,\""+links+"\",0)'>显示</a>");
			message = '<div class="tips_green">显示成功</div>';
		} else {
			$(obj).replaceWith("<a href='javascript:void(0)' onclick='show(this,\""+links+"\",1)'>隐藏</a>");
			message = '<div class="tips_red">隐藏成功</div>';
		}
		if($('.tips_green')[0]==undefined&&$('.tips_red')[0]==undefined){
			$('div.page_top').after(message);
		} else {
			$('.tips_green').replaceWith(message); 
			$('.tips_red').replaceWith(message); 
		}
	});
	clear_message();
}

/*
*
*
*
* 添加新的信息项
*
*
*
*/
function add_detail(){
	i=1;
	num = 0;
	$('table.treeTable tr.detail').each(function() {
												 name = $($(this).children().children()[0]).attr("name");
												 name_ary = name.split("_");
												 if(num < parseInt(name_ary[2]))
												 num = parseInt(name_ary[2]);
												 i = parseInt(num)+1; 
												 });
											
	if($('input#detail').attr('value')){
		$('input#detail').attr('value',$('input#detail').attr('value')+','+i);
	} else {
		$('input#detail').attr('value',i);
	}
	$('table.treeTable').append("<tr class='detail'><td><input type='text' name='detail_name_"+i+"' value='新项_"+i+"' style='width:55%;' class='textbox'/> <input name='' value=' 删除该项 ' type='button' onclick='del_detail(this,"+i+");' /></td><td><input type='text' name='detail_value_"+i+"' value='' style='width:50%;' class='textbox'/> 设为QQ类型 <input name='is_qq_"+i+"[]' type='checkbox' value='"+i+"'/></td></tr>"); 

}

/*
*
*
*
* 删除信息项
*
*
*
*/
function del_detail(obj,i){
	values = $('input#detail').attr('value');
	values_ary = values.split(",");
	new_ary = Array();
	for(var n=0;n<values_ary.length;n++) {
        if(values_ary[n]!=i)
			new_ary.push(values_ary[n]);
    }
	new_values = new_ary.join();
	$('input#detail').attr('value',new_values);
	$(obj).parent().parent().remove();
}

/*
*
*
*
* 显示所选图片
*
*
*
*/
function showpic(pic,title)
{
	$('#bigpic').attr('src',pic);
	$('#pictitle').replaceWith('<p id="pictitle">'+title+'</p>');
}

/*
*
*
*
* 删除信息项
*
*
*
*/
function change(whichLink) {
	var links = $('#carousel_ul a');
	for (var i = 0; i < links.length; i++) {
		links[i].className="off";
	}
	whichLink.className="on";
}

/*
*
*
*
* 提交按钮绑定事件防止重复提交
*
*
*
*/
$(function() {
				$(":submit[name='save']").bind("click",function() {
					//隐藏提交按钮，防止用户重复提交
					$(":submit[name='save']").hide();
					//添加灰色按钮，模拟按钮不可用
					$(":submit[name='save']").before('<input type="button" name="ing" value="提交中..." class="button gary small" disabled="disabled" />');
				});
			});

$(function(){
				$('.delete_button').click(function(e){
					 var a = $(this);					
					 jConfirm('确定要删除吗？', '提示', function(r){
						 if( r ){
						 	window.location.href = a.attr('href');
						 }
					 });
					 return false;
				});		   
		   });

function clear_message()
{
	setTimeout("$('.tips_green').remove();$('.tips_red').remove();",10000);
}

function popo( target, val ){
			$.each( target, function( n, el ){
				if( val ){
					$(el).find('.popo_inner2').text( val );
				}else{
					el = $(el);
					$('<span class="popo_inner"><span class="popo_inner2">' + el.attr('title') + '</span></span>').prependTo( el );
					el.removeAttr('title');

					if( el.attr('tagName').toLowerCase() === 'a' ){
						el.click(function(){
							if( el.attr('href') === '#' )
								return false;
						});
					}else
					{
						el.css('cursor', 'default');
					}

					el.hover(
						function(){
							$(this).find('.popo_inner').css('display', 'block');
						},
						function(){
							$(this).find('.popo_inner').css('display', 'none');
						}
					);
				}
			});
		}
		
jQuery.fn.extend({
	popo: function( val ) {
		return $(this)
				.live({
				mouseenter:function(event){
					$(this).find('.popo_inner').css('display', 'block');
				},

				mouseleave:function(event){
					$(this).find('.popo_inner').css('display', 'none');
				}
			})
			.each(function(n, span){
			var el = $(span);

			if( val ){
				el.find('.popo_inner2').text( val );
			}else{
				el.prepend( $('<span class="popo_inner"><span class="popo_inner2">' + el.attr('title') + '</span></span>') ).removeAttr('title');

				if( el.attr('tagName').toLowerCase() === 'a' ){
					el.click(function(){
						if( el.attr('href') === '#' )
							return false;
					});
				}else
				{
					el.css('cursor', 'default');
				}
			}
		});
	}
});


