dojo.require("dojo.parser");
dojo.require("dojo.string");
dojo.require("dojox.widget.Loader");
dojo.require("dijit.form.Form");
dojo.require("dijit.Dialog");
dojo.require("dojox.layout.ContentPane");

// ===================================
// = class FnFeedbackChannelComments =
// ===================================

FnFeedbackChannelComments = function() {
	console.debug("Inside Constructor !!!");
	
	// Adding the Layout container to the DIV tag "fnFeedbackChannelCommentsPlaceholder"
	this._placeholder = dojo.byId("fnFeedbackChannelCommentsPlaceholder");

	if (this._placeholder) {
		// Adding the title header
		var commentingDivTitlePlaceholder = document.createElement("div");
		commentingDivTitlePlaceholder.id = "commentingDivTitlePlaceholder";
		
		// Temp Message Holder (for the loading icon)
		var tempMessagePlaceholder = document.createElement("div");
		tempMessagePlaceholder.id = "tempMessagePlaceholder";
		
		var commentingDivTitlePlaceholder_title = document.createElement("div");
		commentingDivTitlePlaceholder_title.id = "commentingDivTitlePlaceholder_title";
		commentingDivTitlePlaceholder.appendChild(commentingDivTitlePlaceholder_title);
		
		var commentingDivTitlePlaceholderH3 = document.createElement("h3");
		commentingDivTitlePlaceholderH3.innerHTML = "Community Notes";
		commentingDivTitlePlaceholder_title.appendChild(commentingDivTitlePlaceholderH3);
		
		var commentingDivTitlePlaceholder_button = document.createElement("div");
		commentingDivTitlePlaceholder_button.id = "commentingDivTitlePlaceholder_button";
		commentingDivTitlePlaceholder.appendChild(commentingDivTitlePlaceholder_button);
		
		var commentingDivTitlePlaceholderSpan = document.createElement("span");
		commentingDivTitlePlaceholderSpan.className = "fnButton";
		commentingDivTitlePlaceholderSpan.setAttribute("style", "position: relative");
		commentingDivTitlePlaceholder_button.appendChild(commentingDivTitlePlaceholderSpan);
		
		var commentingDivTitlePlaceholderInput = document.createElement("input");
		commentingDivTitlePlaceholderInput.setAttribute("type", "button");
		commentingDivTitlePlaceholderInput.id = "showCommetingForm_button_1";
		commentingDivTitlePlaceholderInput.value = "+ Add Note";
		
		dojo.connect(commentingDivTitlePlaceholderInput, 'onclick', this, 'toggleDisplayCommetingForm');
		commentingDivTitlePlaceholderSpan.appendChild(commentingDivTitlePlaceholderInput);
		
		this._placeholder.appendChild(commentingDivTitlePlaceholder);
		this._placeholder.appendChild(tempMessagePlaceholder);
				
		// Creating and adding the comments view container to the root container
		console.debug("Creating and adding the comments view container to the root container");
		var commentsViewContainer = document.createElement("div");
		commentsViewContainer.id = "commentsViewContainer";
		commentsViewContainer.setAttribute("dojoType","dijit.layout.ContentPane");
		dojo.byId("fnFeedbackChannelCommentsPlaceholder").appendChild(commentsViewContainer);
		this._commentsViewContainer = new dojox.layout.ContentPane({ executeScripts: true }, dojo.byId("commentsViewContainer"));
	}
}

FnFeedbackChannelComments.prototype._instance;
FnFeedbackChannelComments.prototype._placeholder;

// Containers for holding Dojo widgets
FnFeedbackChannelComments.prototype._layoutContainer;
FnFeedbackChannelComments.prototype._generalMessageContainer;
FnFeedbackChannelComments.prototype._commentsViewContainer;
FnFeedbackChannelComments.prototype._prewviewDialog;

/**
*  Creating a singleton object
*/
FnFeedbackChannelComments.getInstance = function() {
	if (FnFeedbackChannelComments._instance == null) {
		console.debug("Creating new FnFeedbackChannelComments object");
		FnFeedbackChannelComments._instance = new FnFeedbackChannelComments();
	}
	return FnFeedbackChannelComments._instance;
}

