//namespace
var HELV = window.HELV || {};


// give control of the $ and the jquery variable back to whichever library first implemented it
HELV.$ = jQuery.noConflict(false); 


//simple COVERFLOW, jquery ui draggable with additional custom controls and events
HELV.simpleCoverflow = {
	CONTENTWIDTH: 690,
	DRAGWIDTH: 648,
	DEFAULTSLOT: 1, //default slot
	currentSlot: this.DEFAULTSLOT,
	currentHandleWidth: function() {
		var _hw = this.DRAGWIDTH / parseInt(this.maxSlot(),10);
		jQuery("#drag_containment .snap_ct,#simple_drag_me").css("width",_hw + "px");
		return _hw;
	},
	maxSlot: function() { //dynamic max slots
		return jQuery("#drag_ct .cfsnap").length;
	},
	x: function() { //dynamic position for drag handle
		var _x = (this.currentHandleWidth() * this.currentSlot) - this.currentHandleWidth();
		return _x;
	},
	y: function() { //dynamic position for content
		var _y = this.CONTENTWIDTH - (this.currentSlot * this.CONTENTWIDTH);
		return _y;
	},
	prev: function() {
		if (this.currentSlot > 1) {
			this.currentSlot = this.currentSlot - 1;
		}
		var _v = this.x();
		var _w = this.y();
		jQuery("#simple_drag_me").animate({left: _v + "px"},500,this.processHighlighting());
		jQuery("#simple_scroller").animate({left: _w + "px"},500,this.processHighlighting());
	},
	next: function() {
		if (this.currentSlot < this.maxSlot()) {
			this.currentSlot = this.currentSlot + 1;
		}
		var _v = this.x();
		var _w = this.y();
		jQuery("#simple_drag_me").animate({left: _v + "px"},500,this.processHighlighting());
		jQuery("#simple_scroller").animate({left: _w + "px"},500,this.processHighlighting());
	},
	jumpTo: function(target) {
		var t = isFinite(parseInt(target)) ? parseInt(target) : this.DEFAULTSLOT;
		this.currentSlot = t;
		var _v = this.x();
		var _w = this.y();
		jQuery("#simple_drag_me").animate({left: _v + "px"},100);
		jQuery("#simple_scroller").animate({left: _w + "px"},100);
		this.processHighlighting();
	},
	snapSlot: function(handleLeftPosition) {
		//when snapping does not kick in, force jump to nearest neighbour
		var _h = this.currentHandleWidth();
		var _l = parseInt(handleLeftPosition + _h/2);
		var _p = parseInt(_l/_h+1,10);
		this.jumpTo(_p);
	},
	processHighlighting: function() {
		jQuery("#drag_containment .cfsnap").each(function(j) {
			if (HELV.simpleCoverflow.currentSlot == j+1) {
				jQuery(this).addClass("act");
			} else {
				jQuery(this).removeClass("act");
			}
		});
	},
	init: function() {
		this.maxSlot();
		this.currentHandleWidth();
		this.jumpTo(this.currentSlot);

		//click handlers
		//coverflow arrow: scroll left
		jQuery("#drag_ct .prev").click(function() {
			HELV.simpleCoverflow.prev();
			return false;
		});
		//coverflow arrow: scroll right
		jQuery("#drag_ct .next").click(function() {
			HELV.simpleCoverflow.next();
			return false;
		});
	}
} || window.HELV.simpleCoverflow;


