/* --- suminteraktiv --- */
/* --- sorgdesign.de --- */

/* --- Produktion: --- */
/* --- Büro Simon Obitz, Berlin 2009 --- */
/* --- www.buerosimonobitz.net --- */



/* --- Struktur: --- */
/* --- 1 - Bilderkarussel --- */
/* --- 2 - Login-Formular --- */
/* --- 3 - Scroll-Listen --- */



/* --- 1 - Bilderkarussel --- */
_carousel = false;

function mycarousel_initCallback(carousel) {

	_carousel = carousel;

	mycarousel_bind_mediacontrol();

	jQuery('#mediaControl ul li').bind('mouseenter', function() {
		carousel.stopAuto();
	});

	jQuery('#mediaControl ul li').bind('mouseleave', function() {
		carousel.startAuto();
	});
	
	if($$('#mediaControl ul li').length <= 2) {
		$$('#mediaControl ul')[0].hide();
	}

	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});

};

function mycarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
	jQuery('#mediaControl ul li#item-' + idx).addClass("currentImage");
};
function mycarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {
	jQuery('#mediaControl ul li#item-' + idx).removeClass("currentImage");
};


function mycarousel_bind_mediacontrol() {

	jQuery('#mediaControl ul li a').bind('click', function() {
		_carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		_carousel.stopAuto();
	});

}

function mycarousel_load() {

	jQuery("#mycarousel").jcarousel({
		scroll: 1,
		auto: 3.5,
		wrap: 'last',
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: mycarousel_initCallback,
		itemVisibleInCallback: { onBeforeAnimation: mycarousel_itemVisibleInCallbackBeforeAnimation },
		itemVisibleOutCallback:{ onBeforeAnimation: mycarousel_itemVisibleOutCallbackAfterAnimation },
		easing: 'swing',
		animation: 600
	});

}

jQuery(document).ready(function() {

	jQuery(".hide").hide();

	mycarousel_load();


	// PNG-Fix for IE5.5 & 6
	jQuery(document).pngFix();

});





/* --- 2 - Login-Formular --- */

LoginText = "Ihr Passwort, bitte.";

function LoginTextFocus() {
	if (document.getElementById("LoginPWD").value == LoginText) { document.getElementById("LoginPWD").value = ""; }
	}

function LoginTextBlur() {
	if (document.getElementById("LoginPWD").value == "") { document.getElementById("LoginPWD").value = LoginText; }
	}

function LoginTextEinsetzen() {
  try
  {
	  document.getElementById("LoginPWD").value = LoginText;
  }
  catch(e) {}
}

function SubmitTesten() {
	if (document.getElementById("LoginPWD").value == LoginText || document.getElementById("LoginPWD").value == "") { return false; }
	}

//document.onload = LoginTextEinsetzen();



// --- Verhindert den Fokus-Rahmen, den der tolle IE so gerne setzt

function ierahmenausblenden() {
	if(document.getElementsByTagName) {
		var a = document.getElementsByTagName("a");
		for(var i = 0; i < a.length; i++) {
			a[i].onfocus = function(){this.blur()};
		}
	}
}

//document.onload = ierahmenausblenden();

// --- --- ---







