//var	trans = '../img/bg/trans.gif';

//

window.onload = init;

function init() {
    if (document.forms.search) {
        search.init();
    }
    var c;
    if (c = document.getElementById('carrousel')) {
        carrousel.init(c);
    }
    /*@cc_on @if (@_jscript_version == 5.6) alpha.fixAll('alpha'); @end @*/
};

// search box

var search = {
    init : function () {
        var q = document.forms.search.query;
        q.onfocus = this.focus;
        q.onblur  = this.blur;
    },
    focus : function () {
        this.style.backgroundPosition = '-100px 0';
    },
    blur : function () {
        if ('' == this.value) {
            this.style.backgroundPosition = '';
        }
    }
};

// carrousel

var carrousel = {
    id : null,
    speed : 5000,
    trans : 5,
    nr : [],
    cur : 0,
    init : function (o) {
        this.id = o;
        var nr = this.id.getElementsByTagName('DIV');
        for (var i = 0, l = nr.length; i < l; i++) {
            if (nr[i].className.match('citem')) {
                this.nr.push(nr[i]);
                dom.first(nr[i]).style.height = dom.last(nr[i]).offsetHeight;
            }
        }
		if (this.nr.length < 2) {
            return false;
        }
        this.rotate();
    },
    rotate : function () {
        var obj = this, z = 10;
        var timer = window.setInterval(
            function () {
                var prev = obj.nr[obj.cur];
                obj.cur = (obj.cur == (obj.nr.length - 1)) ? 0 : obj.cur + 1;
                /*@cc_on
                  @if (@_jscript_version > 5.5)
                  obj.nr[obj.cur].style.filter = 'alpha(opacity = 0)';
                  @else @*/
                obj.nr[obj.cur].style.opacity = '0';
                /*@end @*/
                obj.nr[obj.cur].style.zIndex = z++;
                obj.fade(obj.nr[obj.cur], prev, obj.trans);
            }, obj.speed);
    },
    fade : function (elm, prev, speed) {
        var o = 0, c = 0;
        var t = window.setInterval(
            function () {
                o += 0.2;
                if (o < 1) {
                    /*@cc_on
                      @if (@_jscript_version > 5.5)
                      c += 10;
                      elm.style.filter = 'alpha(opacity = ' + c +')';
                      @else @*/
                    elm.style.opacity = o;
                    /*@end @*/
                } else {
                    window.clearTimeout(t);
                    prev.style.zIndex = '';
                    /*@cc_on
                      @if (@_jscript_version > 5.5)
                      prev.style.filter = 'alpha(opacity = 0)';
                      elm.style.filter = '100';
                      @else @*/
                    prev.style.opacity = '';
                    elm.style.opacity = 1;
                    /*@end @*/
                }
            }, speed);
    }
};

// toggle

function Toggle(id) {
    this.id = document.getElementById(id);
    this.show = 'toon';
    this.hide = 'verberg';
    this.heading = dom.first(this.id);
    this.content = dom.last(this.id);
    this.init();
};
Toggle.prototype.init = function () {
    this.heading.className = 'toggle show';
    this.heading.innerHTML = '<span>' + this.show + '</span> ' + this.heading.innerHTML;
    this.content.style.display = 'none';
    var objRef = this;
    this.heading.onmouseover = this.hover;
    this.heading.onclick = function () {
        return objRef.handle(this, objRef);
    };
};
Toggle.prototype.handle = function(obj, objRef) {
    if (objRef.show == dom.first(obj).innerHTML) {
        dom.first(obj).innerHTML = objRef.hide;
        objRef.heading.className = objRef.heading.className.replace('show', 'hide');
        objRef.content.style.display = '';
    } else {
        dom.first(obj).innerHTML = objRef.show;
        objRef.heading.className = objRef.heading.className.replace('hide', 'show');
        objRef.content.style.display = 'none';
    }
    return false;
};
Toggle.prototype.hover = function () {
    this.style.cursor = 'pointer';
}

// pop-up

var pop = {
    winRef : null,
    up : function (uriRef, argStr) {
        if (!this.winRef || this.winRef.closed) {
            this.winRef = window.open(uriRef, "singleton", argStr);
        } else {
            if (uriRef != this.winRef.location.href) {
                this.winRef.location = uriRef;
            }
            this.winRef.focus();
        }
        return false;
    }
};

var button = {
	init : function (obj) {
		/*@cc_on @if (@_jscript_version < 5.7) return false; @end @*/
		this.unlite(obj);
		obj.onmouseover = function () {
			button.hilite(obj);
		};
		obj.onmouseout = function () {
			button.unlite(obj);
		}
	},
	hilite : function (obj) {
		/*@cc_on
		  @if (@_jscript_version > 5)
		  obj.style.filter = 'alpha(opacity=100)';
		  @else @*/
		obj.style.opacity = 1;
		/*@end @*/
	},
	unlite : function (obj) {
		/*@cc_on
		  @if (@_jscript_version > 5)
		  obj.style.filter = 'alpha(opacity=90)';
		  @else @*/
		obj.style.opacity = 0.9;
		/*@end @*/
	}
};

