// Copyright (c) Fotki.com 1998-2003
// All rights reserved

//alex: transitional old->new cart temp js file.

var notOrderedPrint = "uf_not_ordered";
var OrderedPrint = "uf_ordered";

//var notOrderedPrint = "http://images.fotki.com/pixel.gif";
//var OrderedPrint = "http://images.fotki.com/order_print_new-1.gif";

var Furl = window.location.href;
var fotki_hostname2 = location.hostname.match(/fotki\.com$/) ? "fotki.com" : location.hostname;

function CartItemPhoto(qnt) {
   this.types = new Array();
   this.giftQ = 0;
   this.changed = 0;
   this.quantity = (qnt ? qnt : 0);
   this.is_checked_at_page = (qnt? 1 : 0);

   this.switchSel = function(type, idPhoto) {
      this.changed = 1;
      if (this.types[type] == 1) {
         this.types[type] = 0;
         this.is_checked_at_page = 0;

         if (type != 'photo_print' && this.giftQ > -1) {
            this.giftQ--;

            if (this.giftQ == 0) {
               document.getElementsByName('gift'+idPhoto)[0].src = notOrderedPrint;
            }
         }
       Core.switchClassNames(idPhoto,OrderedPrint,notOrderedPrint);
         //document.getElementsByName(type+idPhoto)[0].src = notOrderedPrint;
         try {
            chint.quickHide();
            CartHint_items["quantity"] = (CartHint_items["quantity"] - (this.quantity ? this.quantity : 1));
            if (CartHint_items["quantity"] < 1) Core.setClassName("shopping_cart", "shoppingcartempty");
         } catch (ex) {
            //TODO: Delete
            //alert("cart2_fcart: "+ex);
            return;
         }
         //TopNav.addToShoppingCartItemsCount(-(this.quantity?this.quantity:1));
      } else {
         this.types[type] = 1;
         this.quantity = (this.quantity?this.quantity:1);
         this.is_checked_at_page = 1;

         if (type != 'photo_print') {
            this.giftQ++;
            document.getElementsByName('gift'+idPhoto)[0].src = OrderedPrint;
         }

       Core.switchClassNames(idPhoto,notOrderedPrint,OrderedPrint);
         //document.getElementsByName(type+idPhoto)[0].src = OrderedPrint;
         //order_animation(idPhoto);
         try {
            chint.quickHide();
            if (CartHint_items["quantity"] == 0) Core.setClassName("shopping_cart", "shoppingcart");
            CartHint_items["quantity"] = CartHint_items["quantity"] + 1;
         } catch (ex) {
            //TODO: Delete
            //alert("cart2_fcart: "+ex);
            return;
         }
         //TopNav.addToShoppingCartItemsCount(this.quantity);
      }
   }

   this.getSelect = function(type) {
      return this.types[type];
   }

   this.addSelect = function(type) {
       this.types[type] = 1;

       if (type != "photo_print") {
           this.giftQ++;
       }
   }
}

function PageCartItemPhotos() {
   this.photos = new Array();

   this.getPhoto = function(id) {
      return this.photos[id];
   }

   this.switchPhotoItem = function(idPhoto, type) {
      this.photos[idPhoto].switchSel(type, idPhoto);
      this.generateCookie();
   }

   this.setSelect = function(idPhoto, type) {
      this.photos[idPhoto].addSelect(type);

      if (type == 'photo_print') {
         //document.getElementsByName(type+idPhoto)[0].src = OrderedPrint;
        Core.switchClassNames(idPhoto,OrderedPrint,notOrderedPrint);
      } else {
         document.getElementsByName('gift'+idPhoto)[0].src = OrderedPrint;
      }
   }

   this.setCookie = function(name, value, expires, path, domain, secure) {
      var curCookie = name + "=" + escape(value) +
         ((expires) ? "; expires=" + expires.toGMTString() : "") +
         ((path) ? "; path=" + path : "") +
         ((domain) ? "; domain=" + domain : "") +
         ((secure) ? "; secure" : "");
         document.cookie = curCookie;
   }

   this.generateCookie = function () {
      var expiredate = new Date();
      expiredate.setTime(expiredate.getTime() + 14 * 24 * 60 * 60 * 1000);

      var c = getCookie("fcart2") || '';

      var addToCart = "";

      for (i in this.photos) {
         if (this.photos[i].changed > 0) {
            var indexof = c.indexOf(i);

            if (indexof != -1) {
               //remove the part if its already set (somewhere in other window)
               var indexofend = c.indexOf('*', indexof);
               //c.slice(indexof, indexofend);
               c = c.substring(0, indexof) + c.substring(indexofend+1);
            }

            addToCart += i;

            for (k in this.photos[i].types) {
                addToCart += "," + k + "-" + this.photos[i].types[k];
            }

            addToCart += '*';
         }
      }

      addToCart = c + addToCart;
      this.setCookie("fcart2",addToCart,expiredate,'/','.'+fotki_hostname2);
   }
}

var page = new PageCartItemPhotos();

/*
var panel;


function GiftPanel(id, x, y){
   this.id = id;
   this.divGeneral = document.getElementById('giftPanel');

   this.x = x+14;
   this.y = y+14;

   this.aTshirt = document.getElementById('tshirt');
   this.aMug = document.getElementById('mug');
   this.aMousepad = document.getElementById('mousepad');

   this.aTshirt.href = "javascript:page.switchPhotoItem('" + this.id + "', 'tshirt');";
   this.aMug.href = "javascript:page.switchPhotoItem('" + this.id + "', 'mug');";
   this.aMousepad.href = "javascript:page.switchPhotoItem('" + this.id + "', 'mousepad');";

   this.divGeneral.style.top = this.y+"px";
   this.divGeneral.style.left = this.x+"px";

   document.getElementById('imtshirt').name = 'tshirt' + this.id;
   document.getElementById('immug').name = 'mug' + this.id;
   document.getElementById('immousepad').name = 'mousepad' + this.id;

   document.getElementById('imtshirt').src = page.getPhoto(id).types['tshirt'] == 1 ? OrderedPrint : notOrderedPrint;
   document.getElementById('immug').src = page.getPhoto(id).types['mug'] == 1 ? OrderedPrint : notOrderedPrint;
   document.getElementById('immousepad').src = page.getPhoto(id).types['mousepad'] == 1 ? OrderedPrint : notOrderedPrint;

   this.divGeneral.style.zIndex = 10000;

   this.divGeneral.style.visibility = 'visible';

   this.close = function(){
      this.divGeneral.style.visibility = 'hidden';
   }

}

var panel;

function doShow(e){
   var id = e.target.id;
   if (id.indexOf('divphoto')>-1){
      panel = new GiftPanel(id.substring(3), e.pageX, e.pageY);

   }

}

document.onmousedown = doShow;
*/
