/************* OPACITY ***************/

// Fade from one opacity setting to another
function opacityFade (object, opacStart, opacEnd, millisec, thisFade) {
	if (!thisFade) var thisFade = new Date().getTime();
	currentFade = thisFade;

    //speed for each frame
	var skip = 10;
    var speed = Math.round((millisec / 100) * skip);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
	if (opacStart > opacEnd) {
		for (var i=opacStart; i>=opacEnd; i-=skip) {
			setTimeout("opacitySet('" + object + "', " + i + ", "+thisFade+")",(timer * speed));
			timer++;
		}
	} 
	else if (opacStart < opacEnd) {
		for (var i=opacStart; i<=opacEnd; i+=skip) {
			setTimeout("opacitySet('" + object + "', " + i + ", "+thisFade+")",(timer * speed));
			timer++;
		}
	}
}

// Change the opacity for different browsers
function opacitySet (object, opacity, thisFade) {
	if (thisFade && typeof currentFade != 'undefined') {
		if (thisFade != currentFade) {
			return;
		}
	}
	if (typeof object == 'string') object = document.getElementById(object);
    object = object.style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
	object.filter = (opacity < 100 ? "alpha(opacity=" + opacity + ")" : "none");
}





/************ DATE FUNCTIONS **************/







function textInputHint (theInput, theText, theClass) {
	if (typeof theInput == 'string') theInput = document.getElementById(theInput);
	if (typeof theClass == 'undefined') var theClass = 'hint';
	theInput.defaultText = theText;
	theInput.defaultClass = theClass;
	// Add hint if text field is empty
	theInput.onblur = function () {
		if (this.value == '' || this.value == this.defaultText) {
			classAdd(this, this.defaultClass);
			this.value = this.defaultText;
		}
	};
	theInput.onblur(); // force onblur event to run now
	// Remove hint if text field is empty
	theInput.onfocus = function () {
		if (this.value == this.defaultText) {
			classRemove(this, this.defaultClass);
			this.value = '';
		}
	};
	// Remove hint when form is submitted
	if (theInput.form) { // if the text field is inside a submittable form
		if (typeof theInput.form.textInputHints == 'undefined') theInput.form.textInputHints = new Array();
		theInput.form.textInputHints[theInput.form.textInputHints.length] = theInput;
		theInput.form.onsubmit = function () {
			for (var i=0; i<this.textInputHints.length; i++) {
				this.textInputHints[i].onfocus();
			}
		}
	}
}

function loadCSS (url, media) {
	var link = document.createElement('LINK');
	link.rel = 'stylesheet';
	link.type = 'text/css';
	link.href = url;
	if (typeof media != 'undefined') link.media = media;
	document.getElementsByTagName('HEAD')[0].appendChild(link);
}

function loadJS (url) {
	var s = document.createElement('SCRIPT');
	s.type = 'text/javascript';
	s.src = url;
	document.getElementsByTagName('HEAD')[0].appendChild(s);
}


function rotatingHighlightInit () {
	var rh;
	if (rh = document.getElementById('rotatinghighlight')) {
		rHighlights = new Array();
		rHighlightCurrent = 0;
		rHighlightDelay = 7 * 1000; // in milliseconds
		rHighlightFade = 1 * 1000; // in milliseconds
		rHighlightAutomatic = true;
		rHighlightTimeout = new Object();
		rHighlights = rh.getElementsByTagName('LI');
		if (rHighlights.length > 1) {
			for (var i=0; i<rHighlights.length; i++) {
				rHighlights[i].style.zIndex = 1;
				opacitySet(rHighlights[i], 0);
			}
			rHighlights[rHighlightCurrent].style.zIndex = 2;
			opacitySet(rHighlights[rHighlightCurrent], 100);
			rh.innerHTML += '<div class="highlightcontrols"><a onclick="rHighlightGo((rHighlightCurrent-1), true);" href="javascript:void(0);"><img width="23" height="16" src="../images/highlight_box_prev.gif" title="Назад"/></a><a onclick="rHighlightGo((rHighlightCurrent+1), true);" href="javascript:void(0);"><img width="23" height="16" src="../images/highlight_box_next.gif" title="Вперед"/></a></div>';
			setTimeout('if (rHighlightAutomatic) rHighlightGo();', rHighlightDelay);
		}
	}
}

