var agt   = navigator.userAgent.toLowerCase();
var vers  = navigator.appVersion.toLowerCase();
var major = parseFloat(navigator.appVersion);
var mac   = agt.indexOf("mac") != -1;


var macIe = mac && document.all;

var arrayStatus1 = new Array();
arrayStatus1['A+'] = 	new Array('Oscar-worthy', 	96);
arrayStatus1['A'] = 	new Array('Outstanding', 	90);
arrayStatus1['A-'] = 	new Array('Almost perfect', 84);
arrayStatus1['B+'] = 	new Array('Memorable', 		76);
arrayStatus1['B'] = 	new Array('Good' 		,	70);
arrayStatus1['B-'] = 	new Array('Fair', 			64);
arrayStatus1['C+'] = 	new Array('Flawed',			56);
arrayStatus1['C'] = 	new Array('Mediocre', 		50);
arrayStatus1['C-'] = 	new Array('Disappointing', 	44);
arrayStatus1['D+'] = 	new Array('Poor effort', 	36);
arrayStatus1['D'] = 	new Array('Dreadful', 		30);
arrayStatus1['D-'] = 	new Array('Truly awful', 	24);
arrayStatus1['F'] = 	new Array('All-time worst', 10);
arrayStatus1['Z'] = 	new Array('Select grade<br>to the right', 0);


var noValue = "Z";
var grades = new Array();
grades[0] = new Array('A+', 'A', 'A-');
grades[1] = new Array('B+', 'B', 'B-');
grades[2] = new Array('C+', 'C', 'C-');
grades[3] = new Array('D+', 'D', 'D-');
grades[4] = new Array('F');

var textValues 			= new Array();
textValues['rating'] 	= new Array('My Grade:');
textValues['story'] 	= new Array('Story:');
textValues['acting'] 	= new Array('Acting:');
textValues['direction'] = new Array('Direction:');
textValues['visuals'] 	= new Array('Visuals:');
textValues['overall'] 	= new Array('Overall:');


var gHREF = "http://www.yahoo.com/test?";


var gPopup = null;
function getScrollLeftWindow()
{
	if (document.compatMode !="BackCompat") return document.documentElement.scrollLeft;
	else return document.body.scrollLeft;
}

function getScrollTopWindow()
{
	
	if (document.compatMode !="BackCompat") return document.documentElement.scrollTop;
	else return document.body.scrollTop;
}

function getElemLeftOnWindow( elem )
{
	var res = 0;
	for ( ;elem; elem = elem.offsetParent )
	{
		res += elem.offsetLeft;
	}
	
	return res;
}

function getElemTopOnWindow( elem )
{
	var res = 0;
	for ( ;elem; elem = elem.offsetParent )
	{
		res += elem.offsetTop;
	}
	
	return res;
}

function moviePicker(appendTo, popupReturn, id, pickerType, textArray)
{


	var oCont = document.createElement('DIV');
	var classNme = "gradePickerAlt";
	if ( pickerType == "popup") classNme = "gradePickerPopup";
	else if (pickerType == "rating" || pickerType == "overall") classNme = "gradePicker"
	oCont.arrayStatus = textArray;
	oCont.className = classNme;
	oCont.popupReturn = popupReturn;
	oCont.pickerType = pickerType;
	oCont.saved = false;
	oCont.saving = false;
	oCont.hiddenElem = document.getElementById(id +"_value");
	oCont.id = id;
    oCont.saveStuff = saveStuff;
    oCont.savePopup = savePopup;
	if (!popupReturn) insertTextContainer(oCont);

	insertGrades(oCont);
	oCont.onclick = cellClick;
	appendTo.appendChild(oCont);
	return oCont;
}


function insertTextContainer(cont)
{
	
	var textCont = document.createElement('DIV');
	textCont.className = "textContainer";
	var letterValue = getLetter(cont.hiddenElem.value, cont.arrayStatus);
	textCont.innerHTML = getSideContent(letterValue, cont.id, cont.pickerType, cont.arrayStatus, cont.saved)

	cont.appendChild(textCont);
	cont.textContainer = textCont;
	cont.currentText =letterValue;
}


