var screen_y=0, screen_x=0;

function page_display(id, height, width) 
{
 var win = window.open('/popup_page.php?id=' + id + '', id, 'height='+height+',width='+width+',toolbar=0,location=0');
}


function show_hide_menu(id)
{
if (document.getElementById(id).style.display=='none')
	document.getElementById(id).style.display='block';
else
	document.getElementById(id).style.display='none';
}


function show_hide(id, show)
{

if (show==1)
	document.getElementById(id).style.display='block';

else if (show==2)
	document.getElementById(id).style.display='none';

else 
{
if (document.getElementById(id).style.display=='none')
	document.getElementById(id).style.display='block';
else
	document.getElementById(id).style.display='none';
}


}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}



function MM_validateForm() { //v4.0
  if (document.getElementById){
    var name_nm,i,p,q,nm,test,num,min,max,id,errors='',args=MM_validateForm.arguments;

    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]); id=args[i]; name_nm=args[i+1];
      if (val) { if (name_nm!='') nm=name_nm; else nm=val.name; document.getElementById(id+'_label').className='label'; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); 
          if (p<1 || p==(val.length-1)) { errors+='- "'+nm+'" trebuie sa contina o adresa e-mail.\n';  document.getElementById(id+'_label').className='rlabel';}
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- "'+nm+'" must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') { errors += '- Completati "'+nm+'".\n'; document.getElementById(id+'_label').className='rlabel';} }

    } 

if (document.getElementById('txtEmail2'))
{
	var email1 = document.getElementById('txtEmail').value;
	var email2 = document.getElementById('txtEmail2').value;
	if (email1!=email2) {
		errors += '- Email-urile nu coincid.\n';
		document.getElementById('txtEmail_label').className='rlabel';
		document.getElementById('txtEmail2_label').className='rlabel';
	}
}

   if (errors) alert('Va rugam completati cu valorile corespunzatoare:\n'+errors);

    document.MM_returnValue = (errors == '');
}
}


function set_main_picture(id,picture){
	document.getElementById(id).innerHTML='<div style="background-image:url('+picture+'); height:356px; width:485px; -moz-border-radius: 10px;"></div>';
}

function active_menu(id, img){
	document.getElementById(id).src=img;
}


function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("It's about time to upgrade you browser don't you think?");
	}
}


function refresh() {
	if (getReq.readyState == 4 || getReq.readyState == 0) {
		var rand=Math.random();
		getReq.open("GET", '/update_captcha.php?'+rand, true);
		getReq.onreadystatechange = captcha_request;
		getReq.send(null);
	}
}



function captcha_request()
{
if (getReq.readyState == 4) {
		var ss = document.getElementById('captcha_img');
		ss.innerHTML = '';
		ss.innerHTML = getReq.responseText;
			}
}

var getReq = getXmlHttpRequestObject();
var old_gallery=0;
var gallery_height=550;
var max_height=654;
var min_height=250;

function set_gallery(id, height)
{
	if (getReq.readyState == 4 || getReq.readyState == 0) {
                var rand  = Math.random();
		document.getElementById('main_gallery').innerHTML='<div style="padding-top:300px; padding-left:360px"><img src="/templates/default/images/loading.gif" alt="Loading ..."/></div>';
		set_resolution(220);
		if (old_gallery)
		document.getElementById('left_menu_gallery_'+old_gallery).className='';
		old_gallery=id;
		document.getElementById('left_menu_gallery_'+id).className='select_gallery_a';
		getReq.open("GET", '/gallery_ajax.php?gallery='+id+'&height='+gallery_height+'&rand='+rand, true);
		getReq.onreadystatechange = get_gallery;
		getReq.send(null);
	}
}

function get_gallery()
{
	if (getReq.readyState == 4) {
		document.getElementById('main_gallery').innerHTML=getReq.responseText;
		window.addEvent('domready', function(){				
			makeScrollbar( $('main_gallery'), $('scrollbar2'), $('handle2'), true);
		});
	}
}



function set_press(id)
{
	if (getReq.readyState == 4 || getReq.readyState == 0) {
                var rand  = Math.random();
		document.getElementById('main_gallery').innerHTML='<div style="padding-top:300px; padding-left:360px"><img src="/templates/default/images/loading.gif" alt="Loading ..."/></div>';
		set_resolution(220);
		if (old_gallery)
		document.getElementById('left_menu_gallery_'+old_gallery).className='';
		old_gallery=id;
		document.getElementById('left_menu_gallery_'+id).className='select_gallery_a';
		getReq.open("GET", '/press_ajax.php?gallery='+id+'&height='+gallery_height+'&rand='+rand, true);
		getReq.onreadystatechange = get_press;
		getReq.send(null);
	}
}


function get_press()
{
	if (getReq.readyState == 4) {
		document.getElementById('main_gallery').innerHTML=getReq.responseText;
		window.addEvent('domready', function(){				
			makeScrollbar( $('main_gallery'), $('scrollbar2'), $('handle2'), true);
		});
	}
}



function get_resolution()
{

	var ns=(document.layers);
	var ie=(document.all);
	var ns6=(document.getElementById&&!document.all);

	if (ie){
		if(document.body && document.body.scrollTop)
		{
			screen_x=document.body.clientWidth;
			screen_y=document.body.clientHeight;
		}
		else
		{
			screen_x=document.documentElement.clientWidth;
			screen_y=document.documentElement.clientHeight;
		}
	}
	else if (ns||ns6){
		screen_x=window.innerWidth;
		screen_y=window.innerHeight;
	}

}


function set_resolution(val_left)
{
        var width=0;
	get_resolution();
	if (document.getElementById('content-left').style.width=='') document.getElementById('content-left').style.width='155px';
	width=screen_x-parseInt(document.getElementById('content-left').style.width)-val_left;
	if (width==NaN) width=910;
	if (width<700) width=700;
	var width_scroll=width;

        var height=screen_y;
	height=height-240;
	if(height>max_height) height=max_height;
	if(height<min_height) height=min_height;
	gallery_height=height-10;

	document.getElementById('main_gallery').style.width=width+'px';
	document.getElementById('main_gallery').style.height=height+'px';

	document.getElementById('scrollbar2').style.width=width_scroll+'px';

	setTimeout("set_resolution('"+val_left+"')",100);
}



function align_page(width)
{
	get_resolution();

	var div_width=parseInt((screen_x-width)/2);

	if (div_width<0) div_width=0;

	if (document.getElementById('content-left'))
	document.getElementById('content-left').style.width=div_width+'px';

	if (document.getElementById('content-left-hp'))
	document.getElementById('content-left-hp').style.width=div_width+'px';

	if (document.getElementById('bottom-left'))
	document.getElementById('bottom-left').style.width=div_width+'px';

        var height=screen_y;
	height=height-220;

	if(height>max_height) height=max_height;
	if(height<min_height) height=min_height;


	document.getElementById('main_content').style.height=height+'px';
	setTimeout("align_page('"+width+"')",100);
}


function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}