/*$.fn.typewriter = function() {
    this.each(function() {
        var $ele = $(this), str = $ele.text(), progress = 0;
        $ele.text('_');
        var timer = setInterval(function() {
            $ele.text(str.substring(0, progress++) + (progress & 1 ? '_' : ''));
            if (progress >= str.length) clearInterval(timer);
        }, 150);
    });
    return this;
};*/



/* will be initialized by hsga.slideslist onready */
var slides = {
	// ref to each show added with ``addshow``, used by slideshows setTimeout
	shows: {},
	// show currently running
	showing: null,
	// add a new show with params: 
	// 	HTML previewid, path to images, array of image paths	
	addshow: function (previewid, path, images, delay, transdelay) {
		slides.shows[previewid] = 
			new slides.Slideshow(previewid, path, images, delay, transdelay);
	},
	// start the slideshow with previewid
	start: function(previewid) {
		if (this.showing) this.showing.stop();
		this.showing = slides.shows[previewid];
		this.showing.start();
		return false;
	},
	// stop any slideshow running, only 1 at a time
	stop: function() {
		this.showing.stop();
		this.showing = null;
		return false;
	}
}
slides.Slideshow = function(previewid, path, images, delay, transdelay) {
	this.sid = previewid; // pass refs to img.onload and setTimeout
	this.preview = document.getElementById(previewid);
	this.path = path; // common path to images
	this.images = images; // paths to images
	this.delay = delay; // delay between images
	this.transdelay = (transdelay) ? transdelay : '1.3'; // time of transition
	this.showing = 0; // number of image currently showing
	this.running = false; // slideshow is running?
}
slides.Slideshow.prototype.start = function() {
	this.running = true;
	this.next();
}
slides.Slideshow.prototype.stop = function() {
	clearTimeout(this.timeout); 
	this.running = false;
}
slides.Slideshow.prototype.next = function() {
	if (this.running) {
		clearTimeout(this.timeout);
		this.showing++;
		if (this.showing >= this.images.length) {
			this.showing = 0;
		}
		var im = new Image(1,1); // preload
		im.sid = this.sid; // to get ref to slideshow
		im.onload = this.show;
		im.src = this.path + "/" + this.images[this.showing];
	}
}
slides.Slideshow.prototype.show = function() {
	var slideshow = slides.shows[this.sid]; // .this is ref to image 
	crossfade(slideshow.preview, slideshow.path + "/" + 
		slideshow.images[slideshow.showing], slideshow.transdelay, '');
	if (slideshow.running) {
		slideshow.timeout = setTimeout(function() {
			slides.shows[slideshow.sid].next();
			},
			//"slides.shows['"+slideshow.sid+"'].next()", 
			slideshow.delay);
	}
}


/* homepage ticker data */
var ticker = {
	contentheight: 0,
	eleheight: 150, // see hsga.css position top!
	pos: 150,
	delay: 100,
	tickerheight: null, // must be overwritten in page !
	timer: null,
	tickerdiv: null,
	
	start: function() {
		ticker.ele = document.getElementById('TICKER-DIV');
		if (ticker.ele) {
			ticker.stop();
			ticker.timer = setInterval('ticker.run()', ticker.delay); 
			}
	},	
	stop: function() {
		clearInterval(ticker.timer);
	},
	run: function() {
		if (ticker.pos < -ticker.contentheight) {
			ticker.pos = ticker.eleheight;
			ticker.stop();
			ticker.start(); 
			}
		else {
			ticker.pos -= 1;
			ticker.ele.style.top = ticker.pos + "px";
		}
	}
}

/* util */
var opened = false;
var fenster;
function openwin(URL,winname,features) {
	if(opened == false) {
		fenster = open(URL,winname,features);
		opened = true;
		fenster.focus();
		}
	if(fenster.closed == true) {
		fenster = open(URL,winname,features);
		fenster.focus();
		}
	if(fenster.closed == false) {
		fenster = open(URL,winname,features);
		fenster.focus();
		}
	return false;
}

function getStyle(e, styleProp) {
	if (window.getComputedStyle)
		var y = window.getComputedStyle(e,null).getPropertyValue(styleProp);
	else if (e.currentStyle)
		var y = eval('e.currentStyle.' + styleProp);
	return y;
}



// main package
var hsga;
if (!hsga) hsga = {}
$.extend(hsga, {
	init: function() {
		// newsletter links
		$('a.newsletter').each(function() {
			var a = $(this)
			  , h = a.attr('data-height') || 560
			  
			a.colorbox({
				iframe: true,
				width: 355,
				height: h,
				opacity: 0.4,
				close: "schließen"
			})	
		})			
		
		// zoom series
		$('.zoomseries').each(function() {
			var s = $(this),
				a1 = s.find('a:first')

			s.find('a').colorbox({
					opacity: 0.4,
					current: "{current} von {total}",
					previous: "zurück",
					next: "weiter",
					close: "schließen",
					transition: "elastic"
			})			
			s.find('img').click(function() {
				a1.click()
			})
			
		})
		// init slides collected in hsga.slideslist
		for (var i=0, L = hsga.slideslist.length; i<L; i++) {
			var s = hsga.slideslist[i]
			slides.addshow(s[0], s[1], s[2], s[3], s[4])
		}
		
		// typewriter for ads
		//setTimeout(function(){$('.bodyhome #ourads').typewriter()}, 2000)
	}

})
$(hsga.init)