function getLetter(value, arrayStatus)
{
	for (var i in arrayStatus)
	{
		if (arrayStatus[i][1] == value) return i

	}
	return noValue;
}

function getSideContent(letter, id, type, arrayStatus, saved)
{
	curContent = arrayStatus[letter];
	var string = "<div class=\"mygrade\">"+ textValues[type][0] +"</div>"
	if (letter != noValue) string += "<div class=\"bigletter\">"+ letter + "</div>"
	else if (type=="rating") string += "<div class=\"norating\">Rate this Movie!</div>"
	else  string += "<div class=\"norating\">Select a grade to the right!</div>"

	if (letter != noValue || type == "rating")string += "<div class=\"description\">" + curContent[0] + "</div>"
	var saveStr = saved ? "saved" : "&nbsp;";
	var saveClass = saved ? "saved" : "saved";
	string += "<div class=\""+saveClass+"\" id=\""+ id +"_saving\">"+saveStr+"</div>"

	if (type=="rating") string += "<div class=\"reviewlink\"><a href=\"" + gHREF + ""+ id + "\">write a review</a></div>"
	return string;
}








function insertGrades(cont)
{
	for ( var i =0; i < grades.length ; i ++)
	{
		var curGrade = grades[i];
		var rowDiv = document.createElement('DIV');
		rowDiv.id = "row" + i;
		rowDiv.className = "row";
		rowDiv.onmouseover = rowHover;
		rowDiv.onmouseout = rowOut;

		if (macIe && i!=0)
		{
			rowDiv.style.top = "0px";
		}
		if (macIe && !cont.popupReturn)
		{
		rowDiv.style.left = "80px";
		}
		else if (macIe)
		{
			rowDiv.style.left = "3px";
		}

		for (var j = 0 ; j < curGrade.length; j++)
		{
			var cellDiv = document.createElement('DIV');
			cellDiv.className = "cell" + j;
			cellDiv.id = "cell" + j;
			cellDiv.innerHTML=curGrade[j];
			cellDiv.contId = cont.id;
			cellDiv.onmouseover = cellHover;
			cellDiv.onmouseout = cellOut;
			rowDiv.appendChild(cellDiv);
		}
		cont.appendChild(rowDiv);
	}

}



function rowHover()
{
	this.className ="rowHover";
	if (macIe && this.id.indexOf("row0")==-1) this.style.top = "0px";
}

function rowOut()
{
	this.className ="row";
	if (macIe && this.id.indexOf("row0")==-1)
	{

		this.style.top = "0px";

	}

}

function cellOut()
{
	this.className =this.id;
	var curCont = 	document.getElementById(this.contId);
	if (!curCont.popupReturn && !macIe && !curCont.saving)
	{
		curCont.textContainer.innerHTML = getSideContent(curCont.currentText, curCont.id, curCont.pickerType, curCont.arrayStatus, curCont.saved);
	}

}

function cellHover()
{

	this.className =this.id + " hover";
	var curCont = document.getElementById(this.contId);
	if (!curCont.popupReturn && !macIe && !curCont.saving)
	{
		curCont.textContainer.innerHTML = getSideContent(this.innerHTML, curCont.id, curCont.pickerType,  curCont.arrayStatus, curCont.saved);
	}

}

