/**
 * @author:		Angelo Dini
 * @copyright	Maxomedia - Agentur für Crossmedia-Kommunikation BSW
 */

// debug
if (window['console'] === undefined) window.console = { log: $empty };

// $E
$E = document.getElement.bind(document);

// set namespace
var SS = {};

// team module
SS.teamLoader = {
	init: function () {
		this.image = $('team-image');
		this.fx = new Fx.Tween(this.image, {link: 'cancel', duration: 500, transition: 'sine:in:out'});
		
		this.containers = $$('.team-item');
		this.containers.each(function (item) {
			item.addEvent('mouseenter', this.showImage.pass(item, this));
		}, this);
	},
	showImage: function (item) {
		this.container = item;
		this.name = this.container.getElement('h2').get('html');
		this.cleanName = this.name.toLowerCase().replace(' ', '_');
		this.src = '/_gfx/team/' + this.cleanName + '.jpg';
		
		// show picture with sine effect
		this.fx.start('opacity', 0, 1);
		// prevent flimmer effect
		(function () {
			this.image.set('src', this.src);
		}).bind(this).delay(25)
		// set alt & title
		this.image.set('alt', this.name);
		this.image.set('title', this.name);
	}
};

// portfolio module
SS.portfolioLoader = {
	init: function (section) {
		this.image = $('image');
		this.fx = new Fx.Tween(this.image, {link: 'cancel', duration: 500, transition: 'sine:in:out'});
		
		this.containers = $$('#list a');
		this.containers.each(function (item, index) {
			item.addEvent('mouseenter', this.showImage.pass([index, section, item], this));
		}, this);
	},
	showImage: function (index, section, anchor) {
		var rel = anchor.get('rel');
		this.src = '/_gfx/' + section + '/' + rel + '.jpg';
		
		// show picture with sine effect
		this.fx.start('opacity', 0, 1);
		// prevent flimmer effect
		(function () {
			this.image.set('src', this.src);
		}).bind(this).delay(25)
	}
};

