jQuery.noConflict();

/** ARRAY DE RATINGMANAGER **/
RatingArray = function() {
	this.items = new Array();
	this.itemsId = new Array();
};

RatingArray.prototype.addArray = function(ratingId, itemId, saveVar, ajaxUrl, ratingStatus) { 
	this.items.push(new RatingManager(ratingId, itemId, saveVar, ajaxUrl, ratingStatus));
	this.itemsId.push(ratingId);
};

RatingArray.prototype.resolveManager = function(ratingId) {
	for(var i=0; i<= this.items.length; i++) {
		if (ratingId == this.itemsId[i]) {
			return this.items[i];
		}
	}
}

/** RATINGMANAGER **/
RatingManager = function(ratingId, itemId, saveVar, ajaxUrl, ratingStatus) {
	this.ratingId = ratingId;
	this.itemId = itemId;
	this.saveVar = saveVar;
	this.ajaxUrl = ajaxUrl;
	this.ratingStatus = ratingStatus;
	this.actualRatingClass = "selected";
};

/*===================== SET MOVIE RATING =================*/
RatingManager.prototype.saveRatingMovieAjax = function(ratingValue) {
	var params = "movieId=" + this.itemId + "&value=" + ratingValue;
	var callbackSuccess = ratingArray.resolveManager(this.ratingId).setRatingItemSuccessCallback;
	var callbackFailure = ratingArray.resolveManager(this.ratingId).setRatingItemFailedCallback;
		
	var manager = this;
	var request = new Ajax.Request(
			this.ajaxUrl,
		{
			method: "post",
			parameters: params, 
			onSuccess: function(request) {
						    callbackSuccess(request, manager);
					    },
   			onFailure: function(request) {
//   							callbackFailure(request, manager);
   						}
		});
		this.actualRatingClass = "userSelected";
};

RatingManager.prototype.setRatingItemSuccessCallback = function(request, manager) {
	var json =  request.responseText.evalJSON();
	
	requestStatusOk = json.requestStatusOk;

	requestStatus = json.requestStatus;
	
	ratingValue = json.ratingValue;

	if(requestStatusOk != null && requestStatus == requestStatusOk)
		manager.colorRating(ratingValue);
	else
		manager.setRatingItemFailedCallback(request, manager);
};

RatingManager.prototype.setRatingItemFailedCallback = function(request, manager) {
	var json =  request.responseText.evalJSON();
	if (manager.ratingStatus != "") {
		document.getElementById(manager.ratingStatus).innerHTML = "Erro";
		window.setTimeout("hideDivStatus('"+manager.ratingStatus+"')", 10000);
	}
};

/*===================== INTERNAL COMPONENT FUNCIONS =================*/
RatingManager.prototype.colorRating = function(ratingValue) {
	for (var i =1; i <=5; i++) {
		var level = document.getElementById("r" + this.ratingId + "Star" + i);
		
		if (level == null)
			level = document.getElementById(this.ratingId + "Star" + i);
		
		var index = level.className.indexOf("selected");
		
		if (index < 0)
			index = level.className.indexOf("userSelected");
		
		if (index < 0)
			index = level.className.indexOf("halfSelected");
		
		if (index >= 0) {

			if (ratingValue != i) {
				var starElem = jQuery("li#" + this.ratingId + "Star" + i);
				
				if (starElem.length > 0)
					starElem.attr("className",level.className.substring(0,index));
					
				var starElemNew = jQuery("li#r" + this.ratingId + "Star" + i);
				
				if (starElemNew.length > 0)
					starElemNew.attr("className",level.className.substring(0,index));
					
			}
		} else {

			if (ratingValue == i) {
				var ratingElem = jQuery("input#" + this.ratingId + "Selected");
				
				if (ratingElem.length > 0)
					ratingElem.attr("value",ratingValue);
					
				var ratingElemNew = jQuery("input#Selected" + this.ratingId);
				
				if (ratingElemNew.length > 0)
					ratingElemNew.attr("value",ratingValue);
			}
		}
	}
};

RatingManager.prototype.ratingDeselect = function(selectNotInterested) {
	var ratingElem = document.getElementById(this.ratingId + "Selected");
	
	if (ratingElem == null)
		ratingElem = document.getElementById("Selected" + this.ratingId);
	
	var sel = ratingElem.value;
	
	if (sel == null)
		sel = document.getElementById(this.ratingId + "Selected").value;
	
	if (sel > 0 && sel != "") {
		var starElem = document.getElementById(this.ratingId + "Star" + sel);
		var starElemNew = document.getElementById("r" + this.ratingId + "Star" + sel);
		var level = (starElem == null) ? starElemNew : starElem;
		var index = level.className.indexOf("selected");
		
		if (index < 0)
			index = level.className.indexOf("userSelected");
		
		if (index < 0)
			index = level.className.indexOf("halfSelected");
		
		if (index >= 0) {
			this.actualRatingClass = level.className.substring(index, level.className.length)

		var starElems =	jQuery("li#" + this.ratingId + "Star" + sel);
		var starElemsNew =	jQuery("li#r" + this.ratingId + "Star" + sel);
		
		if (starElems.length > 0)
			starElems.attr("className",level.className.substring(0,index));
		
		if (starElemsNew.length > 0)
			starElemsNew.attr("className",level.className.substring(0,index));
		}
	}

	if (eval(selectNotInterested))
		this.notInterestedDeselect(false);
	
	defaultTitle = $("defaultTitle"+this.ratingId)
	title = $("title"+this.ratingId)
	
	if (defaultTitle && title) {
		defaultTitle.hide();
		title.show();
	}
};