//COVERFLOW, jquery ui draggable with additional custom controls and events
HELV.coverflow = {
	CONTENTWIDTH: 692,
	HANDLEWIDTH: 162,
	DEFAULTSLOT: 1, //default slot
	currentSlot: this.DEFAULTSLOT,
	maxSlot: function() { //dynamic max slots
		return jQuery("#drag_ct .cfsnap").length;
	},
	setSlot: function() { //set starting slot: default=1, alternative is url anchor #cf[x]
		var _l = location.hash.substring(1); //complete anchor string from url
		var _h = _l.indexOf("cf"); //string position of our cf anchor
		if (_h != -1) {
			this.currentSlot = parseInt(_l.charAt(_h +2),10); //target slot nr is 3rd char in anchor string
		} else {
			this.currentSlot = this.DEFAULTSLOT;
		}
	},
	x: function() { //dynamic position for drag handle
		var _x = (this.HANDLEWIDTH * this.currentSlot) - this.HANDLEWIDTH;
		return _x;
	},
	y: function() { //dynamic position for content
		var _y = this.CONTENTWIDTH - (this.currentSlot * this.CONTENTWIDTH);
		return _y;
	},
	prev: function() {
		if (!this.warnIE7Zoom()) {
			if (this.currentSlot > 1) {
				this.currentSlot = this.currentSlot - 1;
			}
			var _v = this.x();
			var _w = this.y();
			jQuery("#drag_me").animate({left: _v + "px"},500,this.processHighlighting());
			jQuery("#scroller").animate({left: _w + "px"},500,this.processHighlighting());
		}
	},
	next: function() {
		if (!this.warnIE7Zoom()) {
			if (this.currentSlot < this.maxSlot()) {
				this.currentSlot = this.currentSlot + 1;
			}
			var _v = this.x();
			var _w = this.y();
			jQuery("#drag_me").animate({left: _v + "px"},500,this.processHighlighting());
			jQuery("#scroller").animate({left: _w + "px"},500,this.processHighlighting());
		}
	},
	jumpTo: function(target) {
		if (!this.warnIE7Zoom()) {
			var t = isFinite(parseInt(target)) ? parseInt(target) : this.DEFAULTSLOT;
			this.currentSlot = t;
			var _v = this.x();
			var _w = this.y();
			jQuery("#drag_me").animate({left: _v + "px"},100);
			jQuery("#scroller").animate({left: _w + "px"},100);
			this.processHighlighting();
		}
	},
	snapSlot: function(handleLeftPosition) {
		//when snapping does not kick in, force jump to nearest neighbour on the left
		var _p = parseInt(handleLeftPosition/this.HANDLEWIDTH + 1,10);
		this.jumpTo(_p);
	},
	processHighlighting: function() {
		jQuery("#drag_containment .cfsnap").each(function(j) {
			if (HELV.coverflow.currentSlot == j+1) {
				jQuery(this).addClass("act");
			} else {
				jQuery(this).removeClass("act");
			}
		});
	},
	processSubpaging: function() {
		//remove subpaging for each tab when no paragraphs are found in 2nd container, special treatment for 3rd subpaging (was added later)
		jQuery("#scroller .cftab").each(function(i,e) {
			//search subpage2 in this tab for paragraphs
			var _2ndp = jQuery(this).find(".cf_t").eq(1);
			if (jQuery(_2ndp).find("p").length == 0) {
				jQuery(this).find("ul").next("div").addClass("ui-tabs-panel"); //manually add tab class on first panel for layout consistency
				jQuery(this).children("ul").remove(); //remove the subpaging construct
			}
			//search subpage3 in this tab for paragraphs
			var _3rdp = jQuery(this).find(".cf_t").eq(2);
			if (jQuery(_3rdp).find("p").length == 0) {
				jQuery(this).children("ul").children("li").eq(2).remove(); //remove subpaging for empty 3rd panel
			}
		});
	},
	warnIE7Zoom: function() {//ie7 zoom workaround helper: warn ie7 users if zoom != 100%
		if (jQuery.browser.msie && jQuery.browser.version == 7) {
			this.ie7Rect = document.body.getBoundingClientRect(); 
			this.ie7ZoomLevel = Math.round((this.ie7Rect.right-this.ie7Rect.left)/document.body.clientWidth * 100);
			if (this.ie7ZoomLevel != 100) {
				alert("Die Zoomfunktion im Internet Explorer 7 wird nicht unterst\u00FCtzt.\n\nLa fonction de zoom n\u0027est pas prise en charge dans Internet Explorer 7.\n\nInternet Explorer 7 non supporta la funzione zoom.");
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	},
	init: function() {
		this.processSubpaging();
		this.maxSlot();
		this.setSlot();
		this.jumpTo(this.currentSlot);

		//click handlers
		//coverflow arrow: scroll left
		jQuery("#drag_ct .prev").click(function() {
			HELV.coverflow.prev();
			return false;
		});
		//coverflow arrow: scroll right
		jQuery("#drag_ct .next").click(function() {
			HELV.coverflow.next();
			return false;
		});
		//coverflow anchors
		jQuery(".cfsnap").click(function() {
			HELV.coverflow.jumpTo(jQuery(this).attr("id").substr(3,1)); //id=cfs[x], 1-4 slots expected -> cfs|1 to cfs|4
			return false;
		});
	}
} || window.HELV.coverflow;


HELV.util = {
	
	 
	cookie: function(name, value, options) {
		if (typeof value != 'undefined') { // name and value given, set cookie
			options = options || {};
			if (value === null) {
				value = '';
				options.expires = -1;
			}
			var expires = '';
			if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
				var date;
				if (typeof options.expires == 'number') {
					date = new Date();
					date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
				}
				else {
					date = options.expires;
				}
				expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
			}
			var path = options.path ? '; path=' + options.path : '';
			var domain = options.domain ? '; domain=' + options.domain : '';
			var secure = options.secure ? '; secure' : '';
			document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
		}
		else { // only name given, get cookie 
			var cookieValue = null;
			if (document.cookie && document.cookie !== '') {
				var cookies = document.cookie.split(';');

				for (var i = 0; i < cookies.length; i++) {
					var cookie = jQuery.trim(cookies[i]);
					// Does this cookie string begin with the name we want?
					if (cookie.substring(0, name.length + 1) == (name + '=')) {
						cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
						break;
					}
				}
			}
			return cookieValue;
		}
	},
	log: function(text) { //console log
		if (jQuery.browser.mozilla) {
			console.log(text);
		}
	}
} || window.HELV.util;