function __id(sI){
    return document.all ? document.all[sI] : document.getElementById(sI);
}
__getClass = document.getElementsByClassName ? function(classList, node) {
        return (node || document).getElementsByClassName(classList)
}
: function(classList, node) {
		var node = node || document,
		list = node.getElementsByTagName('*'),
		length = list.length,
		classArray = classList.split(/\s+/),
		classes = classArray.length,
		result = [], i,j
		for(i = 0; i < length; i++) {
			for(j = 0; j < classes; j++)  {
				if(list[i].className.search('\\b' + classArray[j] + '\\b') != -1) {
					result.push(list[i])
					break
				}
			}
		}

		return result
	};

function __tag(sT,oN){
    sT=sT||'*';
    oN=oN||document;
    return oN.getElementsByTagName(sT);
}
function __name(sN){
    return document.getElementsByName(sN);
}
function CornersInit() {
	var corners = __getClass('corners');
	for (var i = 0; i < corners.length; i++) {
		var em1 = document.createElement("em"); em1.setAttribute("class", "tl"); corners[i].appendChild(em1);
		var em2 = document.createElement("em"); em2.setAttribute("class", "tr"); corners[i].appendChild(em2);
		var em3 = document.createElement("em"); em3.setAttribute("class", "bl"); corners[i].appendChild(em3);
		var em4 = document.createElement("em"); em4.setAttribute("class", "br"); corners[i].appendChild(em4);
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = [],
            els = __tag(tag,node),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (var i = 0, j = 0; i < elsLen; i++) 
		if (pattern.test(els[i].className) ) 
			classElements[j++] = els[i];
	return classElements;
}

function showSubmenu(catid, obj) {
	var pos=0,
            blocks=$('*[name=subcat]'),
            i=0,
            padding='';

	if (obj!='') {
		var curr=__id('cat-li-curr');
		if (curr) {
			curr.removeAttribute('style');
			curr.removeAttribute('id');
		}
		obj.setAttribute('style', 'background-color:#1a87c6;');
		obj.setAttribute('id', 'cat-li-curr');
		obj.style.backgroundcolor='#1a87c6;';
		pos=findPosX(obj);
	}

	while (blocks.length > i) {
		if(__id('cat'+i)) {
			__id('cat'+i).removeAttribute('style');
		}
                i++;
	}
        var hCat = __id(catid);
        if(!hCat) return

        var width=hCat.offsetWidth;
        var max=findPosX(__id('last-menu'))+__id('last-menu').offsetWidth;
        hCat.setAttribute('style', 'display:block;');
	if (pos) {
                hCat.setAttribute('style', 'display:block;');
                /*alert('pos='+pos+' width='+width+' max='+max);*/
                if ((pos+width)>max) {
                        var diff=(pos+width)-max,
                            pos=pos-diff,
                            padding = ' padding-left:14px;';
                }
		hCat.setAttribute('style', 'display:block; left:'+pos+'px;'+padding);
	} else {
		var pos=findPosX(__id('cat-li-curr'));
		/*alert('pos='+pos+' width='+width+' max='+max);*/
		if ((pos+width)>max) {
			var diff=(pos+width)-max,
                            pos=pos-diff,
                            padding = '14px';
		}
		hCat.style.display = 'block';
		hCat.style.left = pos+'px';
                if(padding)
                  hCat.style.paddingLeft = padding;
	}
}

function findPosX(obj)
{
  var curleft = 0;
  if(!obj) return 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function checkURL(obj, cid, pid, id) {
  var value = obj.value;
  document.getElementById(cid).innerHTML='checking...';
 $.post("/", {publisher: "newsurl", url: value, pid: pid, id:id}, function(data){
   document.getElementById(cid).innerHTML=data;
 });
}
function checkDomain(value, cid, pid) {
 $.post("/", {publisher: "jquery", domain: value, pid: pid}, function(data){
   document.getElementById(cid).innerHTML=data;
 });
}
var a = 100;
function addDomain(pid, name) {
	var inputElm = document.createElement('input'),
            parent = __id(name),
            span = document.createElement('span'),
            href = document.createElement('a'),
            div = document.createElement('div');
	inputElm.setAttribute("type", "text");
	inputElm.setAttribute("name", name+"[]");
	inputElm.setAttribute("id", "domain"+a);
	inputElm.setAttribute("onchange", "checkDomain(this.value, 'dom"+a+"', '"+pid+"')");
	parent.appendChild(inputElm);
	span.setAttribute("id", "dom"+a);
	href.setAttribute("href", "#");
	href.setAttribute("id", "href"+a);
	href.setAttribute("onClick", "delDomain('"+a+"', '"+name+"'); return false;");
	div.setAttribute("class", "cc");
	parent.appendChild(span);
	parent.appendChild(href);
	__id('href'+a).innerHTML='X';
	parent.appendChild(div);
	a+=1; 
}
var b = 100;
function addFilters() {
	var inputElm = document.createElement('input'),
            parent = __id('filters'),
            span = document.createElement('span'),
            href = document.createElement('a'),
            div = document.createElement('div');
	inputElm.setAttribute("type", "text");
	inputElm.setAttribute("name", "filters[]");
	inputElm.setAttribute("id", "filter"+b);
	parent.appendChild(inputElm);
	span.setAttribute("id", "filt"+b);
	href.setAttribute("href", "#");
	href.setAttribute("id", "hreffilt"+b);
	href.setAttribute("onClick", "delFilter('"+b+"'); return false;");
	div.setAttribute("class", "cc");
	div.setAttribute("id", "filtdiv"+b);
	parent.appendChild(span);
	parent.appendChild(href);
	__id('hreffilt'+b).innerHTML='X';
	parent.appendChild(div);
	b+=1;
}
function delDomain(a, name) {
	var d = __id(name),
            input = __id('domain'+a),
            href = __id('href'+a),
            span = __id('dom'+a);
	d.removeChild(input);
	d.removeChild(href);
	d.removeChild(span);
}
function delFilter(a) {
	var d = __id('filters'),
            input = __id('filter'+a),
            href = __id('hreffilt'+a),
            span = __id('filt'+a),
            div = __id('filtdiv'+a);
	d.removeChild(input);
	d.removeChild(href);
	d.removeChild(span);
	d.removeChild(div);
}

 function switchCdiv(id) {
       $('#cDiv'+id).toggle();
 }

 function showCityDiv(id) {
  var oDiv = $('#cityDiv'+id);
   if(oDiv.html()) {
     oDiv.show();
     oDiv.css({
       'position':'absolute',
       'background-color':'#eee',
       'border':'1px solid #666',
       'padding-left':'2px'
      });
   } else oDiv.hide();
 }
 function hideCityDiv(id) {
   $('#cityDiv'+id).hide();
 }
var nNeedCrop = true;
function cropInit(src){
  if(nNeedCrop) {
    nNeedCrop = false;
    jQuery(function($){
	    // The variable jcrop_api will hold a reference to the
	    // Jcrop API once Jcrop is instantiated.
	    var jcrop_api;

	    // In this example, since Jcrop may be attached or detached
	    // at the whim of the user, I've wrapped the call into a function
	    initJcrop();
	    // The function is pretty simple
	    function initJcrop()//{{{
	    {
	      // Hide any interface elements that require Jcrop
	      // (This is for the local user interface portion.)
	      $('.requiresjcrop').hide();
	      // Invoke Jcrop in typical fashion
	       $('#crop_src').Jcrop({
		boxWidth:400,
		aspectRatio	  :1,
		onChange	  :showCoords,
		onSelect	  :showCoords
	      },function(){
		jcrop_api = this;
		$('#crop_state').show();
		$('#crop_state').attr('value','cancel crop');
		$('#x1').unbind('click');
		$('#crop_state').unbind('click');
		$('#x1').click(function(e) {
		    // This button is visible when Jcrop has been destroyed
		    // It performs the re-attachment and updates the UI
		    var src = $('#crop_src').attr("src");
		    $('#crop_state').show();
		    jcrop_api.setImage(src);
		    $('.requiresjcrop').show();
		    return false;
		});
		$('#crop_state').click(function(e) {
		    // This button is visible when Jcrop has been destroyed
		    // It performs the re-attachment and updates the UI
		    var crop_state = $('#crop_state').attr('value');
		    if(crop_state == 'crop') {
		      initJcrop();
		      $('#crop_state').attr('value','cancel crop');
		    }else{
		      $('#crop_state').attr('value','crop');
		      jcrop_api.destroy();
		      $('.requiresjcrop').hide();
		    }
		    return false;
		});

	      });

	  };
    });
  }
  $('#crop_src').attr("src", src);
  $('#x1').click();
}


function showCoords(c)
{
  $('#x1').val(c.x);
  $('#y1').val(c.y);
  $('#w').val(c.w);
  $('#h').val(c.h);
};


function addSourceFilter() {
  var rec = $('#srcfilters_record0').clone().attr('id','srcfilters_record'+filterCounter),
      i = filterCounter;
  $('a',rec).text('X').removeAttr('onclick').click(function(){delSourceFilter(i); return false});

  $('input',rec).removeAttr('value').attr('value','');
  $('span',rec).removeAttr('id');
  $('div',rec).removeAttr('id');
  $('#srcfilters').append(rec);
  filterCounter++;
}
function delSourceFilter(a) {
  filterCounter--;
  $('#srcfilters_record'+a).remove();
}