RatingManager.prototype.ratingReselect = function(reselectNotInterested) {
	var ratingElem = document.getElementById(this.ratingId + "Selected");
	
	if (ratingElem == null)
		ratingElem = document.getElementById("Selected" + this.ratingId);
	
	var selRating = ratingElem.value;
	
	if (selRating > 0 && selRating != "") {
		var actRatingClass = this.actualRatingClass;
		var elems = jQuery("li#" + this.ratingId + "Star" + selRating);
		
		if (elems.length > 0) {
			elems.each(
				function(index) {
					var starClass = jQuery(this).attr("className");
					jQuery(this).attr("className",starClass + " " + actRatingClass);
				}
			);
		}
		
		var elemsNew = jQuery("li#r" + this.ratingId + "Star" + selRating);

		if (elemsNew.length > 0) {
			elemsNew.each(
				function(index) {
					var starClass = jQuery(this).attr("className");
					jQuery(this).attr("className",starClass + " " + actRatingClass);
				}
			);
		}

	}
	
	var selNotInterested = eval(jQuery("input:hidden#notInterested"+this.ratingId).attr("value"));
	
	if (!selNotInterested && this.actualRatingClass.indexOf("userSelected") < 0) {
		
		if (!selNotInterested) {
			defaultTitle = $("defaultTitle"+this.ratingId)
			title = $("title"+this.ratingId)
			
			if (defaultTitle && title) {
				defaultTitle.show();
				title.hide();
			}
		}
	}
	
	if (reselectNotInterested){
		this.notInterestedReselect(false);
	}
};

RatingManager.prototype.saveRatingMovie = function(ratingValue, urlBack) {
	if(this.ratingId != null && ratingValue != null){
		// Exibe a imagem "carregando..." ao clicar para votar
		//changeLoadingRatingNew(this.ratingId);
		try{
			//Chama o metodo de Rating.
			
			var cpCookie = readCookie('BlockbusterUserIdStoreCode');
			if (cpCookie!=null && cpCookie!='null' && cpCookie.length>0) {
				cpCookie = 'true';
			} else {
				cpCookie = 'false';
			}
			var userCookie = readCookie('BlockbusterUserIdStoreCode');
			if (userCookie==null || userCookie=='null') {
				gravaCookieUrlBack(urlBack);
			}
			setRatingReturn(cpCookie, this.ratingId, ratingValue);
		}catch(exception){}
	}
	jQuery("input:hidden#notInterested"+this.ratingId).attr("value","false");
}

RatingManager.prototype.setNotInterested = function(urlBack) {
	if(this.ratingId != null){
		// Exibe a imagem "carregando..." ao clicar.
		//changeLoadingRatingNew(this.ratingId);
		try{
			//Chama o metodo de NotInterest.
			var cpCookie = readCookie('BlockbusterUserIdStoreCode');
			if (cpCookie!=null && cpCookie!='null' && cpCookie.length>0) {
				cpCookie = 'true';
			} else {
				cpCookie = 'false';
			}
			var userCookie = readCookie('BlockbusterUserIdStoreCode');
			if (userCookie==null || userCookie=='null') {
				gravaCookieUrlBack(urlBack);
			}
			setNotInterestReturn(cpCookie, this.ratingId);
			/*var url = 'https://carrinho.blockbusteronline.com.br/locaonline/begin.do?setNotInterest=true&itemId='+ this.ratingId + '&urlBack=' + urlBack;
			var obj=new JSONscriptRequest(url);
			obj.buildScriptTag();
			obj.addScriptTag();*/
		}catch(exception){}
	}
}

RatingManager.prototype.setNotInterestedReturn = function() {
	if (!eval(jQuery("input#notInterested"+this.ratingId).attr("value"))) {
		jQuery("input#notInterested"+this.ratingId).attr("value","true");
		var ratingElem = jQuery("input#"+this.ratingId + "Selected");
		
		if (ratingElem.length <= 0)
			ratingElem = jQuery("input#Selected"+this.ratingId)
			
		ratingElem.attr("value","0");
		
		jQuery.ajax({
				type:'POST',
				data:{
					'movieId': this.ratingId
				}, 
				url:'/interaction/rating/setInterest',
				success: function(html){
				},
				error:function(){},
				async:false
			});
	}
}

RatingManager.prototype.notInterestedReselect = function(deselectRating) {
	notInterestedImg = jQuery("img#notInterestedImg"+this.ratingId)
	notInterestedValue = jQuery("input:hidden#notInterested"+this.ratingId).attr("value");

	if (eval(notInterestedValue))
		notInterestedImg.attr("src",notInterestedImg.attr("src").replace("_off","_on"));
	else
		notInterestedImg.attr("src",notInterestedImg.attr("src").replace("_on","_off"));

	if (eval(deselectRating)){
		this.ratingReselect(false);
	}
}

RatingManager.prototype.notInterestedSelect = function(deselectRating) {
	notInterestedImg = jQuery("img#notInterestedImg"+this.ratingId)
	notInterestedImg.attr("src",notInterestedImg.attr("src").replace("_off","_on"));
	
	if (eval(deselectRating))
		this.ratingDeselect(false);
}

RatingManager.prototype.notInterestedDeselect = function(selectRating) {
	notInterestedImg = jQuery("img#notInterestedImg"+this.ratingId)
	notInterestedImg.attr("src",notInterestedImg.attr("src").replace("_on","_off"));
	
	if (eval(selectRating))
		this.ratingReselect(false);	
}

var ratingArray = new RatingArray();

hideDivStatus = function(divStatus){
	$(divStatus).update("");
	$(divStatus).hide();
};

// FUNCOES UTILITARIAS
function redirectTo(url) {
	var urlBack = window.location.href;
	var userCookie = readCookie('BlockbusterUserIdStoreCode');
	if (userCookie==null || userCookie=='null') {
		gravaCookieUrlBack(urlBack);
	}
	window.location = url
}
