﻿// Where is the Soap Client?
// Configurate here
var url = '/3d_application/soap_client/client.php';
var ajaxurl = '/Content.Node/scripts/ajax.php';
//var wishlist_url = 'website.php?id=/products/productfinder/wishlist.php';
var wishlist_url = document.location.href;
if (wishlist_url.indexOf("?") < 0) { wishlist_url += "?"; }
var favourites_url = '/Content.Node/scripts/ajax.php?action=getFavoriteTableBox';

var lng = 'en';
var txt_3d = "3D-View";
var txt_loading = "Please wait...";

function initProduct(pid,pgname)
{
  PID = pid; //overwrote global PID
  // get Standard Product Infos
  if (typeof pgname=="undefined") {
   var pars = "action=getProductInfo&id=" +pid+"&lng="+lng;
  } else {
   var pars = "action=getProductInfo&id="+pid+"&pgname="+pgname+"&lng="+lng;
  }
  var myAjax = new Ajax.Updater( 'headline_group', url, { postBody: pars,evalScripts:true, onLoading: showLoading, onComplete: offLoading});

  // get Colors
  var pars = "action=getProductColors&id="+pid+"&lng="+lng;
  var myAjax = new Ajax.Updater( 'select_01_cont', url, { postBody: pars, evalScripts:true, onComplete: function(selectfirst){    var elm = document.getElementsByClassName("s1_link");Element.addClassName(elm[0], "active3d");cid = elm[0].id.replace(/cid_/,"");getSizesForColorOfProduct(pid,cid,"default");  cutLongLinks(elm);} });

  // get SimiliarProducts
  if (typeof pgname=="undefined") {
   var pars = "action=getSimiliarProducts&id="+pid+"&lng="+lng;
  } else {
   var pars = "action=getSimiliarProducts&id="+pid+"&pgname="+pgname+"&lng="+lng;
  }
  var myAjax = new Ajax.Updater( 'simProducts_cont', url, { postBody: pars});

	if (typeof pgname=="undefined") {
//		$('wish_link').href = wishlist_url+"&pid="+pid;
//		$('wish_link').href = wishlist_url+"&favorites=add&type=products&id=9999"+pid;
	} else {
//		$('wish_link').href = wishlist_url+"&pid="+pid+"&pgname=" +pgname;
//		$('wish_link').href = wishlist_url+"&favorites=add&type=products&id=9999"+pid+"&pgname=" +pgname;
	}
}

function selectMe(sElement,sClass) { // set selected Background Color for select options

  sClass1 = sClass.replace(/ active3d/,"");
  sClass2 = sClass1 + " active3d";

  var elm = document.getElementsByClassName(sClass1);
  for (var i=0; i<elm.length; i++) {
    Element.removeClassName(elm[i],"active3d");
  }
  var elm2 = document.getElementsByClassName(sClass2);
  for (var i=0; i<elm2.length; i++) {
    Element.removeClassName(elm2[i],"active3d");
  }
  new Effect.Pulsate(sElement,{duration: 0.7,queue:'end'});
  //Element.setStyle(sElement,  {background:'#369',color:'#fff'} );
  Element.addClassName(sElement, "active3d");
}

function selectFirstSize ()
{
  var elm = document.getElementsByClassName("s2_link");
  Element.addClassName(elm[0], "active3d");
  if (!swf_file.length) {
    $('detail_link_text').innerHTML = "";
  }
  else {
    $('detail_link_text').innerHTML = txt_3d;
  }
  // Added method to cut long links (jQuery needed)
  cutLongLinks(elm);
}

/**
 * Added method to cut long links (jQuery needed)
 * @param {Object} elm	Array of html link objects
 */
function cutLongLinks(elm) {
  for (var i=0; i<elm.length; i++) {
	text = jQuery(elm[i]).text();
	jQuery(elm[i]).attr("title",text);
	if (text.length > 25) {
		shortText = text.substr(0,22)+"...";
		jQuery(elm[i]).text(shortText);
	}
  }
}

function showLoading()
{
  $('detail_pic').innerHTML = ""; Element.setStyle($('detail_pic'),  {display:'none'} );
  $('detail_load').innerHTML = "<img src=\"/Content.Node/productfinder/img/ajax-loader.gif\" style=\"margin-top:85px;margin-left:90px;\">";
  $('select_01_cont').innerHTML = "<img src=\"/Content.Node/productfinder/img/ajax-loader-small.gif\" style=\"margin-top:35px;margin-left:80px;\">";
  $('select_02_cont').innerHTML = "";
  $('simProducts_cont').innerHTML = "<img src=\"/Content.Node/productfinder/img/ajax-loader.gif\" style=\"margin-top:45px;margin-left:160px;\">";
  $('detail_link_text').innerHTML = txt_loading;
}

function offLoading()
{
  $('detail_load').innerHTML = "";
}

