// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {
    var img = new Image
    img.src = '/images/pageloader.gif'

    function createSpinner() {
      return new Element('img', { src: img.src, 'class': 'spinner' })
    }

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
        el.up('.pagination').insert(createSpinner())
        new Ajax.Request(el.href, { method: 'get' })
        e.stop()
      }
    })
  }
})

function translateTo(a) {
jQuery("body").translate("de",a, {async:true, not:"#menu, pre, .jq-translate-ui"})
}

function calcOptions(option){
  size = option.id.split('-')[0];
  optionName = option.id.split('-')[1];
  isMp3 = (optionName == 'mp3') ;
  price_item = $(size + '-price');   // name of the price item to update
  
  
  add = toggleCart(option);   // returns 0 or 1
  value = parseFloat(option.innerHTML.split(' ')[0]); // get value to add for the option
  cur_price = parseFloat(price_item.innerHTML.split(' ')[0]); 
  price = value * add + cur_price;

  $(size + '-' + optionName + '-hidden').setValue((add > 0 ) ? true : false);
  
  if (isMp3)
  {
    competitor_price_item =  $('competitor-' + size );
  
    num_confs_item = $('incl-conf-' + size );
    num_confs = parseFloat(num_confs_item.innerHTML.split(' ')[0]);

    current_competitor_price = parseFloat(competitor_price_item.innerHTML.split(' ')[0]); 
    competitor_price = 69.21 * num_confs * add + current_competitor_price
    competitor_price_item.update(competitor_price.toFixed(2) + " &euro;");
    new Effect.Shake('competitor-' + size , { distance: 5 });
  }

  price_item.update(price.toFixed(2) + " &euro;");
  new Effect.Shake(size + '-price', { distance: 5 });
}


function unsaved_changes(option){
    size = option.id.split('-')[0];
    $(size + '-submit').firstChild.value = "Übernehmen"
    $(size + '-submit').firstChild.className = "buy-button-orange"
}

function calcSingle(option) {
  option_name = option.id.split('-')[0];
  price_item = $('price-' + option_name);
  sum_item = $('single-sum');

  if(option.id.split('-')[1] == 'green'){
     $(option.id).firstDescendant().src = '/images/icon_shoppingcart_red_small.gif';
     option.id = option_name + '-red';
     add = 1.0;
  }
  else {
     $(option.id).firstDescendant().src = '/images/icon_shoppingcart_small.gif';
     option.id = option_name + '-green';
     add = -1.0;
  }

  value = parseFloat(price_item.innerHTML.split(' ')[0]);
  cur_price = parseFloat(sum_item.innerHTML.split(' ')[0]);
  price = value * add + cur_price;

  sum_item.update(price.toFixed(2) + " &euro;");
}

function calcRadio(option) {
  sum_item = $('single-sum');
  room_size_price = parseFloat(option.firstElementChild.value);
  opt_price = mp3Price() + wcfPrice() + intPrice() + statsPrice();
  price = opt_price + room_size_price;

  sum_item.update(price.toFixed(2) + " &euro;");
}

function toggleCart(element){
  if(element.className == "price-green-cart" ){
    element.className = 'price-red-cart';
    add = 1.0;
  }
  else{
    element.className = 'price-green-cart';
    add = -1.0;
  }
  return add;
}

function mp3Price()
{
  price_item = $('price-mp3');
  price = 0
  if($('mp3-red'))
  {
    price = parseFloat(price_item.innerHTML.split(' ')[0]);
  }
  return price
}

function wcfPrice()
{
  price_item = $('price-wcf');
  price = 0
  if($('wcf-red'))
  {
    price = parseFloat(price_item.innerHTML.split(' ')[0]);
  }
  return price
}

function intPrice()
{
  price_item = $('price-int');
  price = 0
  if($('int-red'))
  {
    price = parseFloat(price_item.innerHTML.split(' ')[0]);
  }
  return price
}

function statsPrice()
{
  price_item = $('price-stats');
  price = 0
  if($('stats-red'))
  {
    price = parseFloat(price_item.innerHTML.split(' ')[0]);
  }
  return price
}

function leftArrow () {
  $('price-medium').hide();
  $('price-large').hide();
  $('price-xlarge').hide();
  $('price-seperator').show();
  $('price-free').show();
  $('price-more').show();
  $('header-single').show();
  $('header-multi').setStyle('margin-left: 300px');
}

function rightArrow () {
  $('price-seperator').hide();
  $('price-free').hide();
  $('price-more').hide();
  $('header-single').hide();
  $('header-multi').setStyle('margin-left: 250px');
  new Effect.Appear('price-medium', { duration : 0.5 });
  new Effect.Appear('price-large', { duration : 1 });
  new Effect.Appear('price-xlarge', { duration : 1.5 });
}

function updatePartsPrice(){
  switch ($('parts-amount').getValue()){
    case '10':
      $('price-parts').update('4.95 &euro;');
      break;

    case '25':
      $('price-parts').update('9.95 &euro;');
      break;

    case '50':
      $('price-parts').update('14.95 &euro;');
      break;

    case '100':
      $('price-parts').update('29.95 &euro;');
      break;
  }
}

