/**
 * javascripts
 */

function commentopen(boxid)
{
	if( $('commentform')) 
	{
		$('commentform').style['display'] = 'block';
		$('newcomment').innerHTML = '<a href="javascript:;" onclick="commentclose();">Új komment - mégsem</a>';
	}
	return false;
}
function commentclose(boxid)
{
	if( $('commentform')) 
	{
		$('commentform').style['display'] = 'none';
		$('newcomment').innerHTML = '<a href="javascript:;" onclick="commentopen();">Új komment</a>';
	}
	return false;
}

 
function open_offerbox(boxid)
{
	if( $('offerbox_hidden_' + boxid) && $('offerbox_openlink_' + boxid) ) 
	{
		$('offerbox_hidden_' + boxid).style['display'] = 'block';
		$('offerbox_openlink_' + boxid).innerHTML = '<a href="javascript:;" onclick="hide_offerbox(\''+boxid+'\');">elrejt</a>';
	}
	return false;
}
function hide_offerbox(boxid)
{
	if( $('offerbox_hidden_' + boxid) && $('offerbox_openlink_' + boxid) ) 
	{
		$('offerbox_hidden_' + boxid).style['display'] = 'none';
		$('offerbox_openlink_' + boxid).innerHTML = '<a href="javascript:;" onclick="open_offerbox(\''+boxid+'\');">részletek</a>';
	}
	return false;
}
			
// select current option by value
function selectOptionByValue(selObj, val)
{
	var opts= $(selObj).options;
	var len= opts.length;
	while(len)
	{
		if (opts[--len].value== val)
		{
			$(selObj).selectedIndex = len;
			len= 0;
		}
	}
	return;
}

// @param url string
function gotopage(url)
{
	//window.location.href=url;
	
	setTimeout(function(){window.location = url;}, 0);
	return false;
}

// @desc add url to bookmark list
function add_bookmark()
{
	var title="bitartist.net";
	var url="http://bitartist.net";
	if (window.sidebar) window.sidebar.addPanel(title, url,"");
	else if(window.external) window.external.AddFavorite(url,title); 
	else if(window.opera && window.print) return true; 
}

// XML To Array
function parseToXML(result)
{
	var xmlDocument;
	if (!(window.ActiveXObject))
	{
		var responseString = result.responseText;
		var domParser = new DOMParser();
		xmlDocument = domParser.parseFromString(responseString, 'application/xml');
	}
	else xmlDocument = result.responseXML
	return xmlDocument;
}

// get cookie info
function getCookie(name)
{
	var doc_cookie = document.cookie;
	var pref = name + "=";
	var begin = doc_cookie.indexOf("; " + pref);
	if (begin == -1)
	{
		begin = doc_cookie.indexOf(pref);
		if (begin != 0) return null;
	}
	else begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = doc_cookie.length;
	return unescape(doc_cookie.substring(begin + pref.length, end));
}

// set new cookie
function setCookie(name, value, expire, path, domain, secure, expireinhour)
{
	var extra = "";
	var exdate = new Date();
	if(expire)
	{
		if(expireinhour) exdate.setTime(exdate.getTime()+(3600000*expire));
		else exdate.setDate(exdate.getDate()+expire);
	}
	else exdate.setDate(exdate.getDate()+1);
	extra = "; expires=" + exdate.toGMTString() +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = name + "=" + escape(value) + extra;
}

function docHeight()
{
	var scnHei;
	if (self.innerHeight) return scnHei = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) return scnHei = document.documentElement.clientHeight;
	else if (document.body) return scnHei = document.body.clientHeight;
}

/**
 *	@param design integer (1:hidden div, 2: easy popup window)
 *	@param windowWidth integer
 *	@param windowHeight integer 
 *	@param pageTitle string
 *	@param url string
 *	@desc design:1 - hidden div, 2:popup window )
 */
function popupHandler(design, windowWidth, windowHeight, pageTitle, url)
{
	switch(design)
	{
		// hidden div
		default:
		case "1":
			if($("popup_container"))
			{
				//if(document.viewport.getHeight()>document.body.getHeight()) var h=document.viewport.getHeight();
				//else var h=document.body.getHeight();
				
				var scrollH=document.body.scrollHeight;
				var docH = docHeight();
				if(scrollH > docH) var h = scrollH;
				else h = docH;
				
				
				var w=document.viewport.getWidth();
				$('ajaxPopupBackground').style['display']="block";
				$('ajaxPopupBackground').setStyle({height: h+'px',width: w+'px'});

				var toppos=0;
				var leftpos=0;
				if((document.viewport.getScrollOffsets().top+Math.round((document.viewport.getHeight()-windowHeight)/2))>0)
					toppos=document.viewport.getScrollOffsets().top+Math.round((document.viewport.getHeight()-windowHeight)/2);
				if(Math.round((w-windowWidth)/2)>0)
					leftpos=Math.round((w-windowWidth)/2);
				
				$("popup_container").setStyle(
				{
					//display: 'block',
					top: toppos+'px',
					left: leftpos+'px',
					width: windowWidth+'px',
					height: windowHeight+'px'
				});
				
				$("popup_title").innerHTML=pageTitle;
				
				/**
				 * content
				 * content default width: 400px
				 * popup_form: layout_form.css default_width:400px
				 * form_container: html-ben atirni a szelesseget!
				 */
				var contentWidth=windowWidth-20;
				$("popup_content").setStyle({width: contentWidth+'px'});
				
				new Ajax.Updater('popup_content', url, {method:'post', parameters:{}});
				$('popup_container').style['display']="block";
			}
			else return false;
		break;
		
		// popup window
		case "2":
			var newWindow = window.open(url, pageTitle, 'location=0,resizable=0,status=0,titlebar=1,toolbar=0,directories=0,menubar=0,scrollbars=0,width='+windowWidth+',height='+windowHeight);
			newWindow.focus();
		break;
	}
}

/**
 *	@desc close popup
 */
function popupClose()
{
	$('ajaxPopupBackground').style['display']="none";
	$('popup_container').style['display']="none";
	$('popup_content').update('');
}

/**
 *	@param url string
 *	@param pageTitle string
 *	@param windowWidth integer 
 *	@param windowHeight integer
 *	@param isResizable integer (1,0)
 *	@param hasTitlebar integer (1,0)
 *	@param hasToolbar integer (1,0)
 *	@param hasDirectories integer (1,0)
 *	@param hasMenubar integer (1,0)
 *	@param isScrollable integer (1,0)
 *	@param openMiddle integer (1,0) - if 1 open new window in the middle of parent window
 *	@desc open url in new popup window
 */
function popupWindow(url, pageTitle, windowWidth, windowHeight, isResizable, hasTitlebar, hasToolbar, hasDirectories, hasMenubar, isScrollable, openMiddle)
{
	var middle="";
	if(openMiddle=='1')
	{
		var winposhor = (screen.width - windowWidth) / 2;
		var winposver = (screen.height - windowHeight) / 2;
		middle=",top="+winposver+",left="+winposhor;
	}
	var newWindow = window.open(url, pageTitle, 'location=0,resizable='+isResizable+',status=0,titlebar='+hasTitlebar+',toolbar='+hasToolbar+',directories='+hasDirectories+',menubar='+hasMenubar+',scrollbars='+isScrollable+',width='+windowWidth+',height='+windowHeight+middle);
	newWindow.focus();
}
