﻿/* RatingSystem, version 0.3
 */


function RatingSystem(ratingsTable, containersDiv, thumbsUp) {
	this.ratingsTable = ratingsTable; // classname of the table containing the divs
	this.containersDiv = containersDiv; // classname of the div
	this.divId;
	
	// Files paths
	this.imageURL = "img/all_thumbs.gif";
	
	
	// true if the pointer was just moved into the picture
	this.firstIn = true;
	
	this.isIE = !!(window.attachEvent && !window.opera); // copied from Prototype
	
	// true if it's a thumbs up, false if it's thumbs down
	this.thumbsUp = thumbsUp;
	
	this.brotherObject;
	
	this.adid;
	
	this.grayVisible = true; // defines if we are showing the gray or the colored image
	this.statefulGrayVisible = true; // same as the same but keeps it in state
	
	this.pixelWidth = 16;
}


// static method
RatingSystem.ratingConstructor = function(ratingsTable, containersDown, containersUp) {
	var tables = document.getElementsByClassName(ratingsTable);
	
	for(var i = 0; i < tables.length; i++) {
		this.createListener(ratingsTable, containersDown, containersUp, tables[i]);
	}
}


// static method
RatingSystem.createListener = function(ratingsTable, containersDown, containersUp, tableElement) {
	
	var objThumbsUp = new RatingSystem(ratingsTable, containersUp, true);
	var objThumbsDown = new RatingSystem(ratingsTable, containersDown, false);
	
	objThumbsUp.setBrotherObject(objThumbsDown);
	objThumbsDown.setBrotherObject(objThumbsUp);
	
	var tableId = tableElement.id;
	var adid = document.getElementById(tableId).attributes.adid.nodeValue;
	objThumbsDown.adid = adid;
	objThumbsUp.adid = adid;
	
	
	this.createListenerAux(tableId, objThumbsUp, true);
	this.createListenerAux(tableId, objThumbsDown, false);
}


RatingSystem.createListenerAux = function(tableId, obj, thumbsUp) {
	var divElement;
	var divId;
	
	
	divElement = document.getElementById(tableId).getElementsByClassName(obj.containersDiv)[0];
	divElement.style.background = "url('" + obj.imageURL + "')";
	divElement.style.backgroundPosition = obj.calculateOffSet(thumbsUp, true);
	divElement.style.backgroundRepeat = "no-repeat";
	
	
	divId = divElement.id;
	obj.divId = divId;
	
	
	var onclickFn = function() {
		if( !obj.getFirstIn() ) { // images not already swaped
			obj.swapBackgrounds(divId);
		}
		
		obj.unsetFirstIn();
		obj.saveImagesState();
		
		
		var tmpObj = obj.getBrotherObject()
		tmpObj.setGray( tmpObj.divId );
		tmpObj.saveImagesState();
	};
	
	
	divElement.onclick = onclickFn;
	
	
	if( !obj.isIE ) // IE only throws two events unlike all other browsers.
		divElement.ondblclick = onclickFn;
	
	
	divElement.onmouseover = function (event) {
		obj.setFirstIn();
		obj.saveImagesState();
		
		obj.swapBackgrounds(divId);
	};
	divElement.onmouseout = function (event) {
		obj.loadImagesState(divId);
		obj.rateAds();
	};
}


RatingSystem.prototype.setBrotherObject = function(brotherObject) {
	this.brotherObject = brotherObject;
}


RatingSystem.prototype.getBrotherObject = function() {
	return this.brotherObject;
}


RatingSystem.prototype.setFirstIn = function() {
	this.firstIn = true;
}


RatingSystem.prototype.unsetFirstIn = function() {
	this.firstIn = false;
}


RatingSystem.prototype.getFirstIn = function() {
	return this.firstIn;
}


RatingSystem.prototype.setClass = function(element, classname) {
		element.setAttribute("class", classname);
		element.setAttribute("className", classname); // IE fix
}


RatingSystem.prototype.getClass = function(element) {
	var classname = element.getAttribute("class") || element.getAttribute("className");
	
	return classname;
}


RatingSystem.prototype.swapBackgrounds = function(elementId) {
	var element = document.getElementById(elementId);
	
	element.style.backgroundPosition = this.calculateOffSet(this.thumbsUp, !this.grayVisible);
	this.grayVisible = !this.grayVisible;
}


// This will fix a Safari issue that arrises when comparing the file's URL.
// Safari makes the URL as  "url(file:/// ..."  breaking all the logic.
RatingSystem.prototype.safariFixURL = function(fileURL) {
	var slashLastIndex = fileURL.lastIndexOf("(");
	var strLen = fileURL.length;
	
	return fileURL.substring(slashLastIndex+1, strLen-1);
}


RatingSystem.prototype.isSafari = function() {
	return navigator.userAgent.indexOf('AppleWebKit/') > -1; // prototype code
}


RatingSystem.prototype.swapImagesState = function() {
	if(this.statefulGrayVisible == true)
		this.statefulGrayVisible = false;
	else
		this.statefulGrayVisible = false;
}



RatingSystem.prototype.calculateOffSet = function(thumbsUp, visibleGray) {
	var newOffSet = 0;
	
	if(thumbsUp) {
		if(visibleGray)
			newOffSet = this.pixelWidth * 2;
		else
			newOffSet = this.pixelWidth * 3;
	}
	else {
		if(visibleGray)
			newOffSet = this.pixelWidth * 0;
		else
			newOffSet = this.pixelWidth * 1;
	}
	
	return -newOffSet + "px";
}


RatingSystem.prototype.setGray = function(divId) {
	var element = document.getElementById(divId);
	
	element.style.backgroundPosition = this.calculateOffSet(this.thumbsUp, true);
	this.grayVisible = true;
}


RatingSystem.prototype.loadImagesState = function(elementId) {
	var element = document.getElementById(elementId);
	
	this.grayVisible = this.statefulGrayVisible;
	
	element.style.backgroundPosition = this.calculateOffSet(this.thumbsUp, this.statefulGrayVisible);
}


RatingSystem.prototype.saveImagesState = function() {
	this.statefulGrayVisible = this.grayVisible;
}


RatingSystem.prototype.rateAds = function() {
	var rateValue = this.thumbsUp ? 1 : -1;
	
	if(this.statefulGrayVisible == false)
		rateAd(this.adid, rateValue);
	else
		rateAd(this.adid, 0);
}


RatingSystem.prototype.styleContainsImg = function(style, img) {
	return style.indexOf( this.safariFixURL(img) ) != -1;
}


