/**
 * Here we Collect some Global Constants
 */
var Constants = {
  FLASHVERSION: '9.0.115',
    AjaxUrl : {
    XMLData: shop.baseUrl + 'productData'
  }
};

/*
 * Prodecural methods for flash movies
 */

/**
 * eyeCatcherAction: common function for new eye-catchers growing in the flash movies
 * HOWTO: Just add another switch-case to the body and put your eyeCatcherAction there
 * @param string which
 */
function eyeCatcherAction( which ) {
  switch ( which ) {
    case 'wohnwochen':
      Common.Thickbox.openThickbox( 'life' , 'Wohnwochen' );
        break;
    case 'andere Aktion':
      //  window.open( ... );
      break;
    default:
      // do nothing
      break;
  }
  return false;
}

/**
 * wrapper for firebugs console.log to use with external interface
 * @param string message
 */
function console_log( message ) {
  try {
    console.log( message );
  } catch( e ) {}
}

/*
 * END Prodecural methods for flash movies
 */


/**
 * eye-catcher loader
 * config values will be hosted outside (markup)
 */
var EyeCatcher = {

  eyeCatchers: null,

  go: function() {
    this.eyeCatchers = eyeCatchersConfig;
    if(swfobject.hasFlashPlayerVersion(Constants.FLASHVERSION)) {
      jQuery.each( EyeCatcher.eyeCatchers , function( flashId , options ) {
        if( $( '#' + flashId ).size() ) {
          EyeCatcher.load( flashId , options );
        }
      });
    }
  },
  load: function( flashId , options ) {
    try {
      var movie = swfobject.getObjectById( flashId );
      movie.loadEyeCatcher( options );
    } catch(e) {
      window.setTimeout(function() {
        EyeCatcher.load( flashId , options );
      }, 2000);
    }
  }
};


/**
 * Teaser
 */

