function LoadGood(id,size,name)
{
$("#TB_ajaxContent").html("<center><img src=\"/i/loading.gif\" style=\"margin:10px;\"><BR>...загрузка...</center>");
$("#TB_ajaxContent").load(encodeURI("/shop/vp.php?id="+id+"&size="+size+"&r="+Math.random()));
$("#TB_ajaxWindowTitle").html(name);

}

function ShowOrderBlock(id,size)
{
ClearSelection();
document.getElementById("bsize"+size).className="onesizeselected";
$("#orderform").load(encodeURI("/shop/showorderblock.php?id="+id+"&size="+size+"&r="+Math.random())); 
}

function ClearSelection() { 
 //Populate the array with all the page tags 
 var allPageTags=document.getElementsByTagName("*"); 
 //Cycle through the tags using a for loop 
 for (i=0; i<allPageTags.length; i++) { 
 //Pick out the tags with our class name 
 if (allPageTags[i].className=="onesizeselected") { 
 //Manipulate this in whatever way you want 
 allPageTags[i].className="onesize";
 } 
 } 
} 



function AddToCart(id)
{
  $.add2cart(id, 'cart');
  $("#cart").html("<center><img src=\"/i/loading.gif\" style=\"margin:10px;\"><BR>добавляем товар в корзину...</center>");
  $.post(
    '/shop/listcart.php',
    $("#addtocart"+id).formSerialize(),
    function(data){
      $("#cart").html(data);
    }
  );
  return false;
}
            
function VpAddToCart(id)
{
  $("#cart").html("<center><img src=\"/i/loading.gif\" style=\"margin:10px;\"><BR>добавляем товар в корзину...</center>");
  $.post(
    '/shop/listcart.php',
    $("#vpaddtocart"+id).formSerialize(),
    function(data){
      $("#cart").html(data);
    }
  );
  return false;
}


function ItemUpdate(id, action)
{
$("#cart")
  .html("<center><img src=\"/i/loading.gif\" style=\"margin:10px;\"><BR>пересчет корзины...</center>")
  .load(encodeURI("/shop/listcart.php?action="+action+"&id="+id+"&r="+Math.random()));
return false;
}

function ItemMinus(id)
{
ItemUpdate(id, 'itemminus');
return false;
}

function ItemPlus(id)
{
ItemUpdate(id, 'itemplus');
return false;
}

g_stores = null;

function get_stores() {
  if (g_stores === null) {
    $.ajax({
      url:  '/shop/get_stores.php',
      async:false,
      dataType:'json',
      success: function(data) {
        g_stores = [];
        if (data.stores)
          for (var i in data.stores)
            g_stores[ data.stores[i].id ] = data.stores[i];
      }
    });
  }
  return g_stores;
}

function init_params_selectors(context) {
  
  
  
  var context = context || document.body;
  
  $('.tovar_params_selector,.tovar_param_selector', context).each(function() {
    var tbl = $(this),
        avail_status = $('.avail_status', tbl),
        id = tbl.attr('id').replace('params_selector_', '');
    $('input[avail]', this).change(function() {
      $('.cur', tbl).removeClass('cur');
      var avail = $(this).attr('avail');
      var mes = '<font color="red">нет в наличии</font>';
      var btn = $('.basketbtn_' + id);
      if (avail != '-') {
        mes = '<font color="green">в наличии</font>';
        btn.show();
      }
      else {
        btn.hide();
      }
      avail_status.html(mes);
      $(this).parent().parent('td').addClass('cur');
    });
    var first = $('.avail:first INPUT[avail]', this);
    if (!first.size()) {
      first = $('INPUT[avail]:first', this);
    }
    first.click().triggerHandler('change');
    if (first.get(0) && first.get(0).onclick) {
      first.get(0).onclick();
    }
  });
  
  
  
  /*
  $('.tovar_params_selector,.tovar_param_selector', context).each(function() {
    var tbl = $(this),
        avail_status = $('.avail_status', tbl);
    $('input[avail]', this).change(function() {
      $('.cur', tbl).removeClass('cur');
      var avail = $(this).attr('avail');
      var mes = '<font color="red">нет в наличии</font>';
      if (avail != '-') {
       mes = '<font color="green">в наличии</font>';
      }
      avail_status.html(mes);
      $(this).parent().parent('td').addClass('cur');
    }).filter(':first').click().triggerHandler('change');
  });
  */
}

$(document).ready(function() {
  // Сразу после загрузки страницы
  init_params_selectors();
});

// JavaScript Document

function on(id)
{   
	document.getElementById(id).style.display='block';
}
function off(mode)
{   document.getElementById(mode).style.display='none';

}