function cellClick(p_oEvent)
{
	
	var oEvent = p_oEvent ? p_oEvent : window.event;
	var oSender = p_oEvent ? oEvent.target : oEvent.srcElement;
	if (oSender.nodeName == "#text") oSender = oSender.parentNode;

	var iscell = oSender.className.indexOf('cell') != -1 ? true : false;
	if (this.popupReturn && iscell )
	{
		
		this.popupReturn.innerHTML = "My Rating: " + oSender.innerHTML;
		var hidVal = document.getElementById(this.id +"_value");
		hidVal.value =  this.arrayStatus[oSender.innerHTML][1];

		//changed to get back the not interested link
		var notInterestedForm = this.popupReturn.parentNode;
//		var oInputs = notInterestedForm.getElementsByTagName('input');
//		if (oInputs[0]) oInputs[0].value = 0;
		
		var oStrong= notInterestedForm.getElementsByTagName('STRONG');
		if (oStrong[0] && this.popupReturn.oldAValue)
            oStrong[0].parentNode.innerHTML = this.popupReturn.oldAValue;
		//end of get back the not interested link

        var ratingForm = document.getElementById("rating" + this.id);
        ratingForm.rating.value = this.arrayStatus[oSender.innerHTML][1];
		this.savePopup();
	}
	else if (iscell)
	{
		if (this.saving) return;
		this.hiddenElem.value =  this.arrayStatus[oSender.innerHTML][1];
		
		this.textContainer.innerHTML = getSideContent(oSender.innerHTML, this.id, this.pickerType , this.arrayStatus, this.saved)
		this.currentText = oSender.innerHTML;
		if (this.pickerTyp1e == 'rating' && !macIe) this.saveStuff();
	}
}

function saveStuff()
{
	
	this.saving = true;
	var id = this.id + "_saving";
	var saving = document.getElementById(id);
	
	//var movForm =document.getElementById('movieRatingsForm');
	//movForm.submit();
	setTimeout("flashingSave('"+ this.id +"', 0)", 300)
}

function savePopup()
{
    var movForm = document.getElementById('rating' + this.id);
	movForm.submit();
}

function flashingSave(id, count)
{

	var saving = document.getElementById(id +"_saving");
	saving.className = "saving"
	if (count%2 != 1) saving.innerHTML = "saving...";
	else saving.innerHTML = "&nbsp;";
	if (count < 9 )
	{
			count++;
			setTimeout("flashingSave('"+ id +"',"+ count +" )", 300);



	}
	else
	{
		var cont = document.getElementById(id);
		cont.saving = false;
		cont.saved = true;
		saving.innerHTML = "saved";
		saving.className = "saved";
	}



}


function showPopup(p_oEvent)
{
	//for mouseover showing
	clearGlobalTimeout();
	//end for mouseover
	var oEvent = p_oEvent ? p_oEvent : window.event;
	var oSender = p_oEvent ? oEvent.target : oEvent.srcElement;

	if (oSender.nodeName == "#text") oSender = oSender.parentNode;
	this.blur();
	if (gPopup)
	{
		destroy(gPopup);
		gPopup =null;
	}

	var cont = new moviePicker(document.body, this, this.getAttribute('movieId') , "popup", this.arrayStatus);
	var top = getElemTopOnWindow( oSender ) - cont.offsetHeight;
	//for mouseover showing
	cont.onmouseover = clearGlobalTimeout;
	cont.onmouseout = hidePopup;
	//end for mouseover
	cont.style.top = top > 0 ? top + "px" : "0px";
	var leftPos = getElemLeftOnWindow( oSender )
	cont.style.left = leftPos + "px";
	gPopup = cont;
	return false;
}

function initRaters()
{
	if (!document.getElementById) return false;

	var raters = document.getElementsByName('rater');
	for (var i =0 ; i< raters.length; i++)
	{
		//for mouseover showing
		raters[i].onmouseover = showPopup;
		raters[i].onmouseout = hidePopup;
		//end for mouseover
		raters[i].onclick = showPopup;
		raters[i].type = 'rater'
		raters[i].style.visibility = "visible";
	}
}




function init()
{
	initRaters();
	if (!mac && document.all) document.documentElement.onclick = destroyBody;
	else document.onclick = destroyBody;
}


function destroyBody(p_oEvent)
{
	var oEvent = p_oEvent ? p_oEvent : window.event;
	var oSender = p_oEvent ? oEvent.target : oEvent.srcElement;

	if (oSender.nodeName == "#text") oSender = oSender.parentNode;
	if (oSender.name != 'rater' && gPopup!=null )
	{
		destroy(gPopup);
		gPopup = null;
	}

}
function destroy(obj, doNotRemoveParent)
{
	var numChildNodes = obj.childNodes.length
	
	for (var i=numChildNodes - 1; i>=0; i--)
	{
		obj.removeChild(obj.childNodes[i]);
		
	}
	
	if (!doNotRemoveParent) document.body.removeChild(obj);

}