/**
*  Build layout and containers
*/
FnFeedbackChannelComments.buildLayoutContainers = function() {
	if (FnFeedbackChannelComments._instance == null) {
		console.debug("Creating new FnFeedbackChannelComments object");
		FnFeedbackChannelComments._instance = new FnFeedbackChannelComments();
	}
	return FnFeedbackChannelComments._instance;
}

FnFeedbackChannelComments.prototype.startup = function() {
	console.debug("startup()");
	// Try to initialize the javascript vBUlletin variable
	/*try {
		  vBScriptMain = document.createElement('script');
		  vBScriptMain.type = 'text/javascript';
		  vBScriptMain.innerHTML = 'vBulletin_init();';				
		  documentHead.appendChild(vBScriptMain);
	}
	catch(err) {
		  console.debug("Error while trying to initialize vBulletin_init() in commenting : " + err);
	}*/
		
	// Call to the dibo to attempt an auto login
	dojo.io.script.get({url: "/forum/faq.php?exposeFeedBackChannels=true&exposeCallback=fnFeedbackChannelComments.searchWhetherThreadExists"});
}

/**
* Search in Dibo weather the thread exits for the page URL
*/
FnFeedbackChannelComments.prototype.searchWhetherThreadExists = function() {
	console.debug("searchWhetherThreadExists()");

	if (this._placeholder) {
		var obj = FnFeedbackChannelComments.getInstance();
		obj.showLoaderAnimation(true);
		obj.setErrorMessage("");

		var val = dojo.xhrGet ({
			// Location of the HTML content we want to grab
			url: "/forum/showthread.php?exposeFeedBackChannels=true",
			preventCache: true,
			timeout: 40000,
			// Called when the page loaded successfully
			load: function (response, ioArgs) {
				console.debug("Build View Comments when thread found");
				obj._commentsViewContainer.setContent(response);
				obj.showLoaderAnimation(false);
				return response;
			},
			// Called if there was an error
			error: function (response, ioArgs) {
				var obj = FnFeedbackChannelComments.getInstance();
				
				if (response.message == "timeout exceeded") {
					obj.setErrorMessage("Request timeout exceeded. Please refresh the page and retry");
					obj.showLoaderAnimation(false);
				}
				else {
					   if (ioArgs.xhr && (ioArgs.xhr.status == 301 || ioArgs.xhr.status == 302)) {
						   var url = ioArgs.xhr.getResponseHeader("Location");
					   
						   console.debug("Received HTTP redirect. Retrying after delay.", ioArgs.xhr.status, url);
						   // timeout and try again a little later..
					   }
					   // Checking whether the browser is Opera because it does not support 401
					   // response code
					   else if (dojo.isOpera > 0) {
						   if (ioArgs.xhr.status == 400) {
							   obj.setErrorMessage(ioArgs.xhr.statusText);
						   }
					   }
					   // A generic Error occured
					   else if (ioArgs.xhr.status == 400) {
						   // Displaying the error message to the user
						   if (dojo.isSafari > 0) {
							   obj.setErrorMessage(response.message);
						   }
						   else {
							   obj.setErrorMessage(ioArgs.xhr.statusText); 
						   }
					   }
					   obj.showLoaderAnimation(false);
				}
				console.debug("Error: " + ioArgs.xhr.status + " " + ioArgs.xhr.statusText);
				return response;
			}
		});	 
	}
	else {
		console.debug("No comment block found");
	}
}

/**
* POST form data to either newthread.php or newreply.php in Dibo
*/
FnFeedbackChannelComments.prototype.postComment = function() {
	console.debug("postComment");
	var obj = FnFeedbackChannelComments.getInstance();
	obj.showLoaderAnimation(false);
	
	// checking the message length
	if (dojo.byId("feedback_channel_comments_textarea").value.length < 10) {
		obj.setErrorMessage("The message you have entered is too short. Please lengthen your message to at least 10 characters");
	}
	else {
		obj.showLoaderAnimation(true);
		obj.setErrorMessage("");
		// Removing the Prview Note hidden field values
		obj.setRequestAsPreviewNote(false);

		dojo.xhrPost({
			// Getting the Commenst form by ID
			form: dojo.byId("commentsForm"),
			// This is the URL where we going to submit the Form to. Build upon weather the thread exist or not. 
			// See searchWhetherThreadExists()
			url: dojo.byId("notePostUrl").value,
			timeout: 40000,
			load: function(response, ioArgs) {
				// Checking whether the post is a new thread
				// and if it is build up the comments form to submit a reply
				// from next time
				obj.searchWhetherThreadExists();
				obj.showLoaderAnimation(false);
				obj.setCommentButtonText();
			},
			error: function(response, ioArgs) {
				if (response.message == "timeout exceeded") {
					obj.setErrorMessage("Request timeout exceeded. Please refresh the page and retry");
					obj.showLoaderAnimation(false);
				}
				else {
					console.debug("URL STRING : " + dojo.byId("notePostUrl").value);
					console.debug(ioArgs.xhr.status);
					
					// Displaying the error message to the user
					var errorMessage = ioArgs.xhr.getResponseHeader("X-FVFCError");
					if (errorMessage == null || errorMessage == "") {
						errorMessage = ioArgs.xhr.statusText;
					}
					obj.setErrorMessage(errorMessage);
					obj.showLoaderAnimation(false);
			   }
			}
		});
	}
	return false;
}