// portfolio module
SS.portfolio = {
	init: function (data, index) {
		this.data = data;
		this.folder = '/_gfx/marken/';
		this.containers = $$('.container');
		// define trigers and trigger events
		this.triggers = $$('.trigger a');
		this.triggers.each(function (item, index) {
			item.addEvent('click', this.initSlideshow.bindWithEvent(this, [index, 0, false]));
		}, this);

		// init Accordion
		this.initAccordion(index);
		// init slideShow at startup
		if(index != -1) {
			this.initSlideshow(false, index, 0, true);
		}
	},
	initAccordion: function (index) {
		new Fx.Accordion(this.triggers, this.containers, {
			display: index,
			alwaysHide: true
		});
	},
	initSlideshow: function (event, index, bound, loop) {
		if(event) event.stop();
		this.activeAccordion = index;
		this.loop = loop;
		this.src = this.folder + this.data[this.activeAccordion];

		// load fx
		this.image = $('image');
		this.fx = new Fx.Tween(this.image, {link: 'cancel', duration: 500, transition: 'sine:in:out'});
		
		// get bound
		new Asset.image(this.src + '_' + bound + '.jpg', {
			onload: function () {
				bound++;
				this.initSlideshow(false, this.activeAccordion, bound, this.loop);
			}.bind(this),
			
			onerror: function () {
				bound = bound -1;
				this.startSlideshow(bound, 0);
			}.bind(this)
		});

		if(index != -1) {
			this.triggers.removeClass('setActive');
			this.triggers[index].addClass('setActive');
		}
	},
	startSlideshow: function (bound, start) {
		this.bound = bound;
		this.startIndex = start;
		// load first image
		if(!this.loop) {
			// clear timer
			this.timer = $clear(this.timer);
			this.setSlide(0);
		}
		// start loop
		if(this.bound != 0) this.timer = this.loopSlideshow.periodical(2500, this);
		
		// init pagenav
		this.initPagenav();
	},
	loopSlideshow: function () {
		this.startIndex++
		this.fx.start('opacity', 0, 1);
		
		if(this.startIndex <= this.bound) {
			this.setSlide(this.startIndex);
		} else {
			this.startIndex = 0;
			// show image
			this.setSlide(this.startIndex);
		}
	},
	setSlide: function (index) {
		// show image
		this.fx.start('opacity', 0, 1);
		(function () {
			this.image.set('src', this.src + '_' + index + '.jpg');
		}).bind(this).delay(25)
		// set page
		this.setPage(index);
	},
	initPagenav: function () {
		this.pagenav = $('pagenav');
		// empty
		if($$('#pagenav li')) {
			var anchors = $$('#pagenav li');
			anchors.pop();
			anchors.shift();
			anchors.dispose();
		}
		// create elements
		for(var i = 0; i <= this.bound; i++) {
			// create anchors
			new Element('li').adopt(
				new Element('a', { html: (i + 1), href: '#' }
			)).inject(this.pagenav.getElement('li:last-child'), 'before');
		}
		// save anchors
		this.anchors = $$('#pagenav li a');
		this.anchors.pop();
		this.anchors.shift();
		// set first page active
		this.setPage(0);
		
		// set event on nummeric page
		this.anchors.each(function (item, index) {
			item.removeEvents('click');
			item.addEvent('click', this.movePage.bindWithEvent(this, index));
		}, this);
		
		// set next
		this.next = $('pagenav').getElement('li:last-child a');
		this.next.removeEvents('click');
		this.next.addEvent('click', this.moveNext.bindWithEvent(this));
		// set prev
		this.prev = $('pagenav').getElement('li:first-child a');
		this.prev.removeEvents('click');
		this.prev.addEvent('click', this.movePrev.bindWithEvent(this));
	},
	setPage: function (index) {
		if(this.anchors) {
			this.anchors.set('class', '');
			this.anchors[index].set('class', 'active');
			this.startIndex = index;
		}
	},
	movePage: function (event, index) {
		event.stop();
		// clear timer
		this.timer = $clear(this.timer);
		// show image
		this.setSlide(index);
	},
	moveNext: function (event) {
		event.stop();
		// clear timer
		this.timer = $clear(this.timer);
		// page handling
		if(this.startIndex < this.bound) {
			this.startIndex++;
		} else {
			this.startIndex = 0;	
		}
		this.setSlide(this.startIndex);
	},
	movePrev: function (event) {
		event.stop();
		// clear timer
		this.timer = $clear(this.timer);
		// page handling
		if(this.startIndex != 0) {
			this.startIndex--;
		} else {
			this.startIndex = this.bound;	
		}
		this.setSlide(this.startIndex);
	}
};

// gmaps module
SS.gmaps = {

    init: function () {
        google.load('maps', '2', {
            callback: function () {
                this.initGoogleMaps();
            }.bind(this)
        });
    },

    initGoogleMaps: function () {
        this.map = new google.maps.Map2($('gmaps-container'));
        this.map.addControl(new google.maps.SmallMapControl());
        
		var coordinates = new google.maps.LatLng(47.362221, 8.5142847);
		
		// set ss as center
        this.map.setCenter(coordinates, 16, G_NORMAL_MAP);
		
		// set marker
		var marker = new google.maps.Marker(coordinates);
		
		// init marker
		this.map.addOverlay(marker);
		
		// click event
		/*google.maps.Event.addListener(marker, 'click', function () {
			// init window
        });*/
		var template = '<p style="color:#000;">Schoch&Sonders AG, Markentagentur<br /><nobr>Binzstrasse 23, 8045 Z&uuml;rich</nobr><br /><nobr>Telefon +41 (0)44 383 00 83</nobr></p>';
		marker.openInfoWindowHtml(template);

	}

};








