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

function read_cookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
		var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
		return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


function radioValue(rObj) {
	for (var i=0; i<rObj.length; i++) if (rObj[i].checked) return rObj[i].value;
	return false;
}

/**
 * Zeigt die Lokalisierungs-Auswahl an
 */
function select_locale() {
  Lightview.show({
    href: '/localities',
    rel: 'ajax',
    options: {
      autosize: true,
      topclose: true,
			overflow: 'hidden',
      ajax: {
        method: 'get',
        evalScripts: true
      }
    }
  })
}

/**
 * Setzt die ausgewählte Lokalisierung
 * 
 * @param {Object} form
 */
function set_locale(form) {
	Lightview.hide();
	var doc     = window.parent.document;
	// 1 Jahr in die Zukunft
	var expires = (new Date((new Date()).getTime() + 1000 * 3600 * 24 * 365)).toGMTString();
	
	['ccode','locale'].each(function(key){
		doc.cookie  = key + '='  + radioValue(form[key]) + ';path=/;expires=' + expires;
	})
	
	var location = doc.location; 
	location.href = location.href.split("?")[0] // + '?ccode=' + radioValue(form['ccode']) + '&locale=' + radioValue(form['locale']);
}

/**
 * 
 * 
 * @param {Object} f
 */
function locale_form_loaded(f){
	f.getElementsBySelector("input[type=radio]").each(function(input){
		var option = input.up("td");
		option.addClassName('option');
		option.down("label").style.display = 'block'
		
		if(input.checked){
			option.addClassName("checked");
		}
		
		option.onmouseover = function(){
			if (!this.hasClassName("checked")) {
				this.addClassName("hover");
			}
		};
		option.onmouseout = function(){
			this.removeClassName("hover");
		};
		option.onclick = function(){
			// aktuelle Auswahl entfernen
			option.up("table").getElementsBySelector("td.option").each(function(td){
				td.removeClassName("checked");
				td.down("input").checked=false;
			});
			// Neue Auswahl setzen
			this.addClassName("checked");
			this.down("input").checked=true;
		};
	});
  Lightview.updateViews();
}


// Workaround für den IE
document.observe("dom:loaded", function() {
	$$('.productListing a img').each(function(img){
		img.onclick= function(){
			document.location.href = this.up("a").href;
		};
	});
		
});


function select_delivery_address(checkout) {
  Lightview.show({
    href: '/account/delivery_addresses?checkout=' + checkout,
    rel: 'ajax',
    options: {
      autosize: true,
      topclose: true,
      ajax: {
        method: 'get',
        evalScripts: true//,
      }
    }
  })
}


function edit_billing_address() {
  Lightview.show({
    href: '/account/edit_address',
    rel: 'ajax',
    options: {
      autosize: true,
      topclose: true,
      ajax: {
        method: 'get',
        evalScripts: true//,
      }
    }
  })
}

function update_billing_address(form) {
  Lightview.show({
    href: '/account/update_address',
    rel: 'ajax',
    title: 'Rechnungsadresse ändern',
    options: {
      autosize: true,
      topclose: true,
      ajax: {
        parameters: Form.serialize(form),
				on204: function(){ Lightview.hide(); document.location.reload() }
      }
    }
  })
}

function select_payment_method(checkout) {
  Lightview.show({
    href: '/account/payment_details?checkout=' + checkout,
    rel: 'ajax',
    options: {
      autosize: true,
      topclose: true,
      ajax: {
        method: 'get',
        evalScripts: true//,
      }
    }
  })
}
