/**
 * Generic JS
 *
 * @author 2005 - 2007 Marek Dušek <info@marekdusek.cz>
 */
var version = navigator.appVersion.toLowerCase();
var ie = (version.indexOf('msie')>-1);

function toggleMembers(elmid)
{
	var tr = $('#'+elmid)[0];
	
	while (tr) {
		if (tr.tagName == 'TR' && tr.className != 'incart') {
			$(tr).toggle();
		}
		
		tr = tr.nextSibling;
	}
	return false;
}

function toggleSubtypes()
{
	var div;
	if (!(div = $('#subtypes'))) {
		return false;
	}
	var hiding = false;
	var sw_open = $('#cat-list-open');
	var sw_close = $('#cat-list-close');

	if (sw_close.is(':visible')) {
		sw_open.show();
		sw_close.hide();
		$('div', div).hide();
		hiding = true;	
	} else {
		sw_open.hide();
		sw_close.show();
		$('div', div).show();
	}
	
	saveCookie('hide_subtypes', hiding);
	
	return false;
}
 
function checkedAll(checkboxes)
{
    for(i in checkboxes) {
        document.getElementById(checkboxes[i]).checked = true;
    }
}

function checkedNone(checkboxes)
{
    for(i in checkboxes) {
        document.getElementById(checkboxes[i]).checked = false;
    }
}

function resetInputs(ids)
{
    for(i in ids) {
        document.getElementById(ids[i]).value = 0;
    }
}
var active_code = '';
var codes = [];
var default_inputs = ['title', 'note'];
var langs = ['en', 'fr', 'pt', 'es'];
function set_codes(codes_array)
{
    codes = codes_array;
    for(i in codes) {
        default_inputs[default_inputs.length] = codes[i]+'_color';
    }
}
function lang_sel(lang, inputs, buttons)
{
    if(!inputs) {
        inputs = default_inputs;
    }
    for(var j in langs) {
        for(var i in inputs) {
            elm = document.getElementById(inputs[i]+"_"+langs[j]+"_div");
            if(elm) {
                if(langs[j] == lang) {
                    elm.style.display = "block";
                }
                else {
                    elm.style.display = "none";
                }
            }
        }
        if(buttons) {
            butt = document.getElementById("lang_"+langs[j]);
            if(langs[j] == lang) {
                butt.style.background = "maroon";
                butt.style.color = "white";
                document.getElementById('selected_lang').value = lang;
            }
            else {
                butt.style.background = "#eee";
                butt.style.color = "gray";
            }
        }
    }
}
function member_sel(code)
{
    active_code = code;
    for(var i in codes) {
        elm = document.getElementById(codes[i]+"_box");
        if(elm) {
            if(active_code == codes[i]) {
                elm.style.display = "block";
            }
            else {
                elm.style.display = "none";
            }
        }
        butt = document.getElementById(codes[i]+"_switch");
        if(butt) {
            if(codes[i] == active_code) {
                butt.style.background = "maroon";
                butt.style.color = "white";
                document.getElementById('selected_member').value = active_code;
            }
            else {
                butt.style.background = "#eee";
                butt.style.color = "gray";
            }
        }
    }
}
function copy_data(sourceid, destid)
{
    var inputs;
    var selects;
    source = document.getElementById(sourceid);
    dest = document.getElementById(destid);
    if(source && dest) {
        inputs = source.getElementsByTagName('input');
        for(i in inputs) {
            if(inputs[i].id) {
                tempid = (inputs[i].id).substr((inputs[i].id).indexOf('_'));
                temp = document.getElementById('new'+tempid);
                if(temp) {
                    temp.value = inputs[i].value;
                }
            }
        }
        selects = source.getElementsByTagName('select');
        for(i in selects) {
            if(selects[i].id) {
                tempid = (selects[i].id).substr((selects[i].id).indexOf('_'));
                temp = document.getElementById('new'+tempid);
                if(temp) {
                    temp.value = selects[i].value;
                }
            }
        }
    }
    member_sel('new');
}
function mark_as_missing(elmid)
{
    nodes = document.getElementById('input-'+elmid).childNodes;
    for(i in nodes) {
        if(nodes[i].name == elmid) {
            nodes[i].style.borderColor = "maroon";
            nodes[i].style.background = "#FFF2F2";
        }
    }
    return;
    if(elm) {
        elm.style.color = 'maroon';
        elm.style.fontWeight = 'bold';
    }
}
/*	----------------------------------------------------------------------------
    Drop-down menu
	------------------------------------------------------------------------- */
