/* setup globals */
var items = {};

jQuery.fn.comments = function( blnDeep ){
  var blnDeep = (blnDeep || false);
  var jComments = $( [] );

  this.each(function( intI, objNode ) {
    var objChildNode = objNode.firstChild;
    var strParentID = $( this ).attr( "id" );

    while (objChildNode){
      if (objChildNode.nodeType === 8){
        jComments = jComments.add(
        "<div rel='" + strParentID + "'>" +
        objChildNode.nodeValue +
        "</div>"
        );
      } else if (blnDeep && (objChildNode.nodeType === 1)) {
        jComments = jComments.add($( objChildNode ).comments( true ));
      }

      objChildNode = objChildNode.nextSibling;
    }

  });

  return( jComments );
}
function enableDropZone() {
  $('.drop-zone').droppable({
    activeClass: 'drop-zone-active',
    hoverClass: 'drop-zone-hover',
    tolerance: 'pointer',
    over: function(event, ui) {
      $(this).removeClass('drop-zone-active');
    },
    out: function(event, ui) {
      $(this).addClass('drop-zone-active');
    },
    drop: function(event, ui) {
      img = ui.draggable.children(':first').clone();
      name = ui.draggable.children(':last').text();
      img.attr({
        'height': parseInt($(this).css('height'), 10),
        'width': parseInt($(this).css('width'), 10)
      });
      $(this).after('<a href="#" class="trash" onclick="removeOption(this);return false;">Remove Swatch</a>');
      $(this).droppable('disable');
      $(this).append(img);
      index = $(this).siblings(':first').text();
      if(name == "Custom Pattern") {
        $(this).append('<input name="swatch_pattern'+index+'" value="'+name+'" type="hidden" />');
      } else {
        $(this).append('<input name="swatch_pattern'+index+'" value="'+name+'" type="hidden" />');
      }
    }
  });
}
function parseComments() {
  $('#attributes').comments().each(function(iter,elem) {
    var item = {};
    arr = $(elem).text().split("|");
    $(arr).each(function(i,e) {
      if( i % 2 == 0) {
        item[e] = arr[i + 1];
      }
      items[item.id] = item;
    });
  });
}
function changePageTo(item) {
  image_refs = item.images.split(' ');
  $('form').attr('action', "http://order.store.yahoo.net/cgi-bin/wg-order?yhst-48024196613911+" + item.id);
  $('#image-views img').each(function(i,e) {
    this.src = image_refs[i];
  });
  $('#image-views a').each(function(i,e) {
    this.href = "javascript:popup('" + image_refs[i] + "');";
  });
  $('#hidden-id').val(item.id);
  $('#size-select').children().remove();
  $(item.size.replace(/["]/g, '').split(' ')).each(function(i,e){
    $('#size-select').append('<option value="'+e+'">'+e+'</option>');
  });
  $('#price').text('$' + item.price);
  $('#custom-options').children().remove();
  $(item.options.split(' ')).each(function(i,e) {
    box = document.createElement('div');
    title = document.createElement('div');
    count = document.createElement('div');
    $(box).addClass('option-box');
    $(title).addClass('drop-title');
    $(count).addClass('option-count');
    $(count).text(i + 1);
    $(box).append(title);
    $(box).append(count);
    switch(e) {
      case '"@SWATCH@"':
        drop = document.createElement('div');
        $(drop).addClass('drop-zone');
        $(box).append(drop);
        break;
      case '"@COLOR@"':
        select = document.createElement('select');
        $(select).attr('name', 'color'+(i + 1));
        elements = [];
        $(item.color.split('"')).each(function(i,e) {
          if(e != "" && e != " ") {
            s = e.replace(/["]/g, '');
            elements.push(s);
          }
        });
        $(elements).each(function(i,e) {
          $(select).append('<option value="'+e+'">'+e+'</option>');
        });
        $(box).append(select);
        break;
      case '"@PIPING@"':
        select = document.createElement('select');
        $(select).attr('name', 'piping'+(i + 1));
        elements = [];
        $(item.piping.split('"')).each(function(i,e) {
          if(e != "" && e != " ") {
            s = e.replace(/["]/g, '');
            elements.push(s);
          }
        });
        $(elements).each(function(i,e) {
          $(select).append('<option value="'+e+'">'+e+'</option>');
        });
        $(box).append(select);
        break;
      case '"@STRIPE@"':
        select = document.createElement('select');
        $(select).attr('name', 'stripe'+(i + 1));
        elements = [];
        $(item.stripe.split('"')).each(function(i,e) {
          if(e != "" && e != " ") {
            s = e.replace(/["]/g, '');
            elements.push(s);
          }
        });
        $(elements).each(function(i,e) {
          $(select).append('<option value="'+e+'">'+e+'</option>');
        });
        $(box).append(select);
        break;
      case '"@DRAWSTRING@"':
        select = document.createElement('select');
        $(select).attr('name', 'drawstring'+(i + 1));
        elements = [];
        $(item.drawstring.split('"')).each(function(i,e) {
          if(e != "" && e != " ") {
            s = e.replace(/["]/g, '');
            elements.push(s);
          }
        });
        $(elements).each(function(i,e) {
          $(select).append('<option value="'+e+'">'+e+'</option>');
        });
        $(box).append(select);
        break;
    }
    $('#custom-options').append(box);
  });
  elements = [];
  $(item.titles.split('"')).each(function(i,e) {
    if(e != "" && e != " ") {
      s = e.replace(/["]/g, '');
      elements.push(s);
    }
  });
  $(elements).each(function(i, e) {
    $('.drop-title:eq(' + i.toString() + ')').text(e);
  });
  enableDropZone();
}
function removeOption(trash) {
  drop_zone = $(trash).siblings(".drop-zone");
  drop_zone.children().remove();
  drop_zone.droppable('enable');
  $(trash).remove();
}
function popup(src) {
	win = window.open('enlarge.html?' + src, "", "top=20,left=20,height=450,width=450,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
}
function validateForm() {
  if($('#custom-options input').length > 0) {
    return true;
  } else {
    alert("You must add a pattern to your shorts before you can checkout.");
    return false;
  }
}
$(document).ready(function() {
  parseComments();
  $('#items').bind('change', function() {
    changePageTo(items[this.value]);
  });
  $('.draggable').bind('mouseenter', function() {
    $(this).css('cursor', 'move');
  });
  $('.draggable').bind('dblclick', function() {
    popup($('span', this).attr('title'));
  });
  $('.draggable').draggable({
    revert: true,
    cursor: 'move',
    helper: 'clone'
  });
  enableDropZone();
  $('#slider').cycle({
    fx:     'slideX',
    speed:  'fast',
    timeout: 0,
    next:   '#right-button',
    prev:   '#left-button'
  });
  $('#contact-form').validate();
});