// type

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); };

var type = {
    path : null,
    needle : /(^|\s)(bgSectMain|bgSect|bgCarH2|bgSpecH1|bgSpecH2|bgNewsHomeH2|bgCopyH1|bgCopyColH2|bgCopyH2|bgCopyH2Theme|bgCopyH2News|bgCopyH2Campaign|bgCopyH2Toggle|bgCopyH2XL|bgCopyH2XXL|bgCopyH2Inv|bgSidebar|bgSideSub|bgSideSubSel|bgModH2)($|\s)/,
	ie : /((^|\s)(bgSectMain|bgSect|bgSpecH1|bgSpecH2|bgCopyH1|bgNewsHomeH2|bgCopyH2Inv|bgCarH2)($|\s))/,
    face : function () {
        if (!this.path) {
            return false;
		}
		var ping = new Http();
		ping.http.open('HEAD', this.path + 'bgSectMain', false);
		var obj = this, response = true, timeout = window.setTimeout(
			function () {
				response = false;
			}, ping.timeout);
		ping.http.onreadystatechange = function () {
			if ((4 == ping.http.readyState) && response) {
				if (ping.response()) {
					callback(obj);
					response = null, window.clearTimeout(timeout);
				}
				ping.http = null;
			}
		};
		try {
			ping.http.send(null);
		} catch (e) {}
	}

};

function callback (obj) {
	for (var elm = document.body.getElementsByTagName('*'), l = elm.length, servelet, src, alt, i = 0; i < l; i++) {
		if (servelet = elm[i].className.match(obj.needle)) {
			alt = dom.text(elm[i]);
			src = obj.path + servelet[0].trim() + '?text=' + encodeURIComponent(alt) ;
			elm[i].innerHTML = '<img src="' + src + '" alt="' + alt + '" title="">';
			l++;
			/*@cc_on
			  @if (@_jscript_version == 5.6)
			    if (type.ie.test(elm[i].className)) {
				  elm[i].style.visibility = 'hidden';
			      elm[i].firstChild.className = 'alpha';
			    }
			  @end @*/
		}
	}
};

function escapeSpecialChars(str) {
	str = str.replace(/%25/g, "%2525");
	str = str.replace(/%23/g, "%2523");
	str = str.replace(/%26/g, "%2526");
	str = str.replace(/\)/g, "%29");
	str = str.replace(/\'/g, "%27");
	str = str.replace(/%2B/g, "%252B");
	return str;
}

// IE 6
var alpha = {
    fixAll : function (attrClass) {
		for(var img = document.images, l = img.length, re = new RegExp("(^|\\s)" + attrClass + "($|\\s)"), i = 0; i < l; i++) {
			if (re.test(img[i].className)) {
				this.fixElm(img[i]);
			}
        }
    },
	fixElm : function (elm) {
		elm.style.width = elm.width + 'px';
		elm.style.height = elm.height + 'px';
		elm.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + escapeSpecialChars(elm.src) + ", sizingMethod = 'crop')";
		elm.src = trans;
		elm.parentNode.style.visibility = '';
	},
    hand : function () {
        this.style.cursor = 'pointer';
    }
};

// DOM
var dom = {
    prev:function(el){do{el=el.previousSibling;}while(el&&1!=el.nodeType);return el;},
    next:function(el){do{el=el.nextSibling;}while(el&&1!=el.nodeType);return el;},
    first:function(el){return((el=el.firstChild)&&1!=el.nodeType)?this.next(el):el;},
    last:function(el){return((el=el.lastChild)&&1!=el.nodeType)?this.prev(el):el;},
    text:function(el){var t='';el=el.childNodes;for(var i=0,l=el.length;i<l;i++){t+=1!=el[i].nodeType?el[i].nodeValue:this.text(el[i]);}return t;}
};

// HTTP
Http = function () {
	if (window !== this) {
		this.timeout = 5000;
		/*@cc_on
		  @if (@_jscript_version >= 5)
		    try {
		      this.http = new ActiveXObject("Msxml2.XMLHTTP");
		    } catch (e) {
		      try { this.http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { this.http = false; }
		    }
		   @end @*/
		if (!this.http && ('undefined' != typeof XMLHttpRequest)) {
			try {this.http = new XMLHttpRequest();} catch (e) {this.http = false;}
		}
		if (!this.http && window.createRequest) {
			try {this.http = window.createRequest();} catch (e) {this.http = false;}
		}
	}
};

Http.prototype.response = function () {
	var s = this.http.status;
	try {
		return (
			((s >= 200) && (s <= 300)) || (s == 304)
			|| ((0 === s) && ('file:' == location.protocol))
			|| ((navigator.userAgent.indexOf('Safari') >= 0) && ('undefined' == typeof s))
			);
	} catch (e) {
		return false;
	}
};