/**
* POST form data to either newthread.php or newreply.php in Dibo
*/
FnFeedbackChannelComments.prototype.submitPreviewNote = function() {
	console.debug("postComment");
	var obj = FnFeedbackChannelComments.getInstance();
	obj.showLoaderAnimation(false);
	
	// checking the message length
	if (dojo.byId("feedback_channel_comments_textarea").value.length < 10) {
		obj.setErrorMessage("The message you have entered is too short. Please lengthen your message to at least 10 characters");
	}
	else {
		obj.showLoaderAnimation(true);
		obj.setErrorMessage("");

		dojo.xhrPost({
			// Getting the Commenst form by ID
			form: dojo.byId("commentsForm"),
			// This is the URL where we going to submit the Form to. Build upon weather the thread exist or not. 
			// See searchWhetherThreadExists()
			url: dojo.byId("notePostUrl").value,
			timeout: 40000,
			load: function(response, ioArgs) {
				// Popping up and diasplaying the Preview of the note via dijit.Dialog
				obj.popupPreviewNote(response);
				obj.showLoaderAnimation(false);
			},
			error: function(response, ioArgs) {
				if (response.message == "timeout exceeded") {
					obj.setErrorMessage("Request timeout exceeded. Please refresh the page and retry");
					obj.showLoaderAnimation(false);
				}
				else {
					console.debug("URL STRING : " + dojo.byId("notePostUrl").value);
					console.debug(ioArgs.xhr.status);
					
					// Displaying the error message to the user
					var errorMessage = ioArgs.xhr.getResponseHeader("X-FVFCError");
					if (errorMessage == null || errorMessage == "") {
						errorMessage = ioArgs.xhr.statusText;
					}
					obj.setErrorMessage(errorMessage);
					obj.showLoaderAnimation(false);
			   }
			}
		});
	}
	return false;
}

/**
*  Add/Remove the loader Animation
*/
FnFeedbackChannelComments.prototype.showLoaderAnimation = function(state) {
	if (state) {
		console.debug("showing loader animation");
		
		var generalMessageContainerDOM = dojo.byId("generalMessageContainer");
		if (generalMessageContainerDOM == null) {
			var generalMessageContainerDOM = dojo.byId("tempMessagePlaceholder");
		}
		// Adding the widget loading Animation
		var loaderContainerDiv = document.createElement("Div");
		generalMessageContainerDOM.appendChild(loaderContainerDiv);
		var loadingAnimation = new dojox.widget.Loader({attachToPointer: false}, loaderContainerDiv); 
	}
	else {
		try{
			// Just remove the loader
			console.debug("removing loader animation");
			var generalMessageContainer = dojo.byId("generalMessageContainer");
			var tempMessagePaceholder = dojo.byId("tempMessagePlaceholder");
			
			if (generalMessageContainer != null) {
				var lenGen = generalMessageContainer.childNodes.length;
				for(var i = 0; i < lenGen; i++) {
				   generalMessageContainer.removeChild(generalMessageContainer.childNodes[i]);
				}
			}
			if (tempMessagePaceholder != null) {
				var lenTemp = tempMessagePaceholder.childNodes.length;
				for(var i = 0; i < lenTemp; i++) {
				   tempMessagePaceholder.removeChild(tempMessagePaceholder.childNodes[i]);
				}
			}
		}
		catch(err) {
			console.debug("Error occuered removing loader animation"); 
		}
	}
}

