$(document).ready(function() {
	$("#headerDiv").fadeIn(100, function() {
		$("#quizzes").slideDown(300, function() {
			$("#quizPagination").fadeIn(100);
		});
	});
	$(".menu,#quizFooter,#qzTopNavigation,.fbConnect").fadeIn(100);
 	
 	$(".tagItDiv, .loveItDiv, .taggedItDiv")	
	 .hover( function() {
	 	if( this.className != 'lovedItDiv' &&  this.className != 'taggedItDiv' ) {
	 		var src = $(this).find("img").attr("src");
	 		$(this).find("img").attr("src", src.replace("Rest.gif","Hit.gif") );
	 	}
 	}, function() {
 		if( this.className != 'lovedItDiv' && this.className != 'taggedItDiv' ) {
 			var src = $(this).find("img").attr("src");
	 		$(this).find("img").attr("src", src.replace("Hit.gif","Rest.gif") );
	 	}
 	})
 	.click( function() {
 		if( this.className == 'tagItDiv' || this.className == 'taggedItDiv') {
 		 	ratings_urchin_path = $(this).find(":hidden").val().split("|")[0].split(" ").join("_");
			asset_tag_click({
	          id:$(this).find(":hidden").val()
	        });
			//asset_tag_click( $(this).find(":hidden").val() );
 		} else if( this.className != 'lovedItDiv' ) {//|| this.className != 'loveItDiv'
 			ratings_urchin_path = $(this).find(":hidden").val().split("|")[1].split(" ").join("_"); 
 			asset_rate_click({
        		ratingtag:this,
        		id:$(this).find(":hidden").val().split("|")[0],
        		path:$(this).find(":hidden").val().split("|")[1]
     		});
 		}
 	});
 });
 
 function asset_tag_click(assetInfo) {
	// User should be allowed to rate if they are either logged
	// in through unified reg or through Facebook
  if(isUserLoggedIn) {
    // reset checkboxes
    $("#tagCheckboxes :checkbox").attr("disabled",false).attr("checked",false);
    $("#tagCheckboxes span").css("color","black");
    $("#tagCheckboxes + div").hide();
   	// QUIZ PATH | # of tags | user tags (seperated by comma) | tag counts (tag=value seperated by comma) | QUIZ ID
   	if( assetInfo != null ) {
    	asset_to_rate_after_login = assetInfo;//allows us to reuse old js
    	assetInfo = (typeof(assetInfo.id) == "string")?assetInfo.id:assetInfo;
    	if(assetInfo.split('|')[2] != "") {
			// reset list and graph
	      	$("#userTags div, #tagGraph > div, #noTagsDesc").hide();
	      	$("#userTags span,#tagGraph span").text("");
	      	$("#tagGraph .bargraph").css("width","0");
			var qId = assetInfo.split("|")[4];//quiz ID
			//display tags already selected by user 
	      	$(assetInfo.split('|')[2].split(",")).each(function(i){
	        	$("#userTag_"+i).text($("#tag_checkbox_"+this+" + span").text()).parent().show();
	      	});
	      	if( assetInfo.split('|')[1] == 0 ) {
	        	$("#noTagsDesc").show();
	      	} else {
	        	$(assetInfo.split('|')[3].split(',')).slice().each(function(i){
		          	if(i<5 && $("#tag_checkbox_"+this.split("=")[0]+" + span").text() !="") {
		            	$("#tagGraphbar_"+i).css("width",(230*this.split("=")[1])/$("#tag_btn_"+qId+" :hidden").val().split("|")[1]+"px");
		            	$("#tagGraphname_"+i).text($("#tag_checkbox_"+this.split("=")[0]+" + span").text()).parent().show();
		          	}
	        	});
	      	}  
	      	$($("#tag_btn_"+qId+" :hidden").val().split("|")[2].split(",")).each(function(){
		        $("#tag_checkbox_"+this).click().triggerHandler("click");
		        $("#tag_checkbox_"+this).attr("disabled",true).next().css("color","gray").css("cursor","default");
	    	});
	      	var hOffset1 = 16*Math.min(5,Math.max(1,$("#tag_btn_"+qId+" :hidden").val().split("|")[2].split(',').length-3));
	      	var hOffset2 = 16*Math.min(5,$("#tag_btn_"+qId+" :hidden").val().split("|")[3].split(',').length );
	      	var hOffset = hOffset1 + hOffset2;
	      	$("#viewTagsModalBGSpacer").css("height",140+hOffset+"px");
			$(".viewTagsModal").css("height",165+hOffset+"px");
			$("#tagGraph").css("height", (hOffset2-10)+"px");
			if($.browser.msie) {
	      		$(".viewTagsModal .ui-dialog-buttonpane").css("bottom","-7px");
	      	}
			$(".viewTagsModal .ui-dialog-buttonpane").css("height","44px");
	      	$("#viewTagsModal").dialog("open");
		} else
			$("#addTagsModal").dialog("open");
		}
  	} else {
    	asset_to_rate_after_login = assetInfo;
    	$("#registrationModal #regMode").val("tag");
    	$("#registrationModal").dialog("open");
  	}
}

function asset_rate_click(assetInfo) {
	// User should be allowed to rate if they are either logged
	// in through unified reg or through Facebook
	if(isUserLoggedIn) {
    //ckTrk("/clicks/rating"+ratings_urchin_path+"/Quizzes");
		if(assetInfo.path != "")
      		submitRating(assetInfo);
		if(assetInfo.ratingtag != null) {
			var src = $(assetInfo.ratingtag).find("img").attr("src");
	 		$(assetInfo.ratingtag).find("img").attr("src", src.replace("Rest.gif","Hit.gif") );
			assetInfo.ratingtag.className = "lovedItDiv";
			var rating = $(assetInfo.ratingtag).find("span").html();
			$(assetInfo.ratingtag).find("span").html( parseInt(rating)+1 );
		}
    	asset_to_rate_after_login = {};
	} else {
		asset_to_rate_after_login = assetInfo;
		$("#registrationModal #regMode").val("rate");
		$("#registrationModal").dialog("open");
	}
}