   var isMSIE = /*@cc_on!@*/false;
   var ie6 = /MSIE 6/i
   var isMSIE6=ie6.test(navigator.userAgent);
   shuffle = function(o){
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
   };

   var gal_image_data = new Array();
   var gal_image_data_count = 0;
   var gal_display_image_data = new Array();
   var gal_display_image_img = new Array();
   var gal_imgWait = new Array()
   var galobj=false;
   var galcells=false;
   var gal_totalLoaded=0;
   var gal_timer;
   var http_request = false;
   var newimage = new Image(85,85);

   var splash_itemid=0;
   var splash_timer;


   function gal_debug(text) {
 	 d = document.getElementById('debug');
 	 d.innerHTML=d.innerHTML+"<br/>"+text;
   }

   function gal_makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) {
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) {
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = gal_loadContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }

   function gal_loadContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {

            var xmldoc = http_request.responseXML;

            var root = xmldoc.getElementsByTagName('data').item(0);

            for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
               var node = root.childNodes.item(iNode);
               if(node.childNodes.length==0) continue;
               gal_image_data[gal_image_data_count] = new Array();
               gal_image_data[gal_image_data_count][0]=gal_image_data_count;
               gal_image_data[gal_image_data_count][1]=node.childNodes.item(0).childNodes.item(0).data;
               gal_image_data[gal_image_data_count][2]=node.childNodes.item(1).childNodes.item(0).data;
               gal_image_data[gal_image_data_count][3]=node.childNodes.item(2).childNodes.item(0).data;
               gal_image_data[gal_image_data_count][4]=node.childNodes.item(3).childNodes.item(0).data;
               gal_image_data_count++;
            }

			galobj = document.getElementById('gal');
	 	    galcells = galobj.getElementsByTagName('TD');

	 		if(gal_image_data_count<galcells.length) {
			 	alert('Not enought images. Got '+gal_image_data_count+' of '+galcells.length+'.')
			 	return false;
		    }

		    for(var i=0;i<=galcells.length;i++) {
		       gal_display_image_img[i] = new Image(85,85);
		    }

            gal_fill_images();
         } else {
         }
      }
   }

   function gal_fill_images() {
     var gal_tmp_array = new Array();

     for(var i=0;i<gal_image_data_count;i++) {
       gal_tmp_array[i]=i;
     }

	 gal_totalLoaded=0;
     while(gal_display_image_data.length<galcells.length) {
       var g=0;
       gal_tmp_array=shuffle(gal_tmp_array);
       while(gal_inArray(gal_display_image_data,gal_tmp_array[g]))g++;
       gal_display_image_data[gal_display_image_data.length]=gal_image_data[gal_tmp_array[g]];
     }

     for(var i=0;i<galcells.length;i++) {
   	    gal_display_image_img[i].src=gal_display_image_data[i][1];  //gal_display_image_data[i] is undefined
     	gal_loadImage(i);
     }
   }

   function gal_fill_image(slot) {

     var gal_tmp_array = new Array();

     for(var i=0;i<=gal_image_data_count;i++) {
       gal_tmp_array[i]=i;
     }

     var g=0;
     gal_tmp_array=shuffle(gal_tmp_array);
     while(gal_inArray(gal_display_image_data,gal_tmp_array[g]))g++;
     gal_display_image_data[slot]=gal_image_data[gal_tmp_array[g]];
	 gal_display_image_img[slot].src=gal_display_image_data[slot][1];
     gal_loadImage(slot);
   }


   function gal_inArray(a, v) {
   	 for(var x=0;x<a.length;x++) {   	 	if(a[x]==undefined) continue;
		if(a[x][0]==v) return true;
	 }
   	 return false;
   }

   function gal_loadImage(slot) {
     if(!gal_display_image_img[slot].complete){
		gal_imgWait[slot]=setTimeout('gal_loadImage('+slot+')', 250);
	 }
	 else{
	   gal_totalLoaded++;
	   gal_spreadImage(slot);
	   if(gal_totalLoaded==galcells.length) {
	   	 setTimeout('gal_fadeImage()', 5000);
	   }
	 }
   }

   function gal_reloadImage(slot) {
     if(!newimage.complete){
		gal_imgWait[slot]=setTimeout('gal_reloadImage('+slot+')', 250);
	 }
	 else{
	   gal_processFadeImage(slot,80);
	 }
   }

   function gal_processSpreadImage(slot,v) {
      gal_display_image_img[slot].width=v;
      gal_display_image_img[slot].height=v;
      if(v<85) {
    	gal_imgWait[slot]=setTimeout('gal_processSpreadImage('+slot+','+(v+20)+')', 40);
      }
   }

   function gal_processFadeImage(slot,v) {
      gal_display_image_img[slot].width=v;
      gal_display_image_img[slot].height=v;
      if(v>0) {
    	gal_imgWait[slot]=setTimeout('gal_processFadeImage('+slot+','+(v-20)+')', 40);
      }
      else {
        galcells[slot].removeChild(galcells[slot].firstChild);
        gal_display_image_img[slot].src=gal_display_image_data[slot][1];
     	setTimeout('gal_fadeImage()', 5000);
        gal_spreadImage(slot);
      }
   }

   function gal_spreadImage(slot) {
      gal_display_image_img[slot].width=0;
      gal_display_image_img[slot].height=0;
      var ahref = document.createElement("a");
      ahref.href=gal_display_image_data[slot][4];
      ahref.setAttribute("title",gal_display_image_data[slot][3]);
      galcells[slot].appendChild(ahref);
      gal_display_image_img[slot].setAttribute("alt",gal_display_image_data[slot][3]);

      if(isMSIE) {        gal_display_image_img[slot].onmouseover = function() {
			gal_ie_toggleopacity(gal_display_image_img[slot],false);
		}        gal_display_image_img[slot].onmouseout = function() {
			gal_ie_toggleopacity(gal_display_image_img[slot],true);
		}
      }
   	  ahref.appendChild(gal_display_image_img[slot]);
   	  gal_processSpreadImage(slot,5);
   }

   function gal_fadeImage() {
      var slot = Math.ceil(6*Math.random())-1;
      var gal_tmp_array = new Array();
      for(var i=0;i<gal_image_data_count;i++) {
        gal_tmp_array[i]=i;
      }
      var g=0;
      gal_tmp_array=shuffle(gal_tmp_array);
      while(gal_inArray(gal_display_image_data,gal_tmp_array[g]))g++;
      newimage.src=gal_display_image_data[slot][1];
      gal_display_image_data[slot]=gal_image_data[gal_tmp_array[g]];
      gal_reloadImage(slot,80);
   }

   function gal_ie_toggleopacity(elm, val) {      var f='DXImageTransform.Microsoft.alpha';
      if (elm.filters[f]) {
		elm.filters[f].enabled = val;
  	  } else {
		elm.style.filter = 'progid:' + f + '(opacity=20,enabled=true)';
	  }
   }

	function change_splash_item() {		$j.getJSON('splash.php',{'oid':splash_itemid},function(splash_data) {
			splash_itemid=splash_data.oid;
			$j.preload(['http://shop.tchernovaudio.com/goods/images/'+splash_data.image], {loaded_all: function(loaded, total) {            	$j('#splash').fadeOut('slow',function() {            		$j('#splash img').eq(0).attr({'src':'http://shop.tchernovaudio.com/goods/images/'+splash_data.image,'alt':splash_data.title});
            		$j('#splash a').eq(0).attr({'href':splash_data.link});
            		lm=Math.random()*535;
            		if(isMSIE6) lm=Math.ceil(lm/2);            		$j('#splash').css({'margin-top':Math.random()*200+'px','margin-left':lm+'px'});
            		$j('#splash').fadeIn('slow');
            	});		   		splash_timer=setTimeout('change_splash_item()',15000);
			}});

		});
	}

function authsubmit() {	$j.post('/cartadd.php',$j("#authform").serialize(),function(data){		$j("#cartoverlay").html(data);
	});
	return false;
}

(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;

			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}

		}
	});
})(jQuery);

var $j = jQuery.noConflict();

$j(document).ready(function() {	$j('a[rel|=external]').each( function( intIndex ){ $j( this ).attr("target","_blank"); });});
