var commonFn = {};
var showtv_index = 1;
var t;

(function($){
    $(document).ready(function(){
        $(".area .text a").mouseover(function(){
	   var cur_name = $(this).attr('class').split('-');
	   $(this).parent().parent().children().find('.item-pics').hide(); 
	   $(this).parent().parent().children().find('.pic-item-'+cur_name[1]).show(); 
	});

	showFriendBox($("#final-to-f"));
	$("#shareicon").mouseover( function() { $("#sharefull").show(); } );
	$("#shareicon-close").click( function() { $("#sharefull").hide(); } );
        commonFn.stopDefault = function(e){
	if (e)
	    e.preventDefault();
	else 
	    window.event.returnValue = false;
	};

	var info_height = $("#content .c-l .info").height();
	if( info_height > 18 ){
		$("#content .c-l .info").css("text-align","left");
	}
	 
	//my book hover
	$("#pagePic .picList li").hover(function(){
	    if (t) {
		window.clearTimeout(t);
	    }
	    var that = this;
	    t = window.setTimeout(function () {
		var detailFrame = $('.detail-frame',that).html();
		var thisX = $('img', that).offset().left;
		var thisY = $('img', that).offset().top;
		if ($(that).width() == 133) {
		    var ml = 143;
		} else {
		    var ml = $('img', that).width() + 10;
		}
        	$('body').append('<div class="detail-tip shadow-right"></div>');
		$('.detail-tip').html(detailFrame).append('<span class="arrow-left"></span>').css('left', thisX + ml).css('top', thisY);
	    }, 200);
        },
	function () {
	    if (t) {
		window.clearTimeout(t);
	    }
	    $('.detail-tip').remove();
	}
	);

	$("li","#menu-top-block").each(function(i){
	    var_image=$(this).css("background-image");	
	    var_repeat=$(this).css("background-repeat");
	    var_position=$(this).css("background-position");
	    $(this).hover(function(){
		$(this).css({"background":"#F5F5F5","color":"#fff"});
		},function(){
		    $(this).css({"background-image":var_image,"background-repeat":var_repeat,"background-position":var_position,"color":"#eeeeee"});
	    });
	});

	var_bg_menu_keyword=$("#txt-menu-keyword").css("background-image");
	$("#txt-menu-keyword").click(function(){
	    $(this).css("background","#fff");
	});
	$("#txt-menu-keyword").blur(function(){
	    $(this).css("background-image",var_bg_menu_keyword);
	});
	$("li:last","#menu-bottom-block").css("background","none");
	/* 
	 * For select start
	 * */	
	$("div.select").each(function(){
	    var_select=this;
	    var_inselect=0;
	    $("li",this).hover(function(){
		$(this).css({"background":"#fff","border":"solid 1px #ccc"});
		$(".value",this).css({"color":"#000","font-weight":"normal"});
		},function(){
		    $(this).css({"background":"#F5F5F5","border":"none"});
		    $(".value",this).css({"color":"#000","font-weight":"normal"});
	    });
	    $("li",this).click(function(){
		$("ul",var_select).hide();
		$(".text",var_select).removeClass("drop-down");
		$(".text",var_select).html($(".value",this).html());
		/*location.href=$(".key",this).html();*/
	    });
	    $("ul",this).css("display","none");
		$(".text",this).click(function(){
		//if($("ul",var_select).css("display")=="block"){
		    //$("ul",var_select).slideUp("slowly");
		    //$(this).removeClass("drop-down");
		//}else{
		    $("ul",var_select).slideDown("slowly");
		    $(this).addClass("drop-down");
		//}
	    });
	    $(".last-block",this).hover(function(){var_inselect=1;},function(){var_inselect=0;});
		$(document).click(function(){
		    if(var_inselect==0){
			$("ul",var_select).slideUp("slowly");
			$(".text",var_select).removeClass("drop-down");
		    }
		});
	    });
	});

	/*my book
	$("div.book-sort").each(function(){
	    var_select=this;
	    var_inselect=0;
	    $("li",this).hover(function(){
		$(".value",this).css({"color":"#992927","font-weight":"normal"});
		},function(){
		    $(this).css({"background":"#F5F5F5","border":"none"});
		    $(".value",this).css({"color":"#992927","font-weight":"normal"});
	    });
	    $("li",this).click(function(){
		$("ul.drop-down-list",var_select).hide();
		$(".text",var_select).removeClass("drop-down");
		$(".text",var_select).html($(".value",this).html());

	    });
	    $("ul.drop-down-list",this).css("display","none");
		$(".bookcagetory",this).click(function(){
		if($("ul.drop-down-list",var_select).css("display")=="block"){
		    $("ul.drop-down-list",var_select).slideUp("slowly");
		    $(this).removeClass("drop-down");
		}else{
		    $("ul.drop-down-list",var_select).slideDown("slowly");
		    $(this).addClass("drop-down");
		}
	    });
	    $(".last-block",this).hover(function(){var_inselect=1;},function(){var_inselect=0;});
		$(document).click(function(){
		    if(var_inselect==0){
			$("ul",var_select).slideUp("slowly");
			$(".text",var_select).removeClass("drop-down");
		    }
		});
	    });
	*/
    /*
	$("#btnSendFriend").click(function(){
	 if( checkfriendform() )
	 {
	     var param = "prenom=" + $("#prenom").val();
	         param = param + "&email=" + $("#email").val();
		 param = param + "&commentaire=" + encodeURIComponent($("#commentaire").val());
		 param = param + "&email_ami1=" + $("#email_ami1").val();
		 param = param + "&email_ami2=" + $("#email_ami2").val();
		 param = param + "&email_ami3=" + $("#email_ami3").val();
		 param = param + "&url=" + $("#url").val();

	     $.ajax({ 
      		type: "POST", 
      		url: "/action/send_to_a_friend", 
      		data: param,
      		success: function( responseText ){
          		send_friend_finish( responseText );
      		}
   	    });
 	}
    });

    $("#btnCancelSend").click(function(){
	$("#prenom").attr("value", "");
	$("#email").attr("value", "");
	$("#commentaire").attr("value", "");
	$("#email_ami1").attr("value", "");
	$("#email_ami2").attr("value", "");
	$("#email_ami3").attr("value", "");
	$.unblockUI();
    });
    */

})(jQuery);