function AddonWindow( uri ) {
	myWin = open(uri, "linkWindow", "width=800,height=700,status=no,toolbar=no,menubar=no, titlebar=no, scrollbars=yes, resizable=yes")
}



function LinkWindowWide2( uri,w,h) {
	myWin = open(uri, "linkWindow", "width="+w+",height="+h+",status=no,toolbar=no,menubar=no, titlebar=no, scrollbars=yes, resizable=yes")
}
function LinkWindowWide( uri,w,h) {
	myWin = open(uri, "linkWindow", "width="+w+",height="+h+",status=no,toolbar=no,menubar=no, titlebar=no, scrollbars=yes, resizable=yes")
}




function del_str( del, rep , str  )
{
	res = str;

	while( res.search(del) >= 0  )
	{
		res = res.replace( del, rep );
	}

	return res;
}



// Проверка броузера и установка ширины окна открываемого в нем (для фотогалереи)
browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
var agent = navigator.userAgent;

if (agent.indexOf("Opera") > -1) {
	var send_height = 600;
	var send_width = 627;
}
else if ((browserVer >3) && (browserName == "Microsoft Internet Explorer")) {
	var send_height = 600;
	var send_width = 627;
}
else if ((browserVer >3) &&(browserName == "Netscape")) {
	var send_height = 600;
	var send_width = 624;
}



function GetCenterPosition()
{
   var send_left = (screen.availWidth / 2) - (send_width / 2);
   var send_top = (screen.availHeight / 2) - (send_height / 2);

   return "height="+send_height+",width="+send_width+",top="+send_top+",left="+send_left+",toolbar=0,scrollbars=2,resizable=no";
}
function GetCenterPosition_resize()
{
   var send_left = (screen.availWidth / 2) - (send_width / 2);
   var send_top = (screen.availHeight / 2) - (send_height / 2);

   return "height="+send_height+",width="+send_width+",top="+send_top+",left="+send_left+",toolbar=0,scrollbars=2,resizable=yes";
}


// Открытие IPIX в новом окне
function ipix_open(width, height, src, title){

    param = "width="+width+", height="+height;
    var win=window.open('','win',param);   
    win.focus();

	win.document.open();
	win.document.writeln('<html>');
	win.document.writeln('<head>');
	win.document.writeln('<title>' + title + '</title>');
	win.document.writeln('</head>');
	win.document.writeln('<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="White">');
	win.document.writeln('<OBJECT ID="IpixX1" WIDTH="'+width+'" HEIGHT="'+height+'" CLASSID="CLSID:11260943-421B-11D0-8EAC-0000C07D88CF" CODEBASE="http://www.ipix.com/viewers/ipixx.cab#version=6,0,0,2"><PARAM NAME="IPXFILENAME" VALUE="'+src+'"><EMBED SRC="'+src+'" WIDTH="'+width+'" HEIGHT="'+height+'"  BORDER=0 PALETTE="FOREGROUND" TYPE="application/x-ipix" PLUGINSPAGE="http://www.ipix.com/cgi-bin/download.cgi"></EMBED></OBJECT>');
	win.document.writeln('</body>');
	win.document.writeln('</html>');		
	win.document.close();
}

function str_replace ( search, replace, subject ) {    // Replace all occurrences of the search string with the replacement string
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
 
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }
 
    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
 
    for(var k=0; k<search.length; k++){
        var i = subject.indexOf(search[k]);
        while(i>-1){
            subject = subject.replace(search[k], replace[k]);
            i = subject.indexOf(search[k],i);
        }
    }
 
    return subject;
 
}

(function($) {

	$.extend({
		add2cart: function(source_id, target_id, callback) {
    
      var source = $('#' + 'good_photo_' + source_id );
      if (!source.size()) {
        source = $('#' + 'good_photo_lite_' + source_id );
      }
      var target = $('#' + target_id );
      
      var shadow = $('#' + source_id + '_shadow');
      if( !shadow.attr('id') ) {
          source.before('<div id="'+source.attr('id')+'_shadow" style="display: none; background-color: lightgray; border: solid 1px darkgray; position: static; top: 0px; z-index: -100000;">&nbsp;</div>');
          var shadow = $('#'+source.attr('id')+'_shadow');
      }
      
      if( !shadow ) {
          alert('Cannot create the shadow div');
      }
      
      shadow.width(source.css('width')).height(source.css('height')).css('top', source.offset().top).css('left', source.offset().left).css('opacity', 0.5).show();
      shadow.css('position', 'absolute');
      
      shadow.animate( { width: target.innerWidth(), height: target.innerHeight(), top: target.offset().top, left: target.offset().left }, { duration: 300 } )
        .animate( { opacity: 0 }, { duration: 100, complete: callback } );
        
		}
	});
})(jQuery);