/* --- 3 - Scroll-Listen --- */

	/**
	*
	*	scroll list class
	*
	**/

	c_scroll_list = Class.create({

		//
		//	initialize
		//
		initialize: function(obj) {

			// settings
			this._handle_height_percentage = 20;
			this._list_container_class = 'scroll-list-wrapper';
			this._scroll_speed = 3; // Pixel pro Frame (1 = langsam, >1 = schnell)

			// system variables
			this._list_elmt = $(obj);
			this._list_container_elmt = this._list_elmt.up('.'+this._list_container_class);
			this._list_container_height = this._list_container_elmt.getHeight();
			this._list_container_offset = this._list_container_elmt.cumulativeOffset();
			this._list_height = this._list_elmt.getHeight()+4;
			this._list_top_range = $R(this._list_container_offset.top, ((this._list_container_height*(this._handle_height_percentage/100))+this._list_container_offset.top));
			this._list_bottom_range = $R((this._list_container_height-this._list_container_height*(this._handle_height_percentage/100))+this._list_container_offset.top, this._list_container_height+this._list_container_offset.top);
			this._tracking_interval = false;
			this._pointerY = false;
			this._pointer_is_hovering_top = false;
			this._pointer_is_hovering_bottom = false;
			this._stop_effect = false;

			// initialize
			this.build_dom();
			this.set_event_handlers();

		},

		//
		//	build/set dom related stuff
		//
		build_dom: function() {

			this._list_elmt.setStyle('position: absolute; top:0; left:0;');

		},

		//
		//	set event handlers
		//
		set_event_handlers: function() {

			this._list_container_elmt.observe('mouseover', function(e) {

				this.set_tracking(e, true);

			}.bind(this));

			this._list_container_elmt.observe('mouseout', function(e) {

				this.set_tracking(e, false);

			}.bind(this));

			this._list_container_elmt.observe('mousemove', function(e) {

				this._pointerY = Event.pointerY(e);

			}.bind(this));

		},

		//
		//	set tracking to true or false
		//
		set_tracking: function(e, state) {

			if(state) {
				this._tracking_interval = new PeriodicalExecuter(function(pe) {
					this.scroll();
				}.bind(this), 0.01);
			}

			else {
				if(this._tracking_interval) {
					this._tracking_interval.stop();
				}
			}

		},

		//
		//	scroll
		//
		scroll: function() {

			_x = false;

			// scroll top
			if(this._list_top_range.include(this._pointerY)) {

				_top = parseInt(this._list_elmt.getStyle('top'))+this._scroll_speed;

				if(_top <= 0) {

					_x = true;
					this._pointer_is_hovering_top = true;
					this._list_elmt.setStyle('top: '+_top+'px;');
				}

			}

			// scroll down
			if(this._list_bottom_range.include(this._pointerY)) {

				_top = parseInt(this._list_elmt.getStyle('top'))-this._scroll_speed;

				if(_top > this._list_container_height-this._list_height) {

					_x = true;
					this._pointer_is_hovering_bottom = true;
					this._list_elmt.setStyle('top: '+_top+'px;');

				}

			}

			// scroll out transition
			if(!_x && (this._pointer_is_hovering_bottom || this._pointer_is_hovering_top)) {

				if(this._stop_effect) this._stop_effect.cancel();

				if(this._pointer_is_hovering_top) {
					_top = parseInt(this._list_elmt.getStyle('top'))+(this._scroll_speed+10);
				} else {
					_top = parseInt(this._list_elmt.getStyle('top'))-(this._scroll_speed+10);
				}

				if(_top <= 0 && _top > this._list_container_height-this._list_height) {
					this._stop_effect = new Effect.Morph(this._list_elmt, {
						style: 'top: '+_top+'px;',
						duration: 0.2
					});
				}

				this._pointer_is_hovering_top = false;
				this._pointer_is_hovering_bottom = false;

			}

		}

	});









	/**
	*
	*	nav jalousie class
	*
	**/

	c_nav_jalousie = Class.create({

		//
		//	initialize
		//
		initialize: function() {

			// settings
			this._nav_container_id = 'Nav1Start';
			this._nav_start_elmt = $$('a.Nav1StartScroll')[0];
			this._effect_duration = 0.35; // Dauer des Effekts in Sekunden
			this._nav_closed_height = 29;
			this._nav_opened_height = 185; /*332*/

			// system variables
			this._nav_container_elmt = $(this._nav_container_id);
			this._is_toggling = false;

			// initialize
			this.build_dom();
			this.set_event_handlers();

		},

		//
		//	build dom
		//
		build_dom: function() {

			this._nav_container_elmt.setStyle('height: '+this._nav_closed_height+'px;');

		},

		//
		//	set event handlers
		//
		set_event_handlers: function() {

			if(this._nav_start_elmt) {
				this._nav_start_elmt.observe('mouseover', function(e) {
					this.toggle(e, 1);
				}.bind(this));
				$$('body')[0].observe('mousemove', function(e) {
					this.toggle(e, 0);
				}.bind(this));
			}

		},

		//
		//	toggle
		//
		toggle: function(e, mode) {

			if(!this._is_toggling) {

				if(mode == 0 && this._nav_container_elmt.hasClassName('nav-opened')) {

					_src_element = Event.element(e).up('#'+this._nav_container_id);
					if(_src_element == undefined && Event.element(e) != this._nav_container_elmt) {

						this._is_toggling = true;

						new Effect.Morph(this._nav_container_elmt, {
							style: 'height: '+this._nav_closed_height+'px;',
							duration: this._effect_duration,
							afterFinish: function() {
								this._nav_container_elmt.className = 'nav-closed';
								this._is_toggling = false;
							}.bind(this)
						});

					}

				}

				if(mode == 1 && this._nav_container_elmt.hasClassName('nav-closed')) {

					this._is_toggling = true;

					new Effect.Morph(this._nav_container_elmt, {
						style: 'height: '+this._nav_opened_height+'px;',
						duration: this._effect_duration,
						afterFinish: function() {
							this._nav_container_elmt.className = 'nav-opened';
							this._is_toggling = false;
						}.bind(this)
					});

				}


			}

		}

	});















	/**
	*
	*	login form class
	*
	**/

	c_login_form = Class.create({

		//
		//	initialize
		//
		initialize: function() {

			// settings
			this._login_button_elmt = $('buttonLogin');
			this._form_container_elmt = $$('.login-form-wrapper')[0];
			this._form_container_opened_width = 256;
			this._effect_duration = 0.2; // Dauer des Effekts in Sekunden

			// system variables
			this._is_toggling = false;

			// initialize
			this.build_dom();
			this.set_event_handlers();

		},

		//
		//	build dom
		//
		build_dom: function() {

			this._form_container_elmt.addClassName('login-closed');
			this._form_container_elmt.setStyle('width: 0px;');

		},

		//
		//	set event handlers
		//
		set_event_handlers: function() {

			if(this._login_button_elmt) {
				this._login_button_elmt.observe('click', function(e) {
					e.stop();
					this.toggle(e);
				}.bind(this));
			}

		},

		//
		//	toggle
		//
		toggle: function(e) {

			if(!this._is_toggling) {

				if(this._form_container_elmt.hasClassName('login-opened')) {

					this._is_toggling = true;

					new Effect.Morph(this._form_container_elmt, {
						style: 'width: 0px;',
						duration: this._effect_duration,
						afterFinish: function() {
							this._form_container_elmt.addClassName('login-closed');
							this._form_container_elmt.removeClassName('login-opened');
							this._is_toggling = false;
						}.bind(this)
					});

				} else {

					this._is_toggling = true;

					new Effect.Morph(this._form_container_elmt, {
						style: 'width: '+this._form_container_opened_width+'px;',
						duration: this._effect_duration,
						afterFinish: function() {
							this._form_container_elmt.addClassName('login-opened');
							this._form_container_elmt.removeClassName('login-closed');
							//$('LoginPWD').focus();
							this._is_toggling = false;
						}.bind(this)
					});

				}


			}

		}

	});











	/**
	*
	*	PROJECT DETAIL LOADER
	*
	**/

	c_project_details = Class.create({

		//
		//	initialize
		//
		initialize: function() {

			// settings
			this._mediencontainer_elmt = $('MedienContainer');
			this._next_project_elmt = $('b-next-project');
			this._medieninfo_elmt = $$('.Medieninfo')[0];
			this._mediacontrol_elmt = $('mediaControl');
			this._ajax_link_elmts = $$('#Nav1ScrollListe ul li a');
			this._ajax_file = './index.php';
			this._active_link_classname = 'Auswahl';

			// system variables
			this._is_transferring = false;

			// initialize
			this.set_event_handlers();

		},

		//
		//	set event handlers
		//
		set_event_handlers: function() {

			// navigation
			this._ajax_link_elmts.each(function(a) {

				a.observe('click', function(e) {

					e.stop();
					this.load_details(a, 'navigation');
				}.bind(this));

			}.bind(this));

			// button "next project"
			this._next_project_elmt.observe('click', function(e) {
				e.stop();
				this.load_details(this._next_project_elmt, 'next-project');
			}.bind(this));


		},

		//
		//	load details
		//
		load_details: function(link, source) {

			if(!this._is_transferring) {

				this._is_transferring = true;
				
				this.show_loading_overlay();

				// update active link in navigation
				this.update_navigation(link, source);

				_href = link.href;

				this._ajax_file = link.href;

				new Ajax.Request(this._ajax_file, {

					method: 'post',

					parameters: { 'ajax': 'true' },

					onSuccess: function(t) {
					  	t = t.responseText.evalJSON();
						this.swap_details(t);
					}.bind(this),

					onFailure: function() {
						this._is_transferring = false;
					}.bind(this)

				});

			}

		},

		//
		//	update navigation
		//
		update_navigation: function(link, source) {

			_next_link = false;

			if(source == 'navigation') {
				this._ajax_link_elmts.invoke('removeClassName', this._active_link_classname);
				link.addClassName(this._active_link_classname);
				this._currentlink = link;
			} else {

				_current_active_link = $$('#Nav1ScrollListe ul li a.'+this._active_link_classname)[0];
				if(_current_active_link) {
					if(_current_active_link.up().next()) {
						_next_link = _current_active_link.up().next().down('a');
					} else {
						_next_link = $$('#Nav1ScrollListe ul li a')[0];
					}
				}
				if(_next_link) {
					this._ajax_link_elmts.invoke('removeClassName', this._active_link_classname);
					_next_link.addClassName(this._active_link_classname);
				}
			}

			/*
			this._nextlink = false
			this._ajax_link_elmts.each(function(a){

        if(!this._nextlink && a.hasClassName(this._active_link_classname))
        {
          this._nextlink = true;
        }
        else if(this._nextlink == true)
        {
          this._nextlink = a.href;
        }
      }.bind(this));

      if(this._nextlink == true)
        this._nextlink = this._ajax_link_elmts[0].href;

      try {
        if(this._nextlink!=false && this._nextlink!=true)
        {
          this._next_project_elmt.href = this._nextlink;
          this._next_project_elmt.setStyle('visibility:visible;');
        }
        else
          this._next_project_elmt.setStyle('visibility:hidden;');
      } catch(e) {}

      delete(this._nextlink);
      			*/

		},

		//
		//	swap details
		//
		swap_details: function(json) {

			this._is_transferring = false;
			
			this.hide_loading_overlay();

			// mediencontainer
			this._mediencontainer_elmt.fade({
				duration: 0,
				to: 0.0001
			});

			_carousel.reset();
			_carousel.size(json.mediencontainer.length);
			json.mediencontainer.each(function(a, b) {
				_carousel.add(b, a);
			});
			_carousel.stopAuto();

			this._mediencontainer_elmt.appear({
				duration: 0.6,
				from: 0.0001,
				to: 1
			});

			// mediacontrol
			//this._mediacontrol_elmt.update(json.mediacontrol);
			
			_anzahl_bilder = json.mediencontainer.length;
			
			_mediacontrol_html = (_anzahl_bilder > 2) ? '<ul>' : '<ul style="display: none;">';
			json.mediencontainer.each(function(a, b) {
				_current = (b == 0) ? ' class="currentImage"' : '';
				_mediacontrol_html += '<li id="item-'+(b+1)+'"'+_current+'><a href="#" onclick="return false;">'+(b+1)+'</a></li>';
			});
			_mediacontrol_html += '</ul>';

			this._mediacontrol_elmt.update(_mediacontrol_html);
			mycarousel_bind_mediacontrol();

			// medieninfo
			this._medieninfo_elmt.hide();
			this._medieninfo_elmt.update(json.medieninfo);
			if(json.medieninfo!=false)
        this._medieninfo_elmt.setStyle('visibility:visible;');
      else
        this._medieninfo_elmt.setStyle('visibility:hidden;');
        
        		this._medieninfo_elmt.appear({ duration: 0.6 });
        

			// button "next project"
			this._ajax_link_elmts.each(function(a) {
//        alert(a.href);
      }.bind(this));


			this._next_project_elmt.href = json.nextprojecthref;
			if(json.nextprojecthref!=false)
        this._next_project_elmt.setStyle('visibility:visible;');
      else
        this._next_project_elmt.setStyle('visibility:hidden;');


		},
		


		
		//
		//	show loading overlay
		//
		show_loading_overlay: function() {
	
			new Insertion.Top(this._mediencontainer_elmt, '<div id="loading-overlay"><span><img src="./files/skin_sorgdesign/img/loading.gif" alt="" /></span></div>');
		
		},
		
		//
		//	hide loading overlay
		//
		hide_loading_overlay: function() {
		
			if($('loading-overlay')) $('loading-overlay').remove();
		
		}
		
		

	});












	/**
	*
	*	on dom loaded
	*
	**/

	Event.observe(document, 'dom:loaded', function() {

		// content scroll list?
		if($('content-scroll-list')) {
			_content_scroll_list = new c_scroll_list('content-scroll-list');
		}

		// nav scroll list?
		if($('nav-scroll-list')) {
			_nav_scroll_list = new c_scroll_list('nav-scroll-list');
		}

		// nav jalousie?
		if($('Nav1Start')) {
			_nav_jalousie = new c_nav_jalousie();
		}

		// login form?
		if($('buttonLogin')) {
			_login_form = new c_login_form();
		}

		// project detail loader?
		if($('MedienContainer')) {
			_project_details = new c_project_details();
		}

	});
