// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//$(function() {
window.onload = function() {
  $(".v2 .option").click(function() {
    $(this).toggleClass("active");
    $(".v2 .option").each(function() {
      $(this).children('input').attr('value', "" ); 
    } );
    $(".v2 .active").each(function() {
        // Show the value of each selected element
        var pickval = $(this).attr("value");
        $(this).children('input').attr('value', pickval ); 
        //alert(pickval);
    });
  });
  $( ".multiselect" ).jScrollPane({showArrows: true, scrollbarWidth: 18});

  $( "#news_list dl.list" ).jScrollPane({showArrows: true, scrollbarWidth: 18});

  $('div.slider_gallery').each(function () {
    var ul = $('ul', this);
    var productWidth = ul.innerWidth() - $(this).outerWidth();
    var left_arrow = $( 'div.slider_gallery div.left_arrow' );
    var right_arrow = $( 'div.slider_gallery div.right_arrow' );

    $( '#slider' ).slider({
      min: 0,
      max: productWidth,
      slide: function (ev, ui) {
        ul.css('left', '-' + ui.value + 'px');
      }, 
      stop: function (ev, ui) {
        ul.animate({ 'left' : '-' + ui.value + 'px' }, 500, 'linear');
      }
    });
    var handle = $( '#slider a' );
    var items = $( 'div.slider_gallery ul.items' );

    handle.css( 'left', '50%' );
    items.css( 'left', '-' + productWidth / 2 + 'px' );

    left_arrow.click( function( e ) {
      handle.css( 'left', '0%' );
      items.css( 'left', '0px' );
    } );

    right_arrow.click( function( e ) {
      handle.css( 'left', '100%' );
      items.css( 'left', '-' + productWidth + 'px' );
    } );
  });

  DescField.bindPasswordField( 'pwd' );
  DescField.bindTextField( 'email', 'E-Mail' );
};

var DescField = new function() {

  this.bindTextField = function( field_id, value ) {
    var textField = $( '#' + field_id );

    textField.focus( function( e ) {
      textField = $( '#' + field_id );

      if( textField.attr( 'value' ) == value ) {
        textField.attr( 'value', '' );
      }
    } );
    
    textField.blur( function( e ) {
      textField = $( '#' + field_id );

      if( textField.attr( 'value' ) == "" ) {
        textField.attr( 'value', value );
      }
    } );
  }

  this.bindPasswordField = function( field_id ) {
    var passwordField = $( '#' + field_id );
    var passwordPlainField = $( '#' + field_id + '_plain' );

    passwordPlainField.focus( function( e ) {
      passwordField.css( 'display', 'inline' );
      passwordField.focus();
      passwordPlainField.css( 'display', 'none' );
    } );
    
    passwordField.blur( function( e ) {
      passwordField = $( '#' + field_id );

      if( passwordField.attr( 'value' ) == "" ) {
        passwordField.css( 'display', 'none' );
        passwordPlainField.css( 'display', 'inline' );
      }
    } );
  };

};

// open external links in new window
$(document).ready(function() {
  $("a").each(function(){
    if(this.href.indexOf(location.hostname) == -1) {
      $(this).attr('target', '_blank');
    }
  }
)});