$(document).ready(function(){
	$("#addToFavoriteBt").click(function(){
	//for article
		var obj = $(this);
    	//obj.unbind('click');
		var operation = $(this).attr("operation");

    	$.post(gBaseUrl + 'ajaxhandler/AddToFavorite.html', {objId:obj.attr('oid'),objType:obj.attr('otype'),operation:operation,touser_id:obj.attr('otouserid')},
    			function(data){
    				eval('var ret = ' + data);
    				if(ret.ret == 0){
    					$("#addToFavorite").html(ret.newNum);

						if(operation == 1){
							obj.attr("operation","0");
							$("#addToFavorite").attr("operation","0");

							$("#addToFavorite").css("color","#fdc0c3");

							obj.html("Remove from Favourites");
							$("#addToFavorite").parent().parent().children("p").html("+Faved");
						}
						else{
							obj.attr("operation","1");
							$("#addToFavorite").attr("operation","1");

							$("#addToFavorite").css("color","");

							obj.html("Add To Favourites");
							$("#addToFavorite").parent().parent().children("p").html("+Faves");
						}
    				}
    			});
	});
    $("#addToFavorite").click(function(){
	//for article
    	var obj = $(this);
    	//obj.unbind('click');
		var operation = obj.attr("operation");

    	$.post(gBaseUrl + 'ajaxhandler/AddToFavorite.html', {objId:obj.attr('oid'),objType:obj.attr('otype'),operation:operation,touser_id:obj.attr('otouserid')},
    			function(data){
    				eval('var ret = ' + data);
    				if(ret.ret == 0){
    					obj.html(ret.newNum);
						if(operation == 1){
							obj.attr("operation","0");
							$("#addToFavoriteBt").attr("operation","0");

							obj.css("color","#fdc0c3");

							$("#addToFavoriteBt").html("Remove from Favourites");
							$("#addToFavorite").parent().parent().children("p").html("+Faved");
						}
						else{
							obj.attr("operation","1");
							$("#addToFavoriteBt").attr("operation","1");

							obj.css("color","");

							$("#addToFavoriteBt").html("Add To Favourites");
							$("#addToFavorite").parent().parent().children("p").html("+Faves");
						}
    				}
    			});
    });

	$(".artNumber").click(function(){
//for collaboratin and news and discussion
    	var obj = $(this);
    	//obj.unbind('click');
		if(obj.attr('isLoggedIn') == 0 || obj.attr('role_id') < 2)
		{
		
			alert("Please login or register to add this news to favourites.");
			return;
		}		

		var operation = obj.attr('operation');
		var otype = obj.attr('otype');
		
		if(otype == 4 && obj.attr('status') == "0"){//for discussions 
			return;
		}
				
    	$.post(gBaseUrl + 'ajaxhandler/AddToFavorite.html', {objId:obj.attr('oid'), objType:otype, operation:operation,touser_id:obj.attr('otouserid')},
    			function(data){
    				eval('var ret = ' + data);

    				if(ret.ret == 0){
						if(operation == "0"){

							obj.attr("operation",1);
							$("#addToFaveNew").attr("operation","1");
							obj.css("color","");
							
							if( otype == 2 ){ //for collaboration
								obj.html(ret.newNum + '<br /><span>+Apply</span>');
								$("#addToFaveNew").html("Apply to Collaboration");
							}	
							else{
								obj.html(ret.newNum + '<br /><span>+Faves</span>');
								$("#addToFaveNew").html("Add To Favourites");
							}	
						}
						else{

							obj.attr("operation",0);
							$("#addToFaveNew").attr("operation","0");
							obj.css("color","#fdc0c3");		
							
							if(otype == 2 ){ //for collaboration
								obj.html(ret.newNum + '<br /><span>+Applied</span>');
								$("#addToFaveNew").html("Withdraw Application");
							}	
							else{
								obj.html(ret.newNum + '<br /><span>+Faved</span>');
								$("#addToFaveNew").html("Remove from Favourites");
							}	
						}
    				}
    			});
    });


	$("#addToFaveNew").click(function(){
//for collaboratin and news and discussion
		var obj = $(this);
		var operation = obj.attr('operation');

    	//obj.unbind('click');
    	$.post(gBaseUrl + 'ajaxhandler/AddToFavorite.html', {objId:obj.attr('oid'),objType:obj.attr('otype'),operation:operation,touser_id:obj.attr('otouserid')},
    			function(data){
    				eval('var ret = ' + data);

    				if(ret.ret == 0){
						if(operation == "1"){

							obj.attr("operation","0");
							$(".artNumber").attr("operation","0");
							if(obj.attr('otype') == 2 ) //for collaboration
								$(".floatRight.artNumber").html(ret.newNum + '<br /><span>+Applied</span>');
							else
								$(".floatRight.artNumber").html(ret.newNum + '<br /><span>+Faved</span>');
							$(".floatRight.artNumber").css("color","#fdc0c3");
							if(obj.attr('otype') == 2 ) //for collaboration
								$("#addToFaveNew").html("WithDraw Application");
							else
								$("#addToFaveNew").html("Remove from Favourites");
						}else{
							obj.attr("operation","1");
							$(".artNumber").attr("operation","1");
							if(obj.attr('otype') == 2 ) //for collaboration
								$(".floatRight.artNumber").html(ret.newNum + '<br /><span>+Apply</span>');
							else
								$(".floatRight.artNumber").html(ret.newNum + '<br /><span>+Faves</span>');
							$(".floatRight.artNumber").css("color","");
							if(obj.attr('otype') == 2 ) //for collaboration
								$("#addToFaveNew").html("Apply To Application");
							else
								$("#addToFaveNew").html("Add To Favourites");

						}
    				}
    			});
    });



	//i hope this is not required can be coommneted
	$("#rsvp").click(function(){$("#addToFavorite").trigger("click");});

    $(".deleteThis").click(function(){
    	if(confirm("Are you sure delete it?")){
    		var  e = $(this);
    		$.post(gBaseUrl+"article/delArticle.html",{article_id:$(this).attr("value")},function(data){
    			if(data == "1")
    				e.parents("ul").remove();
    		});
    	}
    });

	$(".closebox img").click(function(){
//for removing fav design on /favourite/index page
		var obj = $(this);
    	obj.unbind('click');
    	$.post('/ajaxhandler/AddToFavorite.html', {objId:obj.attr('oid'),objType:obj.attr('otype'),operation:"0",touser_id:obj.attr('otouserid')},
    			function(data){
    				eval('var ret = ' + data);
    				if(ret.ret == 0){

						var parentLi = obj.parent().parent().parent();
						var i=0;
						var childLis = $("#favDesignUL").children();

						for(i = 0; i < childLis.length; i++)
						{
							if((i+5)%5) {
								$(childLis[i]).removeClass('marLeft35px');
							}
							if((i+5)>9){
								$(childLis[i]).removeClass('marTop35px');
							}
						}

						var totalFaves=document.getElementById("totalDesignFaves").innerHTML;
						document.getElementById("totalDesignFaves").innerHTML = parseInt(totalFaves,10)-1;
						parentLi.hide("slow",function(){

							parentLi.remove()
							var childLis = $("#favDesignUL").children();

							for(i = 0; i < childLis.length; i++)
							{
								if((i+5)%5) {
									$(childLis[i]).addClass('marLeft35px');
								}
								if((i+5)>9){
									$(childLis[i]).addClass('marTop35px');
								}

							}

							});

					}
    			});
    });

	$("#sendMail").click(function(){

			var from = $("#txtFrom").val();
			var to = $("#txtTo").val();
			var randomStr = $("#randomStr").val();
			var type = $(this).attr("otype");

			$.post(gBaseUrl + 'ajaxhandler/sendMail.html', {from:from,to:to,type:type,randomStr:randomStr},
			function(data){

				eval('var ret = ' + data);
				if(ret.ret == 0){

					 $("#txtTo").val("");
					 $("#txtFrom").val("");

					 var box = $('<div class="messageBox">\
									<div class="messageTop">\
									<img src="'+gBaseUrl+'public/images/mesCloseBtn.gif" width="18" height="18" alt="Close" id="closeAlert"/>\
									</div>\
									<div class="messageCon">\
										Your email was sent sucessfully.\
									</div>\
								 </div>');

					$("#container").prepend(box);
						rate = getCenterPosition([596,73]);
					$(".messageBox").css({left:rate[1]});
					$("#closeAlert").click(function(){
						$(this).parents(".messageBox").hide();
					});

				}
			});
	});
});
	function showtext(obj)
	{
		var childDiv = $(obj).children()[1];
		var width = $(obj).width() -40;	
		
		var offset=$(obj).offset();
		var x = offset.left +20;
		var y = offset.top  - 20;
		

		$(childDiv).css("left", x);
		$(childDiv).css("top", y);
		$(childDiv).css("width", width);
		$(childDiv).css("opacity", 0.8);
		$(childDiv).show();
		//$(childDiv).animate({opacity: 1}, 200);
	}
	function removetext(obj)
	{
		var childDiv = $(obj).children()[1];
		$(childDiv).hide();
	}