var is_ie = (document.all && document.getElementById);
var is_moz = (!document.all && document.getElementById);
var is_opera = (navigator.userAgent.indexOf("Opera") > -1);


function hide(element) {$(element).style.display = 'none';return element; }
function show(element) {$(element).style.display = '';return element; }

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
};

Object.extend = function(destination, source) {
	for (var property in source) destination[property] = source[property];
	return destination;
};

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
};

if (!Array.prototype.forEach){
	Array.prototype.forEach = function(fn, bind){
		for(var i = 0; i < this.length ; i++) fn.call(bind, this[i], i);
	};
}

Array.prototype.each = Array.prototype.forEach;

String.prototype.camelize = function(){
	return this.replace(/-\D/gi, function(match){
		return match.charAt(match.length - 1).toUpperCase();
	});
};

var $A = function(iterable) {
	var nArray = [];
	for (var i = 0; i < iterable.length; i++) nArray.push(iterable[i]);
	return nArray;
};

function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
};

if (!window.Element) var Element = {};

Object.extend(Element, {
	  hide: function(element) {
		$(element).style.display = 'none';
		return element;
	  },
	
	  show: function(element) {
		$(element).style.display = '';
		return element;
	  },
	remove: function(element) {
		element = $(element);
		element.parentNode.removeChild(element);
	},

	hasClassName: function(element, className) {
		element = $(element);
		return !!element.className.match(new RegExp("\\b"+className+"\\b"));
	},

	addClassName: function(element, className) {
		element = $(element);
		if (!Element.hasClassName(element, className)) element.className = (element.className+' '+className);
	},

	removeClassName: function(element, className) {
		element = $(element);
		if (Element.hasClassName(element, className)) element.className = element.className.replace(className, '');
	}
});

document.getElementsByClassName = function(className){
	var elements = [];
	var all = document.getElementsByTagName('*');
	$A(all).each(function(el){
		if (Element.hasClassName(el, className)) elements.push(el);
	});
	return elements;
};

//AJAX FUNCTIONS
//based on prototype's ajax class
//to be used with prototype.lite, moofx.mad4milk.net.

ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};


function clearSelectBox(selectbox) {
    var formObj = document.getElementById(selectbox);
    formObj.options.length = 0;  
}




function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function isNumber(field) {
	var re = /^[0-9-'.'-',']*$/;
	if (!re.test(field.value)) {
		field.value = field.value.replace(/[^0-9-'-',']/g,"");
	}
}

function isLatin(field) {
	var re = /^[0-9-'.'-',']*$/;
	if (!re.test(field.value)) {
		field.value = field.value.replace(/[^0-9a-zA-Z-'-',']/g,"");
	}
}



function comment_fadein_text(groupid) {
    //comment_unloading_text();
    if(is_ie) {
        document.getElementById('gallery-'+gallery_groupid).style.display='block';    
        comment_fadein_opacity = 100;
    }
    if(comment_fadein_opacity < 100) {
        comment_fadein_opacity += 10;
        if(is_opera)  { 
            comment_fadein_opacity = 100;
        } else     
        if(is_ie) {
            document.getElementById('gallery-'+gallery_groupid).filters.alpha.opacity = comment_fadein_opacity; 
        } else     if(is_moz) {
            document.getElementById('gallery-'+gallery_groupid).style.MozOpacity = (comment_fadein_opacity/100);
        }
        setTimeout("comment_fadein_text()", 100); 
    } else {
        comment_fadein_opacity = 100;
        is_being_commented = false;  
    }
}
 
// Process Comment

function ProcessAjax(mod,groupid,page) 
{  
	postBody = '&dummy='+(new Date().getTime())+'&groupid='+groupid+'&mod='+mod+'&lang='+currentlang+'&page='+page;
	new ajax ('/ajax.php', {postBody: postBody, update: $(mod+'-'+groupid)});
}

function submitCity( form)
{
	if (form.city.value != '')
		form.action = form.action + '/city-'+form.city.value+'/';
	return true;
}


function preSubmitSearch(langprefix, text)
{
	var pattern = /[\//'\\\/*\(\)\[\]]/ig;
	var str = text.replace(pattern,'');
	$('search').value = str;
	if (str.length<3) {
		//$('serr').innerHTML = cmnlang['shortsearch'];
		alert(cmnlang['shortsearch']);
	} else {
		$('searchform').action = langprefix+'/search?q='+str;
		return true;
	}
	return false;
}

function submitFilmSearch( form)
{
	if (form.year.value != 0)
		form.action = form.action + '/year-'+form.year.value;
	if (form.genre.value != 0)
		form.action = form.action + '/genre-'+form.genre.value;		
	return true;
}

function preSubmitMainSearch(text) {
	var pattern = /[\//'\\\/*\(\)\[\]]/ig;
	var str = text.replace(pattern,'');
	$('mainsearch').value = str;
	if (str.length<3) {
		$('serr').innerHTML = cmnlang['shortsearch'];
	} else {
		//$('sform').action = '{$langprefix}/search?q='+str;
		//alert('{$langprefix}/search?q='+str);
		return true;
		//$('sform').submit();
	}
	return false;
}


 
function checkemail(str)
{
    var testresults
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
        testresults=true
    else
    {
        testresults=false
    }
    return (testresults)
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

		
		function redirectregion(urlprefix, val)
		{
			if (val!=0)
				window.location=urlprefix+'/region-'+val+'/';
			else
				window.location=urlprefix+'/';
		}

