/***********************************************************************
*									*
* utilties								*
*									*
 ***********************************************************************/

function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    return this;
}


function listen(event, elem, func) {
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(event,func,false);
    else if (elem.attachEvent)  // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    //else throw 'cannot add event listener';
}

document.getElementsByClassName = function (tag, c) {
	/* (c) by Andreas Waidelich */
	var ret = new Array();
	var j = 0;
	var objs = document.all ? document.all : document.getElementsByTagName(tag);
	for(i = 0;i<objs.length;i++){
		if(objs[i].className == c){
			ret[j] = objs[i];
			j++;
		} // if
	} // for
	return ret;
} // getElementsByClassName

/* 
Mimics standard push for IE5, which doesn't implement it. 
(c) Caio Chassot (http://v2studio.com/k/code/)
*/
if (!Array.prototype.push) Array.prototype.push = function() { 
	for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
	return this.length;
}

language = "de";
if ( new RegExp("/en/").exec(document.location) )
    language = "en";

/***********************************************************************
*									*
* dynamic search lists							*
*									*
 ***********************************************************************/

function set_manufacturer(man_list, cam_list, add_header) {
	for ( var i = cam_list.options.length - 1 ; i >= 0 ; i-- )
		cam_list.remove(i);
		//cam_list.remove(cam_list.options[i]);

	if ( add_header ) {
		var o = document.createElement("option");
		o.text = "-----";
		o.value = "";
		cam_list.add(o, document.all ? cam_list.length : null);
	}

	var man = man_list.options[man_list.options.selectedIndex].value;
	document.cookie = "qs_man=" + man + "; path=/";

	if ( man != "" ) {
		var re = new RegExp("qs_cam=([^;]+)");
		var cam = re.exec(document.cookie);
		cam = (cam != null) ? cam[1] : 0;

		for ( var i = 0 ; i < camera_list.length ; i++ ) {
			if ( camera_list[i][0] == man ) {
				o = document.createElement("option");
				o.text = camera_list[i][2];
				o.value = camera_list[i][1];
				if ( camera_list[i][1] == cam ) {
					var a = document.createAttribute("selected");
					a.nodeValue = "selected";
					o.setAttributeNode(a);
				}
	
				cam_list.add(o, document.all ? cam_list.length : null);
			}
		}
	}
	else {
		document.cookie = "qs_cam=0; path=/";

		o = document.createElement("option");
		if ( language == "en" )
			o.text = "Select manufacturer first";
		else
			o.text = "Erst Hersteller wählen";
		o.value = "";
		cam_list.add(o, document.all ? cam_list.length : null);
	}
}

function quicksearch_change(elem) {
	if ( elem.name == "man" ) {
		var man = elem.options[elem.options.selectedIndex].value;
		if ( man != "" )
			document.cookie = "qs_man=" + man + "; path=/";

		set_manufacturer(elem, document.getElementById("search_camera_select"), 1);
	}
	else if ( elem.name == "cam" ) {
		var cam = elem.options[elem.options.selectedIndex].value;
		if ( cam != "" ) {
			document.cookie = "qs_cam=" + cam + "; path=/";
			if ( language == "en" )
				window.location.href = "/en/index.php?id=82&cam=" + cam;
			else
				window.location.href = "/index.php?id=82&cam=" + cam;
		}
	}
}

function esearch_change() {
	set_manufacturer(document.getElementById("esearch_man"), document.getElementById("esearch_cam"), 1);
}

function fix_search() {
	var e = document.getElementById("esearch_man");
	if ( e )
		set_manufacturer(e, document.getElementById("esearch_cam"), 1);

	e = document.getElementById("search_manufacturer_select");
	if ( e )
		set_manufacturer(e, document.getElementById("search_camera_select"), 1);
}

listen("load", window, fix_search);

/***********************************************************************
*									*
* mail deobfuscation							*
*									*
 ***********************************************************************/

function replace_mail() {
	var elems = document.getElementsByClassName("span", "mailaddress");
	for ( var i = 0 ; i < elems.length ; i++ ) {
		var e = elems[i];

		var mail = e.firstChild.data;
		mail = mail.replace(/ dot /, ".");
		mail = mail.replace(/ at /, "@");
		var a = document.createElement("a");
		var href = document.createAttribute("href")
		href.nodeValue = "mailto:" + mail;
		a.setAttributeNode(href)
		var text = document.createTextNode(mail);
		a.appendChild(text);
		e.replaceChild(a, e.firstChild);
	}
}

listen("load", window, replace_mail);

/***********************************************************************
*									*
* for typo3 popups							*
*									*
 ***********************************************************************/

function openPic(url,winName,winParams)	{	//
	var theWindow = window.open(url,winName,winParams);
	if (theWindow)	{theWindow.focus();}
}

