//functions used by drag&drop 
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = getMouseXY;
var moduleGhost = null;

var tempX = 0
var tempY = 0

var mouseXY = Array();

function getMouseXY(e) {  	
  if (!e) 
		var e = window.event;
  if (IE) { // grab the x-y pos.s if browser is IE
    tempY = event.clientY + parseInt(document.documentElement.scrollTop);
    tempX = event.clientX + parseInt(document.documentElement.scrollLeft);
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  
  mouseXY[0] = tempX;
  mouseXY[1] = tempY;  
  
  return true;
}


function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
        curleft += obj.x;
    }
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent!=null) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) curtop += obj.y;
	return curtop;

}
function setLeft(o,oLeft) {
    o.style.left = oLeft + "px"
}

function setTop(o,oTop) {
    o.style.top = oTop + "px"
}

function setPosition(o,oLeft,oTop) {
    setLeft(o,oLeft)
    setTop(o,oTop)
}



/*
Ajax.send = function(url, meth, pars, callBack, data){		
	new Ajax.Updater('',url+ '?' + pars,{ method:meth , onComplete: callBack, onFailure: function(){alert('Failure!');}});
}
*/
function stopHere(evt){
	if (!evt) 
		var evt = window.event;
	
	evt.cancelBubble = true
	if (evt.stopPropagation)
		evt.stopPropagation()
}

String.prototype.nl2br = function(){
  return this.replace(/([^>])\n|\r|\r\n/g, '<br>');
}

String.prototype.br2nl = function(){	
	return this.replace(/<br>|<BR>/g, '\n');		
}

String.prototype.trim = function(){	
	return this.replace(/^\s*|\s*$/g,"");		
}
var load_page;
function load_boxes() {
    var url = 'x_index_box.php?action=load_page';
    load_page = new xmlhttp_request(url, load_boxes_resp, 'load');
}
function load_boxes_resp() {
    var o = find_object('dynamic_content');
    if (o) {
        var re = new RegExp("(<script([^<]*?)>(.*?)</script>)", "im");
        var html;
        try {
            html = decodeURIComponent(load_page.response);
        } catch (e) {
            html = unescape(load_page.response);
        }
        var script = '';
        var rmax = 1000;
        var r = 0;
        var m;
        var i=0;
        var mark = new Array();
        while ((r < rmax) && (m = re.exec(html))) {
            if (m[2].indexOf('src=') != -1) {
                html = html.replace(re, '%mark'+i+'%');
                mark[i] = m[1];
                i++;
            } else {
                script += m[3];
                html = html.replace(re, '');
            }
        }
        for (var i in mark) {
            html = html.replace('%mark'+i+'%', mark[i]);
        }
        o.innerHTML = html;
        eval(script);
    }
    FB.XFBML.Host.parseDomTree(); 
    var container = $('div.comment_list_gallery');
    var ul = $('ul', container);
    var productWidth = ul.innerWidth() - container.outerWidth();
    $('.comment_list_slider', container).slider({ 
            handle: '.comment_list_handle',
            minValue: 0, 
            maxValue: productWidth, 
            slide: function (ev, ui) {
                ul.css('left', '-' + ui.value + 'px');
            }, 
            stop: function (ev, ui) {
                ul.animate({ 'left' : '-' + ui.value + 'px' }, 500, 'linear');
            }
    });
}
var index_boximages = new Array;
function index_boximage_add(box, idx) {
    if (typeof(index_boximages[box]) == 'undefined') index_boximages[box] = new Array();
    index_boximages[box][idx] = idx;
}
function index_boximage_switch(box, idx, obj) {
    for(var i in index_boximages[box]) {
        var o;
        if (o = find_object(box + '_'+i)) {
            o.style.display = i == idx ? 'block' : 'none';
        }
        if (o = find_object(box+'_header_'+i)) {
            if (parseInt(i) == i) {
                o.className = i == idx ? "sel" : "";
            } else {
                o.className = i == idx ? i : "";
            }
        }
        
    }
}
function create_index_box(name) {
    var box = new ImageBox(name);
}

var ImageBox = function(name) {				
    return false;
    var obj = document.createElement('DIV');
    obj.className = 'index_box';
    obj.innerHTML = name;
    obj.style.display = 'static';
    obj.id = 'index_box_'+name;
    var o;
    if (o = find_object('index_boxes2')) {
        o.appendChild(obj);
    }
	this.dragStart = function() {
    }
	this.dragEnd = function() {

    }
    this.posSaved = function() {
    }
    this.findPrevImage = function() {
    }
    Drag.init(obj, obj);
    new DragDrop(obj, this);			
}