/**
*  Set an Error Message in the Error Message Div
*/
FnFeedbackChannelComments.prototype.setErrorMessage = function(message) {
	if (dojo.byId("errorMessageContainer") != null) {
		dojo.byId("errorMessageContainer").innerHTML = message;
	}
}

/**
* Setting up the post id for a new reply
*/
FnFeedbackChannelComments.prototype.replyToPost = function(postId, postTitle) {
	var fnFeedbackChannelCommentsObject = FnFeedbackChannelComments.getInstance();
	// Changing the post reply Submit Url 
	dojo.byId("notePostUrl").value = "/forum/newreply.php?do=postreply&p=" + postId + "&exposeFeedBackChannels=true&isReplyToThread=false";
	// Setting the post form title
	var comnetFormTitle = "Reply to note: " + postTitle;
	fnFeedbackChannelCommentsObject.setCommentFormTitle(comnetFormTitle);
	// Changing the Reply Text Title
	dojo.byId("feedbackChannelComments_title").value = "Re: " + postTitle;
	// Displaying the reply box 
	dojo.byId("fnCommentsMainContainer").style.display = "block";
	fnFeedbackChannelCommentsObject.scrollToEditBox();
	fnFeedbackChannelCommentsObject.setCommentButtonText();
	
	return false;
}

/**
*  Preview the post
*/
FnFeedbackChannelComments.prototype.setRequestAsPreviewNote = function(status) {
	var fnFeedbackChannelCommentsObject = FnFeedbackChannelComments.getInstance();	
	if (status) {
		// Changing the post reply Submit Url 
		dojo.byId("notePostUrl").value = dojo.byId("notePostUrl").value + "&exposeFeedBackChannels=true&communityNotePreview=true";
		
		var setPreviewHiddenField = dojo.byId("addNoteSetDisplayPreview");
		setPreviewHiddenField.name = "preview";
		setPreviewHiddenField.value = "Preview Post";
		
		// Posting the form
		fnFeedbackChannelCommentsObject.submitPreviewNote();
	}
	else {
		// Changing the post reply Submit Url 
		dojo.byId("notePostUrl").value = dojo.byId("notePostUrl").value + "&exposeFeedBackChannels=true";
		
		var setPreviewHiddenField = dojo.byId("addNoteSetDisplayPreview");
		setPreviewHiddenField.name = "";
		setPreviewHiddenField.value = "";
	}
	
	return false;
}

/**
* Toggles the display of the commenting form area
*/
FnFeedbackChannelComments.prototype.toggleDisplayCommetingForm = function(clearFields) {
	var obj = FnFeedbackChannelComments.getInstance();
	if (dojo.byId("fnCommentsMainContainer") != null) { 
		if (dojo.byId("fnCommentsMainContainer").style.display == "none" || 
			dojo.byId("fnCommentsMainContainer").style.display == "" || 
			dojo.byId("fnCommentsMainContainer").style.display == null) {
				
			dojo.byId("fnCommentsMainContainer").style.display = "block";
			// Scroll the Window to the commenting form
			obj.scrollToEditBox();
			obj.setCommentButtonText();
			if (dojo.byId("mainHeading") != null) {
				obj.setCommentFormTitle("Add note to: " + dojo.byId("mainHeading").innerHTML);
			}
		}
		else {
			dojo.byId("fnCommentsMainContainer").style.display = "none";
			// Resetting the URL to repl for the thread again
			if (dojo.byId("notePostUrl") != null && dojo.byId("notePostThreadUrl") != null) {
				dojo.byId("notePostUrl").value = dojo.byId("notePostThreadUrl").value;
			}
			obj.setCommentButtonText();
			obj.setErrorMessage("");
		}
		
		if (clearFields) {
			// Clear the title field
			if (dojo.byId("feedbackChannelComments_title") != null) {
				dojo.byId("feedbackChannelComments_title").value = "";
			}
			// Clear the message field
			if (dojo.byId("feedback_channel_comments_textarea")) {
				dojo.byId("feedback_channel_comments_textarea").value = "";
			}
		}
	}
	else {
		console.debug("fnCommentsMainContainer not found in toggleDisplayCommetingForm");
	}
	return false; 
}