function getColorsForProduct(pid)
{
  var pars = "action=getProductColors&id="+pid+"&lng="+lng;
  var myAjax = new Ajax.Updater( 'select_01_cont', url, { postBody: pars});
}

function getSizesForColorOfProduct(pid,cid,pc_pic)
{
  if (pc_pic != 'default') {
  // NOCH KEINE FARBBILDER!
  //$('detail_pic').innerHTML = '<img src="'+pc_pic+'" width="230" height="210" alt="" border="0">';
  }
  var pars = "action=getSizesForColorOfProduct&id="+pid+"&cid="+cid+"&lng="+lng;
  var myAjax = new Ajax.Updater( 'select_02_cont', url, { postBody: pars, evalScripts:true, onLoading: showLoadingSizes, onComplete: selectFirstSize});
}

function showLoadingSizes()
{
  $('select_02_cont').innerHTML = "<img src=\"/Content.Node/productfinder/img/ajax-loader-small.gif\" style=\"margin-top:35px;margin-left:80px;\">";
}

function addToShoppingList()
{
  var  pid = PID;
  var elm = document.getElementsByClassName("s1_link active3d");

  var cid = "";
  if (typeof(elm[0]) != "undefined"){
      cid = elm[0].id.replace(/cid_/,"");
      cid_name = (elm[0].innerHTML);
  } else {
      alert("No color selected");
  }

  var img = document.getElementsByClassName("prodimg_active");
  ppic = img[0].src;
  pgname = $('headline').innerHTML;

  var elm = document.getElementsByClassName("s2_link active3d");

  var sid = "";
  if (typeof(elm[0]) != "undefined"){
      sid = elm[0].id.replace(/sid_/,"");
      sid_name = (elm[0].innerHTML);
  } else {
      alert("No size selected");
  }

  //alert(pid + "|" + cid + "|" +sid);

  var id = escape("9999"+pid+sid_name+cid_name);
//  var id = "9999"+pid;
  var pars = "action=addToShoppingList&favorites=add&type=products&pgpath="+productGroupPath+"&id="+id+"&aid="+pid+"&color="+cid_name+"&size_id="+sid_name+"&title="+pgname+"&ppic="+ppic+"&lng="+lng;
  var myAjax = new Ajax.Request( ajaxurl, { postBody: pars, onLoading: showLoadingList, onComplete: refreshFavouritesList});
//  var myAjax = new Ajax.Request( url, { postBody: pars, onLoading: showLoadingList, onComplete: offLoadingList});
}

function refreshFavouritesList() {
	var pars = "pgpath="+productGroupPath;
	var myAjax = new Ajax.Updater('favouritesBox', favourites_url, { postBody: pars });
//	$('favouritesBox').innerHTML = myAjax;
}

function showLoadingList()
{
  Element.setStyle($('addListLoader'),  {"display":'block'} );
  Element.setStyle($('add_list'),  {"background-image":'url(/Content.Node/productfinder/img/loader.gif)'} );
  $('btt_add_shop').src="/Content.Node/productfinder/img/button_add_shop_ani.gif";
}

function offLoadingList()
{
  Element.setStyle($('addListLoader'),  {"display":'none'} );
  Element.setStyle($('add_list'),  {"background-image":'none'} );
  $('btt_add_shop').src="/Content.Node/productfinder/img/button_add_shop.gif";
}

function listShoppingList()
{
  var pars = "action=listShoppingList"+"&lng="+lng;
  var myAjax = new Ajax.Updater( 'shoppingList', url, { postBody: pars, onComplete: function(selectfirst){new Effect.BlindDown($('shoppingList'),{scaleFrom:1,scaleTo:100,queue: {position:'end', scope: 'menuxscope'} });} });
}

function removeFromShoppingList(list_id)
{
  var pars = "action=removeFromShoppingList&lid="+list_id+"&lng="+lng;
  var myAjax = new Ajax.Request( url, { postBody: pars});
}

function updateShoppingList()
{
  var elm = document.getElementsByClassName("list_quantity");
  var quant_list = "";
  for (var i = 0; i < elm.length; i++ )
  {
    quant_list = quant_list + elm[i].id.replace(/list_/,"") + "|" + elm[i].value;
    if (i != elm.length-1) { quant_list = quant_list + "|" }
  }
  var pars = "action=updateShoppingList&list="+quant_list+"&lng="+lng;
  var myAjax = new Ajax.Request( url, { postBody: pars,  onComplete: listShoppingList });
}


function getProductInfoDesigns(artnum)
{
  var pars = "action=getProductInfoDesigns&id=" +artnum;
  var myAjax = new Ajax.Updater( 'productfinder_components' , url, { postBody: pars, evalScripts:true });

  //var myAjax = new Ajax.Request(url, {postBody: pars, onSuccess:handlerFunc, onFailure:errFunc});

}