var isIE=document.all?true:false;

function HiddenScroll(e,obj){ // dir (horisontal,vertical,both), follow (true/false if you want to folow mouse or go oposite way)
	var dir = (HiddenScroll.arguments.length > 2)? HiddenScroll.arguments[2] : "both";
	var follow = (HiddenScroll.arguments.length > 3)? HiddenScroll.arguments[3] : false;

	if(dir == "vertical" || dir == "both"){
		var objScrollHeight = obj.scrollHeight
		var objMouseY = e.clientY - 2 - getOffsetTop(obj) + scrollT
		var percentY = parseFloat((objMouseY / (parseFloat(obj.offsetHeight)-1)) * 100)
		percentY = follow? 100 - percentY : percentY
		
		//obj.scrollTop = ((objScrollHeight - parseFloat(obj.offsetHeight)) / 100) * percentY
		VitaminXP.Animation.Animate(obj, {scrollTop:{to:((objScrollHeight - parseFloat(obj.offsetHeight)) / 100) * percentY}}, 0.5, 'EaseBoth')
	}
	
	if(dir == "horisontal" || dir == "both"){
		var objScrollWidth = obj.scrollWidth
		var objMouseX = e.clientX - 2 - getOffsetLeft(obj)
		var percentX = parseFloat((objMouseX / (parseFloat(obj.offsetWidth)-1)) * 100)
		percentX = follow? 100 - percentX : percentX
		
		//obj.scrollLeft = ((objScrollWidth - parseFloat(obj.offsetWidth)) / 100) * percentX
		VitaminXP.Animation.Animate(obj, {scrollLeft:{to:((objScrollWidth - parseFloat(obj.offsetWidth)) / 100) * percentX}}, 0.5, 'EaseBoth')
	}
	
	//window.status = "Hidden Scroll: X=" + percentX + "%, Y=" + percentY + "%"
}

function URLtoURI(url){
	url = url.replace(RegExp("\\\\","g"),"/");
	return encodeURI(url);
}

// AJAX Functions NEXT
var http_request = false;

function ajax(obj,url){ // Object,URL,Parameters,Method,Action,Function Name
	var args = ajax.arguments;
	var parameters = (args.length > 2)? args[2] : '';
	var method = (args.length > 3)? args[3] : "GET";
	var act = (args.length > 4)? args[4] : "html";
	var func = (args.length > 5)? args[5] : "";
	
    var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	}else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = function(){processStateChange(http_request,obj,act,func)};
	http_request.open(method, url, true);
	//http_request.setRequestHeader("Content-type", "text/html; charset=windows-1251");
	http_request.setRequestHeader("Content-type", "text/html; charset=utf-8");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
	return true
}

var ready_state = new Array("uninitialized","loading","doaded","interactive","complete")

function processStateChange(http_request,obj){
	var act = (processStateChange.arguments.length > 2)? processStateChange.arguments[2] : "html";
	var func = (processStateChange.arguments.length > 3)? processStateChange.arguments[3] : "";

	window.status = 'Ajax: '+ready_state[http_request.readyState];
	if(http_request.readyState == 4){ // Complete
		if(http_request.status == 200 || http_request.status == 0){ // OK response
			if(act == "html"){
				obj.innerHTML = http_request.responseText;
			}else if(act == "text"){
				obj.innerText = http_request.responseText;
			}else if(act == "function"){
				eval(func)(http_request.responseText,obj)
			}else if(act == "value"){
				obj.value = http_request.responseText
			}else if(act == "link"){
				if(http_request.responseText == "OK"){
					window.location = func
				}else{
					alert(http_request.responseText)
				}
			}else if(act == "ok"){
				if(http_request.responseText == "OK"){
					if(func != ""){
						eval(func)(obj)
					}
				}else{
					alert(http_request.responseText)
				}
			}
			http_request = null
		}else{
			window.status = 'Ajax: problem ' + http_request.statusText + ', status: ' + http_request.status
		}
	}
}
// END AJAX

//General Functions
function DomainName(){
	var start = location.hostname.lastIndexOf(".",location.hostname.length-5);
	if(start >= 0){
		dname = location.hostname.substr(start,location.hostname.length-start)
	}else{
		dname = "."+location.hostname
	}
	return dname.toString();
}

function email(email){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
		return true
	}else{
		return false
	}
}

//COOKIE FUNCTION
function GetCookie(name){
	all = document.cookie.split("; ")
	var allcookies = new Array();
	for(i=0;i<all.length;i++){
		allcookies[i] = all[i].split("=")
		if(name.toLowerCase() == allcookies[i][0].toLowerCase()){
			allcookies[i][1] = unescape(allcookies[i][1])
			return allcookies[i][1]
		}
	}
	return "undefined";
}

