$(document).ready( function()
	{
		//highlight_messages();
		initDropDownMenus();
		initProdImageSwap();
		initProdImageZoom();
		initEqualizeCols();
	}
);


function initEqualizeCols()
{
	$("div.stretch_height, div#shopsidebar").equalizeCols();
}

function highlight_messages()
{
	if($('good_messages')) {
		new Effect.Highlight('good_messages', {duration: 1, startcolor:'#ffff99', endcolor:'#ffffff'});
	}
	if($('error_messages')) {
		new Effect.Highlight('error_messages', {duration: 1, startcolor:'#ffff99', endcolor:'#ffffff'});
	}
}


function initDropDownMenus()
{
	$("ul#topnav li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function(){ $("ul", this).fadeOut("fast"); }
    );
}


function copyAddress()
{
	$("#scn").val($("#bcn").val());
	$("#sfn").val($("#bfn").val());
	$("#sln").val($("#bln").val());
	$("#sa1").val($("#ba1").val());
	$("#sa2").val($("#ba2").val());
	$("#sct").val($("#bct").val());
	$("#sst").val($("#bst").val());
	$("#spv").val($("#bpv").val());
	$("#spc").val($("#bpc").val());
	$("#scn").val($("#bcn").val());
}


function changeShipping(id)
{
	if(id)
	{
		window.location = "/checkout/change_shipping/" + id;
	}
}


function initProdImageSwap()
{
	$("#images .detailswap").click(
		function()
		{
			newsrc = this.title;
			zoomsrc = newsrc.replace('detail', 'zoom');
			$("#images img#detail").attr('src', newsrc);
			$("#images img.zoom2").attr('src', zoomsrc);
			return false;
		}
	);
}


function initProdImageZoom()
{
	$("#images img#detail").click(
		function()
		{
			return false;
		}
	);
}


// Shows the big version of the gallery image clicked on
function showbig(id)
{
	$("#thumbs a").removeClass('selected');
	$("a#a_" + id).addClass('selected');
	
	$("#big_image").attr('src', 'images/gallery/image_' + id + '_big.jpg');

	object_id = "#thumb_" + id;
	
	if($(object_id).attr('alt') != undefined)
	{
		$("#caption").html($(object_id).attr('alt'));
	}
	else
	{
		$("#caption").html('');
	}
	
	prev_id = $(object_id).parents('a').prev('a').children('img.thumb').attr('id')
	$("#back").unbind();
	if(prev_id)
	{
		prev_id = prev_id.replace('thumb_', '');
		$("#back").click(
			function(){ showbig(prev_id); return false; }
		)
	}
	else
	{
		$("#prev").click(
			function(){ return false; }
		)
	}
	
	next_id = $(object_id).parents('a').next('a').children('img.thumb').attr('id');
	$("#next").unbind();
	if(next_id)
	{
		next_id = next_id.replace('thumb_', '');
		$("#next").click(
			function(){ showbig(next_id); return false; }
		)
	}
	else
	{
		$("#next").click(
			function(){ return false; }
		)
	}
	
}


// standart string replace functionality
function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}



$(function(){
  $('#detailzoom img').zoom1().click(function(){
    $(this).zoom2().fadeIn().click(function(){
      $(this).hide(); return false; })
    .end().parent().addClass('hover'); return false; });
});



(function($) {  
   $.fn.equalizeCols = function(children){  
     var child = Array(0);  
     if (children) child = children.split(",");  
     var maxH = 0;  
     this.each(  
       function(i)   
       {  
         if (this.offsetHeight>maxH) maxH = this.offsetHeight;  
       }  
     ).css("height", "auto").each(  
       function(i)  
       {  
         var gap = maxH-this.offsetHeight;  
         if (gap > 0)  
         {  
           t = document.createElement('div');  
           $(t).attr("class","fill").css("height",gap+"px");  
           if (child.length > i)  
           {  
             $(this).find(child[i]).children(':last-child').after(t);  
           }   
           else   
           {  
             $(this).children(':last-child').after(t);  
           }  
         }  
       }    
     );  
       
   }  
})(jQuery);


// zoomi - A zoom for images ~ Sean Catchpole - Version 0.9
(function($){

$.fn.zoomi = function() {
  $(this).filter("img").each(function(){
    if(!this.z) {
      $(this).zoom1().mouseover(function(){$(this).zoom2().show();});
      $(this.z).mouseout(function(){$(this).hide();}); }
  });
 return this;
}

$.fn.zoom1 = function() {
  $(this).each(function(){
    var e = this;
    $(e).css({'position':'relative','z-index':'8'}).after('<img class="'+e.className+'">');
    e.z = e.nextSibling;
    $(e.z).removeClass("zoomi").addClass("zoom2").attr("src",e.alt || e.src)
    .css({'position':'absolute','z-index':'10'});
    $(e.z).hide();
  });
  return this;
}

$.fn.zoom2 = function() {
  var s = [];
  this.each(function(){
    var e = this;
    if(!e.z) e = $(e).zoom1()[0]; s.push(e.z);
    if(!e.z.complete) return;
    if(!e.z.width) { $(e.z).show(); e.z.width=e.z.width; $(e.z).hide(); }
    $(e.z).css({left:$(e).offsetLeft()-(e.z.width-e.scrollWidth)/2+'px',
    top:$(e).offsetTop()-(e.z.height-e.scrollHeight)/2+'px'});
  });
  return this.pushStack(s);
}

$.fn.offsetLeft = function() {
  var e = this[0];
  if(!e.offsetParent) return e.offsetLeft;
  return e.offsetLeft + $(e.offsetParent).offsetLeft(); }

$.fn.offsetTop = function() {
  var e = this[0];
  if(!e.offsetParent) return e.offsetTop;
  return e.offsetTop + $(e.offsetParent).offsetTop(); }

$(function(){ $('img.zoomi').zoomi(); });

})(jQuery);


