var Site = {
	
	start: function(){
		
    new SmoothScroll();
    if ($('accordion')) Site.createAccordion();
    if ($('globalSearch')) Site.parseGlobalSearch();
    if ($$('#content .client').length > 0) Site.parseClients();
    if ($$('#content .input').length > 0) Site.parseInputFields();
    if ($$('#content .csc-sitemap').length > 0) Site.parseSitemap();
    if ($$('#sidebar .slot').length > 0) Site.parseSlots();
    Site.parseTitles();
    if ($$('table.typo3-editPanel').length > 0 && $('editPanel-disable')) Site.editPanel();
	},
  
  
  editPanel: function(){
    var disableLink = $('editPanel-disable');
    
    disableLink.addEvent("click", function(event) {
      var items = $('root').getElements('.typo3-editPanel');
      items.each(function(item, i) {
        if(item.getStyle('display') != 'none') {
          item.setStyle('display', 'none');
        } else {
          item.setStyle('display', 'block');
        }
      });
    });
    
    /*
    var items = $('root').getElements('.typo3-editPanel');
    
    items.each(function(item, i) {
      item.setStyle('opacity', '0.3');
      
      item.addEvent("mouseenter", function(event) {
        item.setStyle('opacity', '1');
      });
      item.addEvent("mouseleave", function(event) {
        item.setStyle('opacity', '0.3');
      });
    });
    */
    
  },
  
  parseTitles: function(){
    /*if($('navigation') && $('navigation').getElements('.sIFR-alternate').length > 0) {
      var navigation_items = $('navigation').getElements('.sIFR-alternate a');
      
      navigation_items.each(function(item, i) {
        if(item.getProperty('title') != null) {
          item.getParent().getParent().setProperty('title', item.getProperty('title'));
        }
      });
      
      Site.createTips($('navigation').getElements('li'));
    }*/ /*else if($('navigation')) {
      Site.createTips($('navigation').getElements('a'));
    }*/
    
    /*if($('subnavigation') && $('subnavigation').getElements('.sIFR-alternate').length > 0) {
      var subnavigation_items = $('subnavigation').getElements('.sIFR-alternate a');

      subnavigation_items.each(function(item, i) {
        if(item.getProperty('title') != null) {
          item.getParent().getParent().setProperty('title', item.getProperty('title'));
        }
      });
      
      Site.createTips($('subnavigation').getElements('li'));
    }*/ /*else if($('subnavigation')) {
      Site.createTips($('subnavigation').getElements('a'));
    }*/
    
    if ($('main').getElements('acronym').length > 0) Site.createTips($('main').getElements('acronym'));
    if ($('root').getElements('span').length > 0) Site.createTips($('root').getElements('span'));
    if ($('root').getElements('a').length > 0) Site.createTips($('root').getElements('a'));
    //if ($('top').getElements('a').length > 0) Site.createTips($('top').getElements('a'));
    //if ($('main').getElements('a').length > 0) Site.createTips($('main').getElements('a'));
    //if ($('footer').getElements('a').length > 0) Site.createTips($('footer').getElements('a'));
    if ($('root').getElements('img').length > 0) Site.createTips($('root').getElements('img'));
    
    if ($$('.input').length > 0) Site.createInputTips($$('.input'));
    if ($$('input.button').length > 0) Site.createTips($$('input.button'));
  },
  
  createTips: function(items){
    var Tips1 = new Tips(items, {
    	initialize:function(){
    		this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 200, wait: false}).set(0);
    	},
    	onShow: function(toolTip) {
    		this.fx.start(1);
    	},
    	onHide: function(toolTip) {
    		this.fx.start(0);
    	}
    });
  },
  
  createInputTips: function(items){
    var inputTips = new Tips(items, {
      killOnFocus: true,
      killOnClick: true,
    	initialize:function(){
    		this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 200, wait: false}).set(0);
    	},
    	onShow: function(toolTip) {
    		this.fx.start(1);
    	},
    	onHide: function(toolTip) {
    		this.fx.start(0);
    	}
    });
  },
    
  createInternalLinks: function(){
    var items = $$('.internal-link');
    
    items.each(function(item, i) {
      if(item.getProperty('title') != null) {
        item.setProperty('title', item.getProperty('title')+' - Internal link');
      }
    });
  },
  
  createExternalLinks: function(){
    var items = $$('.external-link');
    
    items.each(function(item, i) {
      if(item.getProperty('title') != null) {
        item.setProperty('title', item.getProperty('title')+' - External link');
      }
    });
  },
  
  createExternalLinksNewWindow: function(){
    var items = $$('.external-link-new-window');
    
    items.each(function(item, i) {
      item.setProperty('onclick','window.open(this.href);return false;');
      if(item.getProperty('title') != null) {
        item.setProperty('title', item.getProperty('title')+' - External link in a new window');
      }
    });
  },
  
  parseGlobalSearch: function(){
    // Set button
    var submit = $('globalSearchSubmit');
    var button = new Element('a', {title: submit.getProperty('title')});
    button.injectBefore(submit);
    button.setStyle('display', 'block');
    button.addClass('button');
    button.setProperty('title', submit.getProperty('title'));
    button.cloneEvents(submit);
    submit.setStyle('display', 'none');
    
    var input = $('globalSearch').getElement('input.input');
    var defaultInputValue = $('globalSearch').getElement('input.input').getProperty('value');
    
    var inputBgFx = new Fx.Style(input, 'background-color', {wait: false, duration: 300, transition: Fx.Transitions.Quint.easeOut});
    var inputEmptyBgFx = new Fx.Style(input, 'background-color', {wait: false, duration: 600, transition: Fx.Transitions.Quint.easeOut});
    
    input.addEvent("mouseenter", function(event) {
        inputBgFx.stop();
      inputBgFx.start(input.getStyle('background-color'),'#3a84f5');
  	});
    
    input.addEvent("mouseleave", function(event) {
      inputBgFx.stop();
      inputBgFx.start(input.getStyle('background-color'),'#d6d6d6');
  	});
      
    input.addEvent("click", function(event) {  
      //input.addClass('currentInput');
      if(input.getProperty('value') == defaultInputValue) {
        input.setProperty('value', '');
      }
      inputBgFx.stop();
      inputBgFx.start(input.getStyle('background-color'),'#3a84f5');
  	});
    
    input.addEvent("blur", function(event) {
      //input.removeClass('currentInput');
      if(input.getProperty('value') == '') {
        input.setProperty('value', defaultInputValue);
      }
      inputBgFx.stop();
      inputBgFx.start(input.getStyle('background-color'),'#d6d6d6');
  	});
       
  	button.addEvent("click", function(event) {
      if(input.getProperty('value') == '' || input.getProperty('value') == defaultInputValue) {
        new Event(event).stop();
        inputBgFx.stop();
        inputEmptyBgFx.start('#c4352d','#d6d6d6');
      } else {
        $('globalSearch').submit();
        button.addClass('activeButton');
      }
  	});
    button.addEvent("mouseenter", function(event) {
      button.addClass('hoverButton');
      button.removeClass('activeButton');
  	});
    
    button.addEvent("mouseleave", function(event) {
      button.removeClass('hoverButton');
      button.removeClass('activeButton');
  	});
  },
  
	createAccordion: function(){
		var items = $$('#accordion li');
    var activeItem = undefined;
    var originalWidth = 320;
    var deselectedWidth = 230;
    var selectedWidth = 500;
    var o = {};
    var fx = new Fx.Elements(items, {wait: false, duration: 600, transition: Fx.Transitions.Quint.easeOut});
    items.each(function(item, i) {
      item.setStyle('overflow', 'hidden');
      
      // Setting default state
      if(i == 0) {
        o[i] = {width: [item.getStyle("width").toInt(), selectedWidth]};
        activeItem = item;
      }
      if(i == 1 || i == 2) {
        var w = item.getStyle("width").toInt();
    	  if(w != deselectedWidth) o[i] = {width: [w, deselectedWidth]};
      }
      
    	item.addEvent("mouseenter", function(event) {
        if(activeItem == item) { return; }
        var o = {};
        
        if(activeItem != undefined) {
          // new sIFR hack
          var headlines = activeItem.getElements('.sIFR-flash');
          headlines.each(function(headline, i) {
            obj = sIFR.getCallbackByFlashElement(headline);
            obj.changeCSS({
              '.sIFR-root': { 'color': '#7d7d7d', 'text-transform': 'uppercase', 'leading': '0', 'background-color': '#ffffff' },
              'a': { 'color': '#7d7d7d', 'text-decoration': 'none' },
              'a:hover': { 'color': '#7d7d7d' }
            });
          });
          activeItem.removeClass('currentAccordion');
        }
        item.addClass('currentAccordion');
        
        activeItem = item;
        
        // new sIFR hack
        var headlines = activeItem.getElements('.sIFR-flash');
        headlines.each(function(headline, i) {
          obj = sIFR.getCallbackByFlashElement(headline);
          obj.changeCSS({
            '.sIFR-root': { 'color': '#ffffff', 'text-transform': 'uppercase', 'leading': '0', 'background-color': '#3a84f5' },
            'a': { 'color': '#ffffff', 'text-decoration': 'none' },
            'a:hover': { 'color': '#ffffff' }
          });
        });

    		o[i] = {width: [item.getStyle("width").toInt(), selectedWidth]}
    		items.each(function(other, j) {
    			if(i != j) {
    				var w = other.getStyle("width").toInt();
    				if(w != deselectedWidth) o[j] = {width: [w, deselectedWidth]};
    			}
    		});

        fx.start(o);
    	});
      if(i == 2) {
        fx.start(o);
      }
    });
  },
  
  parseSlots: function(){
		var items = $$('#sidebar .slot');
      
    items.each(function(item, i) {
      if(item.hasClass('slot-special') == false) {
        var bgFx = new Fx.Style(item, 'background-color', {wait: false, duration: 300, transition: Fx.Transitions.Quint.easeOut});
        
        item.setStyle('background-color', '#DADADA');
        
      	item.addEvent("mouseenter", function(event) {
          bgFx.stop();        
          item.addClass('currentSlot');
          bgFx.start(item.getStyle('background-color'),'#cbdff6');
      	});
        
        item.addEvent("mouseleave", function(event) {
          bgFx.stop();
          item.removeClass('currentSlot');
          bgFx.start(item.getStyle('background-color'),'#DADADA');
      	});
      }
    });
  },
  
  parseClients: function(){
   var items = $$('#content .client');
   var activeItem = undefined;
   
   // Enable the arrows 
   items.each(function(item, i) {
     item.setStyle('border-bottom', '1px solid #e3e3e3');
     item.getElement('h2').setStyles({'cursor': 'pointer'});
     item.getElement('h2').addEvent("mouseenter", function(event) { item.getElement('h2').setStyle('text-decoration','underline'); });
     item.getElement('h2').addEvent("mouseleave", function(event) { item.getElement('h2').setStyle('text-decoration','none'); });
     item.getElement('div.status').setStyles({'display': 'block'});
     item.getElement('p').setStyles({'margin-right': '10px'});
   });
   
   // Create accordion of the client divs   
   var acc = new Accordion('.client h2', '.client p', {
    	opacity: true,
      show: 0,
      transition: Fx.Transitions.Quint.easeOut,
      duration: 300,
      
    	onActive: function(toggler, element){
        element.setStyle('padding', '0 0 .7em 0');
        toggler.getParent().addClass('currentClient');
        toggler.setStyles({'cursor': 'auto'});
        toggler.addEvent("mouseenter", function(event) { toggler.setStyle('text-decoration','none'); });
    	},
     
    	onBackground: function(toggler, element){
        element.setStyle('padding', '0');
        toggler.getParent().removeClass('currentClient');
        toggler.setStyles({'cursor': 'pointer'});
        toggler.addEvent("mouseenter", function(event) { toggler.setStyle('text-decoration','underline'); });
    	}
    }, $('content'));
  },
  
  parseInputFields: function(){
   var items = $$('#content .input');
   
   items.each(function(item, i) {
      item.addEvent("mouseenter", function(event) {
        item.addClass('hoverInput');
    	});
      
      item.addEvent("mouseleave", function(event) {
        item.removeClass('hoverInput');
    	});
      
      item.addEvent("blur", function(event) {
        item.removeClass('currentInput');
    	});
      
    	item.addEvent("focus", function(event) {
        item.addClass('currentInput');
    	});
      
      item.addEvent("blur", function(event) {
        item.removeClass('currentInput');
    	});
      
      if(i == 0) {
        item.focus();
      }
    });
  },
  
  createContactSteps: function(){
   var index = 1;
   var form = $$('#content .contact form');
   var steps = $$('#content .contact .steps');
   var submit = $('submit');
   var current = steps[0];
   
   submit.setProperty('value', 'Weiter');
   
   steps.each(function(step, i) {
     if(i > 0) {
       //step.setStyle('margin', '0');
       step.slideFx = new Fx.Slide(step, {wait: false, duration: 500, transition: Fx.Transitions.Quint.easeOut});
       step.styleFx = new Fx.Style(step, 'opacity', {wait: false, duration: 1500, transition: Fx.Transitions.Quint.easeOut});
       step.slideFx.hide();
     }
   });
   
   
   submit.addEvent("click", function(event) {
     if(index < steps.length) {
       var step = steps[index];
       step.styleFx.start(0,1);
       step.slideFx.slideIn();
       
       current = step;
       
       if(index == steps.length - 1) {
         if(submit) submit.setProperty('value', 'Submit');
       }
       
       index++;
       
       new Event(event).stop();
     }
   });
  },
  
  parseSitemap: function(){
    var items = $$('#content .csc-sitemap ul li');
      
    items.each(function(item, i) {
      var colorOriginal = item.getElement('a').getStyle('color');
      var colorFx = new Fx.Style(item.getElement('a'), 'color', {wait: false, duration: 300, transition: Fx.Transitions.Quint.easeOut});
      
    	item.getElement('a').addEvent("mouseenter", function(event) {
        if(item.getElement('ul') != null) {
          item.addClass('currentSitemap');
        }
    	});
      
      item.getElement('a').addEvent("mouseleave", function(event) {
        if(item.getElement('ul') != null) {
          item.removeClass('currentSitemap');
          //colorFx.stop();
          //colorFx.start('#ff6600',colorOriginal);
        }
    	});
    });
  }

};

window.addEvent('load', Site.start);


// Flash <> JS communication
var currentPosition;
var currentItem;
function sendEvent(typ,prm) { thisMovie("mpl").sendEvent(typ,prm); };
function thisMovie(movieName) {
    if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};