function rHighlightGo (num, now) {
	if (typeof num == 'undefined') var num = rHighlightCurrent+1;
	
	if (num < 0) num = rHighlights.length-1; // negative overflow
	else if (num > rHighlights.length-1) num = 0; // positive overflow
	
	if (typeof now == 'undefined') var now = false;
	if (now) rHighlightAutomatic = false;
	
	rHighlightNewObj = rHighlights[num];
	for (var i=0; i<rHighlights.length; i++) {
		if (i==num) {
			rHighlights[i].style.zIndex = 3;
			if (now) {
				opacitySet(rHighlights[i], 99);
			}
			else {
				opacitySet(rHighlights[i], 0);
				var opacSteps = 20;
				for (var j=1; j<=opacSteps; j++) {
					setTimeout("if (rHighlightAutomatic) opacitySet(rHighlightNewObj, "+Math.min(99, 100 * (j/opacSteps))+")", (rHighlightFade * (j/opacSteps)));
				}
			}
		}
		else if (i==rHighlightCurrent) {
			rHighlights[i].style.zIndex = 2;
			opacitySet(rHighlights[i], 99);
		}
		else {
			rHighlights[i].style.zIndex = 1;
			opacitySet(rHighlights[i], 0);
		}
	}
	setTimeout('rHighlightGoAfter('+num+');', (now ? 0 : rHighlightFade));
}

function rHighlightGoAfter (num) {
	rHighlightCurrent = num;
	for (var i=0; i<rHighlights.length; i++) {
		if (i==rHighlightCurrent) {
			rHighlights[i].style.zIndex = 2;
			opacitySet(rHighlights[i], 99);
		}
		else {
			rHighlights[i].style.zIndex = 1;
			opacitySet(rHighlights[i], 0);
		}
	}
	setTimeout('if (rHighlightAutomatic) rHighlightGo();', rHighlightDelay);
}

// Force the page to refresh (unless an in-page video/audio player is open)
function autoRefresh (minutes) {
	if (typeof minutes == 'undefined') var minutes = 10;
	setInterval('if (new Date().getTime() >= '+(new Date().getTime()+(1000*60*minutes))+') if (!document.getElementById(inpageplayer)) location.replace(document.URL)', 1000*20);
}


function watchnowboxInit (arr) {
	wnCurrent = 0;
	wnArray = arr;
	document.getElementById('watchnowbox').innerHTML += '<div class="watchnowboxControls"><a onclick="watchnowboxPrev(this);" href="javascript:void(0);"><img width="23" height="16" src="../images/watchnow_box_prev.png" title="Назад" /></a><a onclick="watchnowboxNext(this);" href="javascript:void(0);"><img width="23" height="16" src="../images/watchnow_box_next.png" title="Вперед" /></a></div>';
}

function watchnowboxPrev (link) {
	wnCurrent--;
	if (wnCurrent < 0) wnCurrent = wnArray.length-1;
	watchnowboxUpdate();
	link.blur();
}

function watchnowboxNext (link) {
	wnCurrent++;
	if (wnCurrent >= wnArray.length) wnCurrent = 0;
	watchnowboxUpdate();
	link.blur();
}

function watchnowboxUpdate () {
	document.getElementById('watchnowboxImage').alt = wnArray[wnCurrent][0];
	document.getElementById('watchnowboxLink').href = wnArray[wnCurrent][1];
	document.getElementById('watchnowboxImage').src = wnArray[wnCurrent][2];
}

;