function rateIt(name, curValue , pickerType, arrayStatus)
{
    if (!document.getElementById)
	{
		var str ="";
		if (pickerType!="popup")
		{
		str += "<table cellpadding=\"1\" cellspacing=\"0\" width=\"140\" bgColor =\"#cccccc\" border=\"0\"><tr><td><table cellpadding=\"3\" cellspacing=\"0\"  border=\"0\" width=\"100%\" bgColor =\"#eeeeee\"><tr valign=\"middle\"><td align=\"center\"><strong>";
		str += 	textValues[pickerType][0];
		str +=	"</strong><br>";
		str +=	"Select a Grade<br>";
		}

		str += "<select name=\""+ name  +"_value\">";
		str += "<option value=\"0\">Rate It</option>";
		for ( var i in arrayStatus )
		{
			if ( i != noValue)
			{
				var selected = (arrayStatus[i][1] == curValue) ? " selected " : "";
				str += "<option value=\""+ arrayStatus[i][1] +"\" "+ selected +">" + i + "</option>";
			}
		}
		str +="</select>";
		if (pickerType!="popup") str +=	"<br></td></tr></table></td></tr></table>";
		document.write( str);
	}
	else
	{
		var str="";
		if ( pickerType == "popup")  str += getHrefText(name, curValue , arrayStatus);

		else str += "<div id=\""+ name +"_cont\"></div>"
		str += "<input type=\"hidden\" name=\""+ name +"_value\" id=\""+ name +"_value\"  value=\""+ curValue +"\" >"
		document.write( str );
		if (pickerType!= "popup")
		{

			var test = document.getElementById(name +"_cont");
			var cont = moviePicker(test, null, name, pickerType , arrayStatus);
		}
		else
		{
			var test = document.getElementById(name +"_href");


			test.arrayStatus = arrayStatus;
		}
	}
}
function getHrefText(name, curValue, arrayStatus)
{
	var hrefText = "Seen It? Rate It!"
	for ( var i in arrayStatus )
	{
		if (i == curValue && i != noValue)
		{
			hrefText = " My Rating: " + i ;
			break;
		}
	}

	return "<a href=\"#\" name=\"rater\" id=\""+ name +"_href\" class=\"rater rateit\" movieId=\""+ name +"\">"+ hrefText +"</a>"

}

function removePicker(pPickerId, pNotInterestedLink)
{
    var linkText = "Don't Recommend Again";
var picker = document.getElementById(pPickerId+'_href');
	oForm = pNotInterestedLink.parentNode;
	oForm.submit();

    if (picker)
    {
    	picker.oldAValue = oForm.innerHTML;
    	var checkCase = picker.oldAValue.indexOf('</A>');
    	var newInner = (checkCase < 0) ? picker.oldAValue.split('</a>') : picker.oldAValue.split('</A>');
        	if (newInner[1]) oForm.innerHTML ='<strong class="notint">' + linkText + '</strong>' + newInner[1] ;
    }
    else
    {
        oForm.innerHTML = '<strong class="notint">' + linkText + '</strong>';
    }

	return false;
}

//for mouseover showing
var gTimeouts =null;
function hidePopup(p_oEvent)
{
	var oEvent = p_oEvent ? p_oEvent : window.event;
	var oSender = p_oEvent ? oEvent.target : oEvent.srcElement;
	
	gTimeouts = setTimeout('closePopup()', 1000);
	
}
function closePopup()
{
	if (gPopup) destroy(gPopup);
	gPopup = null;
	clearGlobalTimeout();
}
function clearGlobalTimeout()
{
		window.clearTimeout(gTimeouts);
		gTimeouts = null;

}
//end for mouseover