function send_friend_finish( responseText )
{
    var res = eval('('+responseText+')');

    if(res.res == "OK")
    {
	alert(msg_send_to_a_friend8);
	$("#prenom").attr("value", "");
	$("#email").attr("value", "");
	$("#commentaire").attr("value", "");
	$("#email_ami1").attr("value", "");
	$("#email_ami2").attr("value", "");
	$("#email_ami3").attr("value", "");
	$("#url").attr("value", "");
	$.unblockUI();
    }
    else
    {
	var msg = msg_send_to_a_friend9 + "\n";
	res.mail_echec.each(function(mail) {
	    msg = msg + mail + "\n";
	});
	res.mail_echec.count
	    alert(msg);
    }
}

function checkfriendform()
{
    if( $("#prenom123").val() == "" )
    {
	alert(msg_send_to_a_friend1);
	$("#prenom123").focus();
	return false;
    }
    if( $("#email123").val() == "" )
    {
	alert(msg_send_to_a_friend2);
	$("#email").focus();
	return false;
    }
    if( $("#email_ami1").val() == "" )
    {
	alert(msg_send_to_a_friend3);
	$("#email_ami1").focus();
	return false;
    }
    if( !checkMail( $("#email").val() ) )
    {
        alert(msg_send_to_a_friend4);
	$("#email").focus();
	return false;
    }
    if( !checkMail( $("#email_ami1").val() ) )
    {
        alert(msg_send_to_a_friend5);
	$("#email_ami1").focus();
        return false;
    }
    if( $("#email_ami2").val() != "" && !checkMail( $("#email_ami2").val() ) )
    {
         alert(msg_send_to_a_friend6);
	 $("#email_ami2").focus();
         return false;
    }
    if( $("#email_ami3").val() != "" && !checkMail( $("#email_ami3").val() ) )
    {
         alert(msg_send_to_a_friend7);
	 $("#email_ami3").focus();
         return false;
    }
    return true;
}

function checkMail(mail)
{
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(mail))
        return true;
    else
        return false;
}

function showFriendBox(o){
    o.click(function(){
	var w=document.documentElement.clientWidth;
	var h=document.documentElement.clientHeight;
	$.blockUI({
	    message:$("#light-box-friend"),css:{cursor:"default",top:(h-390)/2+'px',left:(w-550)/2+'px',width:'550px'}
	});
    });
}

//add favorite
function addFavorite(){
    var url = window.location;
    var titre = document.title;
    if (window.sidebar){
        window.sidebar.addPanel(titre,url,'');
    }
    else{
	window.external.AddFavorite(url,titre);
    }
    return false;
}