var DragDrop = function(obj, box){
	obj.onDragStart = function(x, y){						
			if(box!=null)	{
                box.dragStart();		
            }
			var top = findPosY(this);
            if (moduleGhost == null) moduleGhost = document.getElementById('module_ghost');
			
			this.style.top = parseInt(top)+"px";
			this.parentNode.insertBefore(moduleGhost, this);			
			moduleGhost.style.height = this.offsetHeight +"px";	
			moduleGhost.style.width= this.offsetWidth +"px";	
			this.style.position = "absolute";			
            
		},
	obj.onDrag = function(nx, ny){
			var list = document.getElementById('index_boxes2');
			
			if(list){
				var myPos = 0;							
				
				var elems = null;
				elems = Array();
				for(var i=0;i<list.childNodes.length;i++){
					if(list.childNodes[i].tagName == "DIV" && list.childNodes[i]!=this){			
						elems.push(list.childNodes[i]);
					}
				}
				for(var i=0;i<elems.length;i++){
					if(elems[i] == moduleGhost){
						myPos = i;
						break;
					}
				}		
				
				if (myPos!=0 && ny<=findPosY(elems[myPos-1])) {	
					list.removeChild(moduleGhost);	
					list.insertBefore(moduleGhost, elems[myPos-1]);	
				}			
				
				if (myPos!=(elems.length-1) && ny>=findPosY(elems[myPos+1])) {			
					if (elems[myPos+2]) {
						list.insertBefore(moduleGhost, elems[myPos+2]);
					} else {						
						list.appendChild(moduleGhost);
					}	
				}
				
			}		
		},
	obj.onDragEnd = function(x,y) {
			if (!IE) document.captureEvents(Event.MOUSEMOVE)
			document.onmousemove = getMouseXY;
			this.style.position=  "static"			
			moduleGhost.parentNode.insertBefore(this, moduleGhost);		
			moduleGhost.parentNode.removeChild(moduleGhost);
			if(box!=null)	{
                box.dragEnd();		
            }
	}
}
var Drag = {	
	obj : null,

    init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
    {
        o.onmousedown    = Drag.start;

        o.hmode            = bSwapHorzRef ? false : true ;
        o.vmode            = bSwapVertRef ? false : true ;

        o.root = oRoot && oRoot != null ? oRoot : o ;

        if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
        if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
        if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
        if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

        o.minX    = typeof minX != 'undefined' ? minX : null;
        o.minY    = typeof minY != 'undefined' ? minY : null;
        o.maxX    = typeof maxX != 'undefined' ? maxX : null;
        o.maxY    = typeof maxY != 'undefined' ? maxY : null;

        o.xMapper = fXMapper ? fXMapper : null;
        o.yMapper = fYMapper ? fYMapper : null;				
		
        o.root.onDragStart    = new Function();
        o.root.onDragEnd    = new Function();
        o.root.onDrag        = new Function();  
    },

    start : function(e)
    {
        var o = Drag.obj = this;
		
		
        e = Drag.fixE(e);		
        var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
        o.root.onDragStart(x, y);

        o.lastMouseX    = e.clientX;
        o.lastMouseY    = e.clientY;

        if (o.hmode) {
            if (o.minX != null)    o.minMouseX    = e.clientX - x + o.minX;
            if (o.maxX != null)    o.maxMouseX    = o.minMouseX + o.maxX - o.minX;
        } else {
            if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
            if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
        }

        if (o.vmode) {
            if (o.minY != null)    o.minMouseY    = e.clientY - y + o.minY;
            if (o.maxY != null)    o.maxMouseY    = o.minMouseY + o.maxY - o.minY;
        } else {
            if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
            if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
        }
		Drag.drag(e);
        document.onmousemove    = Drag.drag;
        document.onmouseup        = Drag.end;
			
        return false;
    },

    drag : function(e)
    {
		moduleGhost.style.display = "block";
        e = Drag.fixE(e);
        var o = Drag.obj;
        var ey    = e.clientY;
        var ex    = e.clientX;
        
		var y = parseInt(o.root.style.top);
        var x = parseInt(o.root.style.left);
        var nx, ny;
         
		nx = x + ex - o.lastMouseX;
        ny = y + ey - o.lastMouseY;

        Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
        Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
        Drag.obj.lastMouseX    = ex;
        Drag.obj.lastMouseY    = ey;

        Drag.obj.root.onDrag(nx, ny);
		
        return false;
    },

    end : function()
    {
        
		document.onmousemove = null;
        document.onmouseup   = null;
        Drag.obj.root.onDragEnd(    parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
                                    parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));		
		
        Drag.obj = null;
    },

    fixE : function(e)
    {
        if (typeof e == 'undefined') e = window.event;
        if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
        if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
        return e;
    }
}

var index_tags = new Array();var index_tag_ids = new Array();
var populate_req;
function populate_index_taglist() {
    var url = 'x_index_box.php?action=populate_taglist';
    populate_req = new xmlhttp_request(url, populate_index_taglist_resp, 'load');
}
function populate_index_taglist_resp() {
    add_index_taglist(decodeURIComponent(populate_req.response));
}
var actbobj;
function add_index_taglist(str) {
    var a = str.split(':||:');
    for (var i=0;i<a.length;i++) {
        if (a[i].length) {
            try {
                var b = a[i].split('||');
                var tag = decodeURIComponent(b[1]);
                index_tags.push(tag); 
                index_tag_ids[tag] = b[0];
            } catch(e) {}   
        }
    }
    actbobj = actb(document.getElementById('tag_search'),index_tags);
}
function push_index_tag(tag,id) {
    if (id == undefined) id=0;
    index_tags.push(tag); 
    index_tag_ids[tag] = id;
}
function go_tag() {
    var o = document.getElementById('tag_search');
    if (o && index_tag_ids[o.value] != undefined) {
        document.location = 'section.php?tag_id='+index_tag_ids[o.value];
    }
}