function resetPartsPrice(){
  option = $('parts-red');
  if(option){
    sum_item = $('single-sum');
    option.firstDescendant().src = '/images/icon_shoppingcart_small.gif';
    option.id = 'parts-green';

    value = parseFloat($('price-parts').innerHTML.split(' ')[0]);
    cur_price = parseFloat(sum_item.innerHTML.split(' ')[0]);
    price = cur_price - value;

    sum_item.update(price.toFixed(2) + " &euro;");
  }
}

function get_price(sectionid, option){
  price = 0.0;
  if($('article_'+option+'_'+sectionid) &&
     $('article_'+option+'_'+sectionid).checked){
    price += parseFloat($(option+'_price_'+sectionid).innerHTML);
  }
  return price;
}

function calc_price(sectionid){
  /* clicked_sectionid = */
  price = 0.0;
  price += get_price(sectionid, 'mp3');
  price += get_price(sectionid, 'wcf');
  price += get_price(sectionid, 'dialin');
  price += get_price(sectionid, 'flatconf');

  $('sum_'+sectionid).innerHTML = price.toFixed(2) + " &euro;"
}

function getPosition(element) {
	var elem=element,tagname="",x=0,y=0;
	try {
		while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined")) {
			y+=elem.offsetTop;
			x+=elem.offsetLeft;
			tagname=elem.tagName.toUpperCase();
			if (tagname=="BODY")
			elem=0;
			if (typeof(elem)=="object")
				if (typeof(elem.offsetParent)=="object")
					elem=elem.offsetParent;
		}
		
	} catch (e){};
	position=new Object();
	position.x=x;
	position.y=y;
	return position;
}

function Position(x,y) {
	this.x=x;
	this.y=y;
	
	this.toString=function() {
		return "X: "+ this.x +" Y:"+this.y;
	}
	
	
	this.add = function (offset) {
		this.x=this.x+parseInt(offset.x);
		this.y=this.y+parseInt(offset.y);
	}
	
	this.sub = function (offset) {
		this.x=this.x-parseInt(offset.x);
		this.y=this.y-parseInt(offset.y);
	}
	
	this.applyToElement = function (elementName) {
		document.getElementById(elementName).style.left=this.x+"px";
		document.getElementById(elementName).style.top=this.y+"px";
	}
	
	this.adjustMinimum = function() {
		if (this.x < 0){this.x = 0;}
  		if (this.y < 0){this.y = 0;}
	}
	this.clone=function() {
		return new Position(this.x,this.y);
	}
};


function BrowserInfo(id){
	this.id = id;

	this.setScreenSizeAndFlashVersion = function () {
		new Ajax.Request('/browser_info/update_info/' + this.id + '?width='+screen.width+"&height="+screen.height+"&flashversion="+GetSwfVer());
	}
	
	this.mouseOff = function(){
		new Ajax.Request('/browser_info/set_features_state/' + this.id + '?m_pointer=0');
	}
	this.mouseOn = function(){
		new Ajax.Request('/browser_info/set_features_state/' + this.id + '?m_pointer=1');
	}
	this.disconnected = function(){
		new Ajax.Request('/browser_info/set_features_state/' + this.id + '?j_connected=0&j_lost=1');
	}
	this.connected = function(){
		new Ajax.Request('/browser_info/set_features_state/' + this.id + '?j_connected=1');
	}

}

function JuggernautHelper() {
	this.ElementName_juggernaut_status='juggernaut_status',
	this.bInfo=null;
	this.lastConnectionStatus=-1;


	this.setBrowserInfo=function(bInfo) {
		this.bInfo=bInfo;
	}
	this.updateJuggernautConnectionStatus= function() {
		updatePage =true;
		 
		if (window.juggernaut==undefined) {
			if (updatePage==true) {
				document.getElementById(this.ElementName_juggernaut_status).innerHTML = "Verbinde";
			}

		} else if (window.juggernaut.is_connected==true) {
			if (this.lastConnectionStatus!=1) {
				this.lastConnectionStatus=1;
				bInfo.connected();
			}
			if (updatePage==true) {
				document.getElementById(this.ElementName_juggernaut_status).innerHTML = "Verbunden";
			}
		} else {
			if (this.lastConnectionStatus!=0) {
				this.lastConnectionStatus=0;
				bInfo.disconnected();
			}
			if (updatePage==true) {
				if (GetSwfVer()<0) {
					document.getElementById(this.ElementName_juggernaut_status).innerHTML = 'Kein <a style="color:red;" href=http://www.adobe.com/go/getflash/>Flash</a>';
				}
				else {
					document.getElementById(this.ElementName_juggernaut_status).innerHTML = "Nicht verbunden";
				}

			}
			//
		}
		
	}
	this.initializeIntervalls=function(instanceName) {
		window.setInterval(instanceName+'.updateJuggernautConnectionStatus()',250);
	}
  
}

