function FVShoppingCartCode(){
   if (jQuery('.crtAdd').length > 0)
   jQuery( '.crtAdd' ).ajaxForm({
      beforeSubmit: function( formData, jqForm, options ){
         //serialize form data; append to the form action; tack on ThickBox params
         var URL = jqForm[0].action + "?" + jQuery.param(formData);
         URL += "&height=220&width=600"+"&PPEmail="+g_PPEmail+"&CARTDIR="+g_CARTDIR;

         var form = jqForm[0]; 
         if( form.shp ){
            var shipping = form.shp.value;
            if (form.shp.selectedIndex==0) {          // force a selection
               alert ("Make a shipping selection!");
               return false;
            }

            var ary = shipping.split (" ");          // break apart
            for (j=0; j<ary.length; j++) {  
               if (ary[j].length < 4) continue;
               tok = ary[j].substring (0,3); // first 3 chars
               val = ary[j].substring (3);   // get data
               if (tok == "hn=")             // value for item handling
                  URL += "&" + "handling=" + val;
               if (tok == "hc=")             // value for handling cart
                  URL += "&" + "handling_cart=" + val;
               if (tok == "s1=")             // value for shipping
                  URL += "&" + "shipping=" + val;
               if (tok == "s2=")             // value for shipping2
                  URL += "&" + "shipping2=" + val;
            }
         }
         //call ThickBox directly
         var caption = "My Shopping Cart";
         var imageGroup = false;
         tb_show(caption,URL,imageGroup);
         //cancel the form submission by returning false
         return false;
      }
   });
}

function FVMKTLoadProduct( strLink ){
   jQuery.post( strLink, { fv_mpm_template_redirect : "yes" }, function( strData ){
      jQuery( "#single-product" ).html( strData );
      var strTitle = strData.match( /\<h3\>(.*?)\<\/h3\>/ );
      strTitle = ": " + strTitle[1];
      jQuery( "#product-title" ).html( strTitle );

      FVShoppingCartCode();
   });
   window.scrollTo( 0, 0 );
}

function FVMKTLoadCat( strLink ){
   jQuery.post( strLink, { fv_mpm_template_redirect : "yes" }, function( strData ){
      jQuery( "#products-cat" ).html( strData );
   });
   return false;
}

function FVMKTChangeDateLoadProducts( strLink, strDate ){
   jQuery.post( strLink,
      {
         fv_mpm_template_redirect : "yes",
         delivery_date_change: strDate
      }, 
      function( strData ){
         var jdomProducts = jQuery( "#products-cat" );
         if( 0 < jdomProducts.length ) jQuery( "#products-cat" ).html( strData );
      }
   );
}

function FVMKTUpdateDate( strDate ){
   jQuery( "#left-side-date-from" ).val( strDate );
}

function FVMKTLoadReferencedProduct( strLink, strCatLink ){
   if( 0 >= strLink.length || 0 >= strCatLink.length ) return true;

   var strForm = '<form id="fvredirform" action="' + strCatLink + '" method="post"><input type="hidden" name="fv-redir-link" value="' + strLink + '" /></form>';
   jQuery( "#single-product" ).append( strForm );
   document.getElementById( "fvredirform" ).submit();
   return false;
}

function ShowInLightbox( strUrl ){
   if( !g_bAnimationInProgress ){
      tb_show( "", strUrl, false );
      g_strRunLightbox = "";
   }else g_strRunLightbox = strUrl;

   return false;
}

var g_strImageLinedUp = "";
var g_strShowedImage = "addImg";
var g_strShowImage = "";
var g_bAnimationInProgress = false;
var g_strRunLightbox = "";

function FVFadeOutImage(){
   if( g_strShowedImage ) jQuery( "#" + g_strShowedImage ).fadeOut( 500, FVFadeInImage );
   else alert( "No Showed Image !" );
}

function FVShowImageToMain( strImage ){
   g_strImageLinedUp = strImage;
   if( strImage == g_strShowedImage ) return false;
   if( g_bAnimationInProgress ) return false;
   g_bAnimationInProgress = true;

   g_strShowImage = strImage;
   window.setTimeout( FVFadeOutImage, 200 );
}

function FVFadeInImage(){
   if( g_strImageLinedUp != g_strShowImage ) g_strShowImage = g_strImageLinedUp;
   if( g_strShowImage ) jQuery( "#" + g_strShowImage ).fadeIn( 500, FVFadeSetProperImages );
   else alert( "No Image to show !" );
}

function FVFadeSetProperImages(){
   g_strShowedImage = g_strShowImage;
   g_strShowImage = "";
   g_strImageLinedUp = "";
   g_bAnimationInProgress = false;
   if( 0 < g_strRunLightbox.length ) ShowInLightbox( g_strRunLightbox );
}


var g_bEditShown   = false;
var g_bSaveReorder = false;
var g_strProducts  = "";

function FVShowEdit(){
   if( g_bEditShown ){
      jQuery( ".magic-reorder" ).fadeOut( 500 );
      g_bEditShown = false;
   }else{
      jQuery( ".magic-reorder" ).fadeIn( 500 );
      g_bEditShown = true;
   }
}

function FVUpdateReorder( bSave ){
   g_strProducts = "";
   jQuery( "#products-cat h5" ).each( function( i, prod ){
      g_strProducts += prod.id.substr( 5 ) + ",";
   });

   g_strProducts = g_strProducts.substr( 0, g_strProducts.length - 1 );
   if( bSave ) document.getElementById( "fv-reorder" ).value = g_strProducts;
   jQuery( "#reorder-form" ).submit();
}

function FVReorderProductsStart(){
   jQuery( "#products-cat a" ).removeAttr( "onclick" );
   jQuery( "#products-cat a" ).attr( "href", "#" );
   jQuery( "#products-cat" ).sortable({ items: 'h5' });
   jQuery( "#reorder-products" ).removeClass( "magic-cursor" );
   jQuery( "#reorder-start" ).fadeOut( 300, function(){
      jQuery( "#reorder-save" ).fadeIn( 300 );
   });
   g_bSaveReorder = true;
}