FnFeedbackChannelComments.prototype.scrollToEditBox = function() {
	if (dojo.byId("fnCommentsMainContainer") != null) {
		// IE is not setting the offset corectly.  We'll force it to the
		// bottom of the page.
		if (dojo.byId("fnCommentsMainContainer").offsetTop == 0) {
			window.scrollTo(0, 65000);
		}
		else {
			window.scrollTo(dojo.byId("fnCommentsMainContainer").offsetLeft, dojo.byId("fnCommentsMainContainer").offsetTop);			 
		}
	}
	else {
		console.debug("fnCommentsMainContainer not found in scrollToEditBox");
	}
}

FnFeedbackChannelComments.prototype.scrollToNotes = function() {
	if (dojo.byId("posts") != null) {	  
		window.scrollTo(dojo.byId("posts").offsetLeft, dojo.byId("posts").offsetTop);
	}
	else {
		console.debug("posts not found in scrollToEditBox");
	}
	return false;
}

FnFeedbackChannelComments.prototype.popupPreviewNote = function(content) {
	var obj = FnFeedbackChannelComments.getInstance();
	obj._prewviewDialog.setContent(content);
	obj._prewviewDialog.show();
}

FnFeedbackChannelComments.prototype.hidePreviewNote = function() {
	var obj = FnFeedbackChannelComments.getInstance();
	obj._prewviewDialog.hide();
	return false;
}

FnFeedbackChannelComments.prototype.setCommentButtonText = function() {
	var obj = FnFeedbackChannelComments.getInstance();
	if (dojo.byId("fnCommentsMainContainer") != null) {
		if (dojo.byId("fnCommentsMainContainer").style.display == "none" || dojo.byId("fnCommentsMainContainer").style.display == "") {
			dojo.byId("showCommetingForm_button_1").value = "+ Add Note";
			if (dojo.byId("showCommetingForm_button_2") != null) {
				dojo.byId("showCommetingForm_button_2").value = "+ Add Note";
			}
		}
		else {
			dojo.byId("showCommetingForm_button_1").value = "- Hide Note";
			if (dojo.byId("showCommetingForm_button_2") != null) {
				dojo.byId("showCommetingForm_button_2").value = "- Hide Note";
			}
		}
	}
	else {
		console.debug("fnCommentsMainContainer not found in setCommentButtonText");
	}
}

FnFeedbackChannelComments.prototype.subscribeToPage = function(link, refreshComments) {
	var subscribtionLink = link.href;
	var obj = FnFeedbackChannelComments.getInstance();
	var val = dojo.xhrGet ({
		// Location of the HTML content we want to grab
		url: subscribtionLink,
		preventCache: true,
		sync: true,
		timeout: 40000,
		// Called when the page loaded successfully
		load: function (response, ioArgs) {
			//dojo.byId('subscribeLoaderAnimation').display = 'block';
			dojo.byId('subscribeToThread').innerHTML = response;
			//dojo.byId('subscribeLoaderAnimation').display = 'none';
			if (refreshComments) {
				// Refreshing the commenst on the page after subscription
				obj.searchWhetherThreadExists();
			}
		},
		// Called if there was an error
		error: function (response, ioArgs) {
			//dojo.byId('subscribeLoaderAnimation').display = 'block';
			console.debug('ERROR in subscribeToPage ' + response);
			//dojo.byId('subscribeLoaderAnimation').display = 'none';
		}
	});
	return false;
}

FnFeedbackChannelComments.prototype.setCommentFormTitle = function(heading) {
	// Setting the Form Title
	if (dojo.byId('addANoteTitleHeading') != null) {
	   var formTitle = dojo.byId('addANoteTitleHeading');
	   formTitle.innerHTML = "<h3>" + heading + "</h3>";
	}
	else {
		console.debug('addANoteTitleHeading div not found');
	}
}

/**
 * This function will write community note summary text to <div id='communityNoteSummaryPlaceholder'>
 */