var Teaser =  {
  TEASER_Start: 'start',
  TEASER_CatStart: 'cat_start',
  TEASER_Product: 'changing_product',
  TEASER_fullScreenBtnId: 'fullscreen',

  items : null,
  teasers: null,

  go: function() {
    teasers = new Array();
    if($('#top_teaser_' + this.TEASER_Start).size() > 0) {
      teasers.push( new this.Control( this.TEASER_Start ) );
    }
    if($('#top_teaser_' + this.TEASER_CatStart).size() > 0) {
      teasers.push( new this.Control( this.TEASER_CatStart ) );
    }
    if($('.' + this.TEASER_Product + '_teaser').size() > 0) {
      teasers.push( new this.Control( this.TEASER_Product ) );
    }

    for( var i=0; i<teasers.length; i++ ) {
      teasers[i].init();
    }
  },

  next: function() {
    for( var i=0; i<teasers.length; i++ ) {
      teasers[i].nextItem();
    }
  },
  prev: function() {
    for( var i=0; i<teasers.length; i++ ) {
      teasers[i].prevItem();
    }
  },


  loadData: function( id , data ) {
    if(id == this.TEASER_Start) {
      return this.XMLParser.getStartPageDataByXml( data );
    } else if(id == this.TEASER_CatStart) {
      return this.XMLParser.getAssortmentPageDataByXml( data );
    } else if(id == this.TEASER_Product) {
      return this.XMLParser.getProductTeaserDataByXml( data );
    }
    return null;
  },

  /**
   *
   */
  Control: function( teaserId ) {

    this.teaserId = teaserId;
    this.items = null;
    this.currentItemNo = 0;
    this.teaserHref =  null;
    this.teaserImg =  null;



    /**
     * @param string type
     */
    this.init = function() {
      var self = this;
      var id = this.teaserId;
      if( id === null ) {
        return false;
      }
      var tt = $('#contentbar');
      var href = tt.find('a:first');
      var img = href.children('img');

      this.teaserHref = href;
      this.teaserImg = img;
      // load xml data
      var params = {loadFor: id };
      if( (id === Teaser.TEASER_CatStart || id === Teaser.TEASER_Product)  && typeof ASSORTMENT != 'undefined'&& ASSORTMENT != undefined) {
        params.assortment = ASSORTMENT;
      }
      
      //Hotspot  
      if(id == Teaser.TEASER_Start){
        if (typeof CONTAINERHOTSPOTID != 'undefined' && CONTAINERHOTSPOTID!= undefined){
          params.containerId = CONTAINERHOTSPOTID;
        }
      }
      //Sortimentsteaser
      if(Teaser.TEASER_CatStart== id){
        if (typeof CONTAINERSORTIMENTID != 'undefined' && CONTAINERSORTIMENTID!= undefined){
          params.containerId = CONTAINERSORTIMENTID;
        }
      }
      //topAccessoireTeaser
      if(Teaser.TEASER_Product== id){
        if (typeof CONTAINERACCESSOIREID != 'undefined' && CONTAINERACCESSOIREID!= undefined){
          params.containerId = CONTAINERACCESSOIREID;
        }
      }  
            
      $.get( Constants.AjaxUrl.XMLData , params , function( data ) {
        var result = null;
        self.items = Teaser.loadData(id, data);
        self.setItem(self.items[0]);
        var linkPreFix = id == Teaser.TEASER_Product ? '#changing_product_' : '.top_teaser_nav_';
        $(linkPreFix + 'prev').click( function() {
          self.prevItem();
          return false;
        });
        $(linkPreFix + 'next').click( function() {
          self.nextItem();
          return false;
        });

        // changing_product_prev
        if( swfobject.hasFlashPlayerVersion(Constants.FLASHVERSION) ) {
          /*
          $('#fullscreen').click(function(){
            self.goFullScreen();
            return false;
          }); */
        } else {
          $('#fullscreen').hide();
        }

      } , 'xml' );
      return true;
    };


    this.getCurrentItem =  function() {
      return this.items[this.currentItemNo];
    };

    this.setItemForFlashHelper = function (item, flashId) {
      if(swfobject.hasFlashPlayerVersion(Constants.FLASHVERSION)) {
        try {
          var movie = swfobject.getObjectById( flashId );
          movie.setItem(item);
        } catch(e) {
          var teaser = this;
          window.setTimeout(function() {
            teaser.setItemForFlashHelper(item,flashId);
          }, 200);
        }
      }
    };

    this.setItemForFlash = function(item) {
      this.setItemForFlashHelper(item, this.teaserId);
      this.setItemForFlashHelper(item,Teaser.TEASER_fullScreenBtnId);
    };

    this.setItem = function( item ) {
      if( this.teaserId == Teaser.TEASER_Start ) {
        // noflash
        this.teaserHref.attr('href', item.assortmentlink);
        this.teaserImg.attr('src', item.landscapeimage);
        // flash/...
        this.setItemForFlash(item);
      } else if( this.teaserId  == Teaser.TEASER_CatStart ) {
        // noflash
        this.teaserHref.attr('href', item.subassortmentlink);
        this.teaserImg.attr('src', item.landscapeimage);
        var productContainer = $('.top_teaser_product');
        var imageHref = productContainer.children('div.product_image').children('a');
        imageHref.attr('href', item.detaillink);
        imageHref.children('img').attr('src', item.detailimage);
        var productDetailContainer = productContainer.children('.product_content');
        productDetailContainer.children('h3').html( item.itemtitle );
        this.setRating( productDetailContainer.children('.product_voting') , item.rating );
        var priceAndLinks = productDetailContainer.children('p');
        priceAndLinks.children('span.bold').html( item.price);
        priceAndLinks.children('a.product_link').attr('href' , item.detaillink );
        priceAndLinks.children('a.cat_link').attr( 'href' , item.subassortmentlink );
        priceAndLinks.children('a.cat_link').html(item.subassortmentname );

        if( item.colorsavailable ) {
          productDetailContainer.children('#color_facets').show();
        } else {
          productDetailContainer.children('#color_facets').hide();
        }
        // flash/...
        this.setItemForFlash(item);
      } else if( this.teaserId ==  Teaser.TEASER_Product ) {
        var productContainer = $( '#' + this.teaserId + "_teaser" );
        var imageHref = productContainer.find('div.product_image').children('a');
        imageHref.attr('href', item.detaillink);
        imageHref.children('img').attr('src', item.detailimage);
        var productDetailContainer = productContainer.find('.product_content');
        productDetailContainer.children('h3').html( item.title );
        this.setRating( productDetailContainer.children('.product_voting') , item.rating );
        var priceAndLinks = productDetailContainer.children('p');
        priceAndLinks.children('span.bold').html( item.price);
        priceAndLinks.children('a').attr('href' , item.detaillink );
      }
    };

    this.nextItem =  function() {
      if(this.currentItemNo == this.items.length-1) {
        this.currentItemNo = 0;
      } else {
        this.currentItemNo++;
      }
      var nextitem = this.items[this.currentItemNo];
      this.setItem( nextitem );
    };

    this.prevItem =  function() {
      if(this.currentItemNo == 0) {
        this.currentItemNo = this.items.length-1;
      } else {
        this.currentItemNo--;
      }
      var prevItem = this.items[this.currentItemNo];
      this.setItem(prevItem);
    };

    this.setRating =  function( element , rating ) {
      var i;
      if( element.hasClass('voting_none') ) {
        element.removeClass('voting_none');
      }
      for(i=0; i<6; i++) {
        if( element.hasClass('voting_' + i ) ) {
          element.removeClass('voting_' + i );
        }
      }
      if (rating!=null && rating!="") {
        element.addClass('voting_' + rating );
      } else {
        element.addClass('voting_none');
      }
      element.show();
      return element;
    };
  },


  /**
   * Several XML Paraser for Teaser Type
   */
  XMLParser: {
    /**
     * Parse XML for Start Page Header
     * @param string xml
     * @return array StartPageItem
     */
    getStartPageDataByXml: function( xml ) {
      var startPageItems = new Array();
      var items = $(xml).find( 'page' );
      items.each(function() {
        var item = $( this );
        item.each(function(){
          var startPageitem = new Teaser.StartPageItem();
          startPageitem.assortmentLink = $( this ).children('assortmentlink').text();
          startPageitem.landscapeimage = $( this ).children('landscapeimage').text();
          startPageitem.fulllandscapeimage = $( this ).children('fulllandscapeimage').text();

          var products  = $( this ).find('product');
          startPageitem.products = new Array();
          products.each(function() {
            var p = $( this );
            var product = new Teaser.StartPageItemProduct();
            product.id = p.attr( 'id' );
            product.price = p.children('price').text();
            product.itemtitle = p.children('title').text();
            product.xpos = p.children('xpos').text();
            product.ypos = p.children('ypos').text();
            product.detaillink = p.children('detaillink').text();
            product.subassortmentlink = p.children('subassortmentlink').text();
            product.subassortmentname = p.children('subassortmentname').text();

            product.rating = p.children('rating').text();
            startPageitem.products.push( product );
          });
          startPageItems.push( startPageitem );
        });
      } );
      return startPageItems;
    }, // end getStartPageDataByXml

    /**
     * Parse XML for Assortment Start
     * @param string xml
     * @return array AssortmentPageProduct
     */
    getAssortmentPageDataByXml: function(xml) {
      var assortmentpageItems = new Array();
      var items = $(xml).find('product');
      items.each(function(){
        var p = $( this );
        var product = new Teaser.AssortmentPageProduct();
        product.id = p.attr( 'id' );;
        product.price =  p.children('price').text();;
        product.itemtitle = p.children('title').text();
        product.colorsavailable = p.children('colors-available').text() == 'true' ? true : false;
        product.detailimage = p.children('detailimage').text();
        product.detaillink = p.children('detaillink').text();
        product.landscapeimage = p.children('landscapeimage').text();
        product.fulllandscapeimage = p.children('fulllandscapeimage').text();
        product.teaserlink=p.children('teaserlink').text();
        product.subassortmentlink = p.children('subassortmentlink').text();
        product.subassortmentname = p.children('subassortmentname').text();
        product.rating = p.children('rating').text();;
        assortmentpageItems.push( product );
      });
      return assortmentpageItems;
    },

    getProductTeaserDataByXml: function(xml) {
      var products = new Array();
      var items = $(xml).find('product');
      items.each(function(){
        var p = $( this );
        var product = new Teaser.Product();
        product.id = p.attr( 'id' );
        product.price =  p.children('price').text();;
        product.title = p.children('title').text();
        product.detailimage = p.children('detailimage').text();
        product.detaillink = p.children('detaillink').text();
        product.rating = p.children('rating').text();;
        products.push( product );
      });
      return products;
    }

  }, // XMLParser

  /**
   * Class StartPageItem for Asoortment Objects
   */
  StartPageItem: function() {
    this.assortmentlink =  '';
    this.landscapeimage = '';
    this.products = new Array();
  }, // end StartPageItem

  /**
   * Class StartPageItemProduct for Products in StartPageItems
   */
  StartPageItemProduct: function() {
    this.id = 0;
    this.price = 0;
    this.itemtitle = '';
    this.xpos = 0;
    this.ypos = 0;
    this.detaillink = '';
    this.subassortmentlink = '';
    this.rating = 0;
  }, // end StartPageItemProduct

  /**
   * Class AssortmentPageProduct for Products in
   */
  AssortmentPageProduct: function() {
    this.id = 0;
    this.price = 0;
    this.itemtitle = '';
    this.colorsavailable = false;
    this.detailimage = '';
    this.detaillink = '';
    this.landscapeimage = '';
    this.subassortmentlink = '';
    this.subassortmentname = '';
    this.rating = 0;
  },

  Product: function() {
    this.id = 0;
    this.price = 0;
    this.title = '';
    this.detailimage = '';
    this.detaillink = '';
    this.rating = 0;
  }

}; // end Teaser


$(document).ready(function(){
    Teaser.go();
    // EyeCatcher.go(); TODO reparieren, wenn nötig
});
