/*function $(element)
{
    return document.getElementById(element);
}*/
function Pop (a, elID)
{
var outcome = document.getElementById(elID).style.display =
document.getElementById(elID).style.display == "none"
|| document.getElementById(elID).style.display == ""
? "block" : "none";

	return false;
}

function popitup(url)
{
    var w = 500;
    var h = 600;
    var topPos = 50;
    var leftPos = (screen.width - w) / 2;

	newwindow=window.open(url,'name','left=' + leftPos + ',top=' + topPos + ',height=' + h + ',width=' + w + ',scrollbars=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}

function resizeIframe() {

	// Must launched on the body onload event handler for IE
	// Use document.documentElement if you are in Compat mode
	i = parent.document.getElementById(window.name)
	iHeight = document.body.scrollHeight
	i.style.height = iHeight + 5 + "px"
}


function toggleDisp()
{
    for (var i=0;i<arguments.length;i++)
    {
        var d = $(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}
function toggleTab(num,numelems,opennum,animate)
{
    if ($('tabContent'+num).style.display == 'none')
    {
        for (var i=1;i<=numelems;i++)
        {
            if ((opennum == null) || (opennum != i))
            {
                var temph = 'tabHeader'+i;
                var h = $(temph);
                if (!h)
                {
                    var h = $('tabHeaderActive');
                    h.id = temph;
                }
                var tempc = 'tabContent'+i;
                var c = $(tempc);
                if(c.style.display != 'none')
                {
                    if (animate || typeof animate == 'undefined')
                        Effect.toggle(tempc,'blind',{duration:0.5, queue:{scope:'menus', limit: 3}});
                    else
                        toggleDisp(tempc);
                }
            }
        }
        var h = $('tabHeader'+num);
        if (h)
            h.id = 'tabHeaderActive';
        h.blur();
        var c = $('tabContent'+num);
        c.style.marginTop = '0';
        if (animate || typeof animate == 'undefined')
        {
            Effect.toggle('tabContent'+num,'blind',{duration:0.5, queue:{scope:'menus', position:'end', limit: 3}});
        }
        else
        {
            toggleDisp('tabContent'+num);
        }
        
        document.cookie='activeIboxLoginTab='+num+'; expires=01 Jan 2010 01:01:01 UTC;';
    }
}

function readCookie(name)
{
    var cookies = document.cookie.split(';');
    for(var i=0; i<cookies.length; i++)
    {
        var arr = cookies[i].split('=');
        if (arr[0].replace(/^\s+|\s+$/g, '') == name)
        {
            return arr[1];
            break;
        }
    }
    return null;
}

function Ajax(method,onComplete)
{
	this.requestobj = null;
	this.queryStringSeparator = "?";
	this.argumentSeparator = "&";
	this.method = method;
	this.URLString = '';
	this.failed = false;
	
	if (window.XMLHttpRequest)
	{
          // If IE7, Mozilla, Safari, etc: Use native object
          this.requestobj = new XMLHttpRequest()
	}
	else
	{
		if (window.ActiveXObject)
		{
          // ...otherwise, use the ActiveX control for IE5.x and IE6
          this.requestobj = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
        	this.failed = true;
        }
	
	}
	
	
	if (this.failed)
	{
		alert('Failed to create AJAX object');
	}
	
	this.request = function(url,params)
	{
		
		this.urlString= url + this.queryStringSeparator + params;
		if (this.method == "GET") {
			totalurlstring = url + this.queryStringSeparator + params;
			this.requestobj.open(this.method, totalurlstring, true);
			
		} else {
			this.requestobj.open(this.method, url, true);
			try {
				this.requestobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
			} catch (e) { }
			this.URLString=params;
		}
		this.requestobj.onreadystatechange = onComplete;
		
		this.requestobj.send(this.URLString);
	}
	
	
}

function video_prompt(video_source,txtarea)
{
  txtarea = document.getElementById(txtarea);
  switch(video_source)
  {
    case 'youtube':
      text = prompt('Your Youtube URL');
      var opentag = '[youtbe]';
      var closetag = '[/youtbe]';
      
    break;
    case 'googlevideo':
      text = prompt('Your Google Video URL');
      var opentag = '[googlevideo]';
      var closetag = '[/googlevideo]';
    break;
  }
  if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} 
	else 
	{
	  if(txtarea.selectionStart) 
	  {
	    var start = txtarea.selectionStart; 
      var end   = txtarea.selectionEnd;
      txtarea.value = txtarea.value.substr(0, start) 
      + text 
      + txtarea.value.substr(end, txtarea.value.length); 
    }
    else{
		  txtarea.value  += text;
		  txtarea.focus();
    }
	}
}


function mozWrap(txtarea, open, close)
{
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    if (selEnd == 1 || selEnd == 2)
      selEnd = selLength;
     
    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd)
    var s3 = (txtarea.value).substring(selEnd, selLength);
    txtarea.value = s1 + open + s2 + close + s3;
    return;
}

function bbcode_pop(code, txtarea)
{
  
  txtarea = document.getElementById(txtarea);
  var text = get_selection(txtarea);
  codeend = code;
  switch(code)
  {
    case 'youtube':
    case 'googlevideo':
      text = null;
      text = prompt('Въведете URL на видеото');
    break;
    case 'img':
      text = prompt('Въведете URL на изображението. Препоръчителна ширина – дo 400 Pix.');
    break;
    case 'url':
      url = prompt('Въведете URL','http://');
      if (url)
      {
        text = prompt('Име на връзката',text);
      }
      else{
        text = '';
      }
      code = 'url='+url+'';
    break;
  }
  
  if (!text) return;
  
  text = '['+code+']'+ text + '[/'+codeend+']';
  
  
  if (txtarea.createTextRange && txtarea.caretPos) 
  {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} 
	else 
	{
	  if(txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	  {
	    /*var open = '['+code+']';
	    var close = '[/'+codeend+']';
	    mozWrap(txtarea, open, close)*/
	    
	    var start = txtarea.selectionStart; 
      var end   = txtarea.selectionEnd;
      txtarea.value = txtarea.value.substr(0, start) 
      + text 
      + txtarea.value.substr(end, txtarea.value.length); 
      
    }
    else{
		  txtarea.value  += text;
		  txtarea.focus();
    }
	}
}

function get_selection(txtarea)
{
  if (txtarea.createTextRange && txtarea.caretPos) 
  {
		text = txtarea.caretPos;
	} 
	else 
	{
	  if(txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	  {
	    var start = txtarea.selectionStart; 
      var end   = txtarea.selectionEnd;
      len = end - start;
      text = txtarea.value.substr(start, len)
    }
    else
    {
		  text = '';
    }
	}
	
	return text
}

function change_user_status()
{
  txt = $('user_status').value;
  var ajax = new Ajax('POST',onComplete=function()
		{
			switch(ajax.requestobj.readyState)
			{
				case 4:
				  status = ajax.requestobj.responseText;
				  document.getElementById('status').innerHTML=status;
					document.getElementById('status_edit').style.display='none';
				break;
			}
		}
	);
	ajax.request('/profile/set_status.php','status='+txt);
}


function emoticon(text) 
{
  	var txtarea = document.post.description;
  	text = ' ' + text + ' ';
  	if (txtarea.createTextRange && txtarea.caretPos) {
  		var caretPos = txtarea.caretPos;
  		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
  		txtarea.focus();
  	} else {
  		txtarea.value  += text;
  		txtarea.focus();
  	}
}

function updateContent(e,content)
{
  $(e).innerHTML = content;
}

var panes = new Array();

function setupPanes(containerId, defaultTabId) {
  // go through the DOM, find each tab-container
  // set up the panes array with named panes
  // find the max height, set tab-panes to that height
  panes[containerId] = new Array();
  var maxHeight = 0; var maxWidth = 0;
  var container = document.getElementById(containerId);
  var paneContainer = container.getElementsByTagName("div")[0];
  var paneList = paneContainer.childNodes;
  for (var i=0; i < paneList.length; i++ ) {
    var pane = paneList[i];
    if (pane.nodeType != 1) continue;
    if (pane.offsetHeight > maxHeight) maxHeight = pane.offsetHeight;
    if (pane.offsetWidth  > maxWidth ) maxWidth  = pane.offsetWidth;
    panes[containerId][pane.id] = pane;
    pane.style.display = "none";
  }
    paneContainer.style.height = maxHeight + "px";
    paneContainer.style.width  = maxWidth + "px";
    document.getElementById(defaultTabId).onclick();
}

function showPane(paneId, activeTab) {
  // make tab active class
  // hide other panes (siblings)
  // make pane visible
  
    for (var con in panes) {
    activeTab.blur();
    activeTab.className = "tab-active";
    if (panes[con][paneId] != null) { // tab and pane are members of this container
      var pane = document.getElementById(paneId);
      pane.style.display = "block";
      var container = document.getElementById(con);
      var tabs = container.getElementsByTagName("ul")[0];
      var tabList = tabs.getElementsByTagName("a")
      for (var i=0; i<tabList.length; i++ ) {
        var tab = tabList[i];
        if (tab != activeTab) tab.className = "tab-disabled";
      }
      for (var i in panes[con]) {
        var pane = panes[con][i];
        if (typeof pane == "function") continue;
        if (pane == undefined) continue;
        if (pane.id == paneId) continue;
        pane.style.display = "none"
      }
    }
  }
  return false;    
}

//End tabs

/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
// 2008-01-28T20:24:17Z
function prettyDate(time){
  
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
  
	
	if ( isNaN(day_diff) || day_diff < 0  )
		return 'Сега';
		
	if (day_diff >= 7)
	{
	  return date.getDate()+'.'+(date.getMonth()+1)+'.'+date.getFullYear()+', '+date.getHours()+':'+date.getMinutes();
	}
	
	  if (day_diff == 0 && diff < 60)
	    return "Сега";
	  else if(day_diff == 0 && diff < 120)
	    return "Преди 1 минута";
	  else if(day_diff == 0 && diff < 3600)
	    return "Преди "+Math.floor( diff / 60 ) + " минути";
	  else if(day_diff == 0 && diff < 7200)
	    return "преди час";
	  else if(day_diff == 0 && diff < 86400)
	    return "Преди " + Math.floor( diff / 3600 ) + " часа";
	  else if (day_diff == 1)
	    return "Вчера";
	  else if(day_diff < 7)
	    return "Преди "+day_diff+" дни";
	  else if(day_diff < 31)
	    return "Преди " + Math.ceil( day_diff / 7 ) + " седмици";
}

function Collection(div_id, images)
{
    this.block_id        = div_id.split('-')[0];
    this.block_div_id    = div_id;
    this.el_image        = document.getElementById('image-collection-' + this.block_div_id);
    this.el_description  = document.getElementById('description-' + this.block_div_id);
    this.el_counter      = document.getElementById('collection-counter-' + this.block_div_id);
    this.images          = images;
    this.current_index   =  0;

    this.getNextImage = function()
    {
        var temp = new Image();
        var img = new Image();

        if(arguments.length)
        {
            switch (arguments[0])
            {
                case 'forward':
                    temp = this.images[this.current_index + 1] || this.images[0];
                    break;

                case 'back':
                    temp = this.images[this.current_index - 1] || this.images[this.images.length - 1];
                    break;
            }
        }
        else
        {
            temp = this.images[current_index + 1] || this.images[current_index];
        }

        img.src = temp.src;
        img.width = temp.width;
        img.height = temp.height;
        img.description = temp.description;
        img.key_index = temp.key_index;

        return img;
    };

    this.next_image      = this.getNextImage('forward');
    this.previous_image  = this.getNextImage('back');

    this.show = function(direction)
    {
        switch(direction)
        {
            case 'next':
                this.el_image.src             = this.next_image.src;
                this.el_image.width           = this.next_image.width;
                this.el_image.height          = this.next_image.height;
                this.current_index            = this.next_image.key_index;
                this.el_description.innerHTML = this.next_image.description;
                this.el_counter.innerHTML = this.current_index + 1 + '/' + this.images.length;
                break;

            case 'previous':
                this.el_image.src             = this.previous_image.src;
                this.el_image.width           = this.previous_image.width;
                this.el_image.height          = this.previous_image.height;
                this.current_index            = this.previous_image.key_index;
                this.el_description.innerHTML = this.previous_image.description;
                this.el_counter.innerHTML = this.current_index + 1 + '/' + this.images.length;
                break;
        }

        this.next_image     = this.getNextImage('forward');
        this.previous_image = this.getNextImage('back');

        return false;
    };

    this.onclick = function()
    {
		var w = 500;
		var h = 500;
		var topPos = (screen.height - h) / 2;
		var leftPos = (screen.width - w) / 2;

		newwindow=window.open(this.images[this.current_index].href,'name','left=' + leftPos + ',top=' + topPos + ',height=' + h + ',width=' + w + ',scrollbars=yes,resizable=yes');
		if (window.focus) {newwindow.focus()}
    	return false;
    }

}