FnFeedbackChannelComments.prototype.displayCommunityNotesSummary = function(postcount) {
	try {
		// Setting the Form Title
		if (dojo.byId('communityNoteSummaryPlaceholder') != null) {
		   console.debug('<div id="communityNoteSummaryPlaceholder"> found');	 
		   if (postcount <= 1) {
			   console.debug('No Notes');
			   dojo.byId('communityNoteSummaryPlaceholder').innerHTML = 'No Notes (<a href="#" onclick="return fnFeedbackChannelComments.toggleDisplayCommetingForm(false)">Add a note<a>)';
		   }
		   else if (postcount == 2) {
			   console.debug((postcount-1) + ' Note');
			   dojo.byId('communityNoteSummaryPlaceholder').innerHTML =	 '' + (postcount-1) + ' Note (<a href="#" onclick="return fnFeedbackChannelComments.scrollToNotes()">View note<a>)';
		   }
		   else {
			   console.debug((postcount-1) + ' Notes');
			   dojo.byId('communityNoteSummaryPlaceholder').innerHTML =	 '' + (postcount-1) + ' Notes (<a href="#" onclick="return fnFeedbackChannelComments.scrollToNotes()">View notes<a>)';
		   }
		}
		else {
			console.debug('No <div id="communityNoteSummaryPlaceholder"> defined in the page');
		}
	}
	catch (err) {
		console.debug('Error displaying community note summary : ' . err);
	}
}

// ========================================
// = Code to run when the above is loaded =
// ========================================
fnFeedbackChannelComments = null;

// run fnBuildCommentsView() once the page has been loaded.
dojo.addOnLoad(function(){
	// Appending the vBulletin vbulletin_global.js to the head to work with the WYSYWIG editor
	var documentHead = document.getElementsByTagName("head").item(0);

	// Used when browser is IE6 to fix the positions of the images on the editor toolbar.
	if (dojo.isIE == 6) {
	  FnFeedbackChannelComments.changeCssRule('img', 'position', 'fixed');
	  FnFeedbackChannelComments.changeCssRule('.controlbar', 'position', 'fixed');
	  FnFeedbackChannelComments.changeCssRule('.imagebutton', 'position', 'fixed');
	}

	var vBScriptGlobal = document.createElement("script");
	vBScriptGlobal.src = "http://www.forum.nokia.com/piazza/discussion/forum/clientscript/vbulletin_global.js";
	vBScriptGlobal.type = "text/javascript";
	documentHead.appendChild(vBScriptGlobal);
	
	var vBScriptMenu = document.createElement("script");
	vBScriptMenu.src = "http://www.forum.nokia.com/piazza/discussion/forum/clientscript/vbulletin_menu.js";
	vBScriptMenu.type = "text/javascript";
	documentHead.appendChild(vBScriptMenu);
	
	var vBScriptPostLoader = document.createElement("script");
	vBScriptPostLoader.src = "http://www.forum.nokia.com/piazza/discussion/forum/clientscript/vbulletin_post_loader.js";
	vBScriptPostLoader.type = "text/javascript";
	documentHead.appendChild(vBScriptPostLoader);
	
	fnFeedbackChannelComments = FnFeedbackChannelComments.getInstance();
	// The Dojo dijit.Dialog for the preview functionality of a note
	fnFeedbackChannelComments._prewviewDialog = new dijit.Dialog({style: "width: 604px"},dojo.byId("commentPreviewDialog"));
	fnFeedbackChannelComments.startup();
});

/**
 * This function is used to change a CSS Rule in IE.
 * Note: This will only work in IE to change for use with other browsers:
 * 1. change rules to cssRules
 * 2. change the addRule function to the insertRule function
 *
 * @param cssClass is the class that we are checking to see if it is in the stylesheets.
 * @parm attribute is the css attribute that we are checking to see that it is in the class 
 *	  and changing its value.
 * @param value is the value that you wish the attribute to be changed to.
 *
 
**/
FnFeedbackChannelComments.changeCssRule = function(cssClass, attribute, value) {
	var styleSheetCount = document.styleSheets.length;
	var found = false;
	// Iterate through all the style sheets
	for (var i = 0; i < styleSheetCount; i++) {
		// iterate through all the rules
		for (var j = 0; j < document.styleSheets[i].rules.length; j++) {
			console.debug('Selector:' + document.styleSheets[i].rules[j].selectorText.toLowerCase());
			// if you find the rule
			if (document.styleSheets[i].rules[j].selectorText.toLowerCase() == cssClass) {
				// check whether it has attribute position
				if (document.styleSheets[i].rules[j].style[attribute]) {
					console.debug('Attribute position found');
					// update it
					document.styleSheets[i].rules[j].style[attribute] = value;
					found = true;
				}
				else {
					console.debug('Attribute position not found');
				}
			}
		}
	}
	// If not found add
	if (found == false) {
		document.styleSheets[styleSheetCount -1].addRule(cssClass, (attribute + ':' + value));
	}
}