subs_visible = [];
timer = null;
timer2 = null;
milisec = 500;
function show_sub(subId)
{
	list = document.getElementById('navi-list');
	submenus = list.getElementsByTagName('ul');
	for(i in submenus) {
	    if(submenus[i].id) {
	        if(submenus[i].id == subId) {
	            elm = document.getElementById(subId);
	            if(timer) {
	                clearTimeout(timer);
	                clearTimeout(timer2);
	            }
	            if(elm && !subs_visible[subId]) {
	                elm.style.display = "block";
	                subs_visible[subId] = true;
	            }
	        }
	        else {
	            hide_sub(submenus[i].id);
	        }
	    }
	}
	$('#'+subId).parent().children('a').addClass('navi-cur');
}
function timer_sub(subId)
{
    elm = document.getElementById(subId);
    if(elm && subs_visible[subId]) {
        timer = setTimeout("hide_sub(\""+subId+"\")", milisec);
    }
}
function hide_sub(subId)
{
    elm = document.getElementById(subId);
    if(elm && subs_visible[subId]) {
        elm.style.display = "none";
        subs_visible[subId] = false;
        $('#'+subId).parent().children('a').removeClass('navi-cur');
    }
}
/* -----------------------------------------------------------------------------
    AJAX magic box
    ------------------------------------------------------------------------- */
jQuery.extend({
	context: function (context) {
		var co = {
			callback: function (method) {
				if (typeof method == 'string') method = context[method];
				var cb = function () { method.apply(context, arguments); }
				return cb;
			}
		};
		return co;
	}
}); 

function MagicBox(el, url)
{
	this.elem = $('#'+el)[0];
	this.url = url;

	this.BATCH = 20;
	this.INTERVAL = 5;
	this.MAX_H = 110;
	this.MAX_W = 190;

	this.counter = 0;
	this.cache = [];
	this.images = [];

	this.init = function() {
		this.refresh();
	},

	this.refresh = function() {
		if (this.counter % this.BATCH == 0) {
			// load new
			$.get(this.url+'/magic-box.php', $.context(this).callback('load')); 
		} else {
			// serve cached
			this.render();
		}
	},

	this.load = function(data) {
		var prods = data.split("\r\n"); 
		for (i in prods) {
			this.cache[i] = prods[i];
			try {
				var meta = this.cache[i].split('|');
			} catch (e) {
				continue;
			}
			this.images[i] = new Image();
			this.images[i].src = meta[0];
		}
		this.render();
	}

	this.render = function() {
		var img = $('img', this.elem)[0];
		var meta = this.cache[this.counter].split('|');

		img.that = this;
		img.container = $('p', this.elem)[0];
		img.onload = function() {
			img.style.margin = 0;
			img.style.padding = 0;
			img.style.width = 'auto';
			img.style.background = '#fff';

			if (meta[3] > this.that.MAX_H) {
				meta[2] = Math.round(meta[2] * this.that.MAX_H / meta[3]);
				meta[3] = this.that.MAX_H;
			}

			if (meta[3] < this.that.MAX_H) {
				img.container.style.marginTop = Math.floor((this.that.MAX_H - meta[3]) / 2)+'px';
			} else {
				img.container.style.marginTop = 0;
			}
			if (meta[2] < this.that.MAX_W) {
				img.container.style.marginLeft = Math.floor((this.that.MAX_W - meta[2]) / 2)+'px';
			} else {
				img.container.style.marginLeft = 0;
			}

			img.container.style.width = meta[2]+'px';
			img.container.style.height = meta[3]+'px';

			img.alt = meta[1];
			img.width = meta[2];
			img.height = meta[3];

			$('h5', this.that.elem)[0].innerHTML = meta[1];
			$('a', this.that.elem)[0].href = meta[4];
			$('a', this.that.elem)[0].title = meta[1];

			$(img).fadeIn(1000);
		};

		$(img).fadeOut(1000, function() { this.src = meta[0] });

		if (++this.counter > this.BATCH) {
			this.counter = 0;
		}
		setTimeout($.context(this).callback('refresh'), this.INTERVAL * 1000);
	}
};
/**
 * Cookies
 */
var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') == '1');
  }
}; 
function saveCookie(name, value)
{
    cookie = Cookie.set(name, value, 90);
}
function deleteCookie(name)
{
	cookie = Cookie.erase(name);
}

// click trapper
function click_override(e)
{
    if (navigator.appName == 'Netscape' && (e.which == 2 || e.which == 3)) {
        alert("© 2009 Herrajes Online");
        return false;
    }  else if (navigator.appName == 'Microsoft Internet Explorer'
        && (event.button == 2 || event.button == 3)) {
        alert("© 2009 Herrajes Online");
        return false;
    }
    return true;
}

document.onmousedown = click_override;
if(document.layers) window.captureEvents(Event.MOUSEDOWN);