function SetCookie(one,two){
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var days = (argc > 2)? argv[2] : 365;
var expires = new Date ();
expires.setTime (expires.getTime() + (24 * 60 * 60 * 1000 * days));
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : DomainName();
var secure = (argc > 5) ? argv[5] : false;
document.cookie = one + "=" + escape (two) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}
// END COOKIE

// Select Element Functions
function addElement(objSelCtrl, strText, strValue, selected){
	var objOptions = objSelCtrl.options;
	var intOptionLength;
	if (objOptions.length < 0){ //IE for Mac 4.5 sets length to -1 if list is empty
		intOptionLength = 0;
	}else{
		intOptionLength = objOptions.length;        
	}
	objOptions[intOptionLength] = new Option(strText, strValue);

	var argv = addElement.arguments;
	var argc = addElement.arguments.length;
	if(argc > 3 && argv[3] == true){
		objOptions[intOptionLength].selected = argv[3]
		//objOptions[intOptionLength].style.background = "#C0DDFA"
	}
}

function ClearElements(what){var walength = what.options.length;for (i = walength; i >= 0; i--){what.options[i] = null}}
// End Select Element options

function PlayCD(id){
	var url='http://enjoymp3s.com/player/?id='+id
	var w=350
	var h=350
	var le=420
	var to=10
	setup='toolbar=no,location=no,directories=no,status=no,left='+le+',top='+to+',menubar=no,width='+w+',height='+h
	setup += 'scrollbars=no,resizable=no'
	window.open(url,"playcd",setup)
}

function open_window(url,id,width,height){ // Additional args: 5) left, 6) top.
	var args = open_window.arguments
	
	var left = (args.length > 4)? args[4] : (screen.availWidth/2)-(width/2)
	var top = (args.length > 5)? args[5] : (screen.availHeight/2)-(width/2)
	setup = 'toolbar=no,location=no,directories=no,status=no,left='+left+',top='+top+',menubar=no,width='+width+',height='+height
	setup += 'scrollbars=no,resizable=no'
	window.open(url,id,setup)
}

function SetOpacity(obj,val){
	if (isIE && typeof obj.style.filter == 'string') {
		obj.style.filter = 'alpha(opacity='+val+')'
		if (!obj.currentStyle || !obj.currentStyle.hasLayout) {
			obj.style.zoom = 1; // when no layout or cant tell
		}
	}else{
		obj.style.opacity = val/100;
		obj.style['-moz-opacity'] = val/100;
		obj.style['-khtml-opacity'] = val/100;
	}
	
}

function str2arr(str,seperator){
	arr = new Array()
	items = str.split("seperator")
	for(r=0;r<items.length;r++){
		temp = items[r].split("=")
		arr[temp[0]] = temp[1]
	}
	
	return arr
}

function GetOpacity(obj){
	if(isIE && typeof obj.style.filter == 'string'){
		filter = obj.style.filter
		filter = filter.replace("alpha(","")
		filter = filter.replace(")","")
		
		filters = str2arr(filter,",")
		
		opacity = filters['opacity']
		opacity = (opacity == undefined)? 100 : opacity
	}else{
		opacity = 100
	}
	
	return parseFloat(opacity)
}

function positive(num){
	return (num >= 0)? num : num * -1;
}

var fade_working = new Array()

function fade(element_id){
	var args = fade.arguments;
	if(args.length > 2){
		from = (isIE)? GetOpacity(eval(element_id)) : args[1]
		to = args[2]
		fade_working["'"+element_id+"'"] = new Array(from,to)
	}else{
		from = fade_working["'"+element_id+"'"][0]
		to = fade_working["'"+element_id+"'"][1]
	}
	//var round_time = (args > 3)? args[3] : 1; // Seconds
	step = 2
	round_time = 20
	
	var obj = document.getElementById(element_id)
	SetOpacity(obj,from)
	
	if(from != to){
		dif = positive(from - to)
		if(from > to){
			from -= (dif < step)? incr : step
		}else if(from < to){
			from += (dif < step)? incr : step
		}
		
		fade_working["'"+element_id+"'"][0] = from
		//return setTimeout('fade(\''+element_id+'\','+from+','+to+')',50)
		return setTimeout('fade(\''+element_id+'\')',round_time)
	}else{
		return from
	}
}

AnimateMe = function(element_id,attributes){
	var args = 	AnimateMe.arguments
	var type = (args.length > 2)? args[2] : "easeBothStrong"
	
	if(type == "easeBothStrong"){
		var anim = new YAHOO.util.Anim(element_id, attributes, 0.8, YAHOO.util.Easing.easeBothStrong);
	}else if(type == "easeBoth"){
		var anim = new YAHOO.util.Anim(element_id, attributes, 0.8, YAHOO.util.Easing.easeBoth);
	}
	
	anim.animate()
};

function RefreshPage()
{
	if(isIE){
		history.go(0)
	}else{
		window.location.reload(true)
	}
}

