AMN = {};

AMN.rollover =
{
   init: function()
   {
      this.preload();
     
      $(".ro").hover(
         function () { $(this).attr( 'src', AMN.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', AMN.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', AMN.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_o' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/_o\./, '.');
   }
};

var submenu, submenuDflt;
$(document).ready( function() {
$(".submenu").hover(function() { $(document).stopTime(); }, function() { restoreDefaultSubmenu(); } );
  $("#menu1").hover(function() { changeSubmenu(1); }, function() { restoreDefaultSubmenu(); });
  $("#menu2").hover(function() { changeSubmenu(2); }, function() { restoreDefaultSubmenu(); });
  $("#menu3").hover(function() { changeSubmenu(3); }, function() { restoreDefaultSubmenu(); });
  $("#menu4").hover(function() { changeSubmenu(4); }, function() { restoreDefaultSubmenu(); });
  $("#menu5").hover(function() { changeSubmenu(5); }, function() { restoreDefaultSubmenu(); });
  $("#menu6").hover(function() { changeSubmenu(6); }, function() { restoreDefaultSubmenu(); });
  $("#menu7").hover(function() { changeSubmenu(7); }, function() { restoreDefaultSubmenu(); });
});

function nothing() {}

function changeSubmenu(id) {
  $(document).stopTime();
  if (submenu==id)
    return;
  if(submenu!=undefined)
    $("#submenu"+submenu).hide(444);
  $("#submenu"+id).show(444);
  submenu= id;
}

function restoreDefaultSubmenu() {
  $(document).oneTime(600, function(){ changeSubmenu(submenuDflt); });
    
}

function setDefaultSubmenu(id) {
  // This is done delayed for Chrome
  setTimeout(	
    function() { 
      changeSubmenu(id); 
      submenuDflt= id;}, 
  100);  
  
}


function SKU(sizes, colors, addCartURL, pack) {
    var arr=new Array(sizes.length);
    for (i=0; i <arr.length; i++)
        arr[i]=new Array(colors.length);

    var usedColors= new Array(colors.length);
    var usedSizes= new Array(sizes.length);
    var disableColors= true;
    var disableSizes= true;

    var defaultColor= -1;
    var selColor= -1;
    var selSize= -1;
    var selImage= 0;

    this.add= function(size, color) {
        arr[size==-1 ? 0 : size-1][color==-1 ? 0 : color-1]= true;
        usedColors[color-1]= color;
		if (defaultColor==-1) defaultColor= color;
        	usedSizes[size-1]= size;
    }

    this.init= function() {
        var i=0, j=0, ncol=0, nsiz=0, instance=this;

		sizes.each(function() {
            if (usedSizes[i++]!=undefined) {
			nsiz++;
			selSize= i-1;
            $(this).fadeIn(500);
            $(this).click(function() { instance.selectSize($(this)); });
        }
    });
	
	colors.each(function() {
        if (usedColors[j++]!=undefined) {
			ncol++;
			selColor= j-1;
            $(this).fadeIn(500);
            $(this).click(function() { instance.selectColor($(this)); });
        }
	    
    });

	disableSizes= nsiz==0;
    disableColors= ncol==0;
    if (disableSizes) 
	    $("#sizes").hide();
	else {
	    if (nsiz>1) selSize= -1;
	    else this.selectSize(sizes[selSize]);
	}

	if (disableColors) 
	    $("#colors").hide();
	else {
	    if (ncol>1) selColor= -1;
	    else this.selectColor(colors[selColor]);
	}


	$("#add2cart").click(function() {
	    if (!readyToCart())
			return;
        sku= (disableSizes ? '' : selSize+1)+ '-'+ (disableColors ? '' : selColor+1);
		$.getJSON(addCartURL, { quantity:1, sku: sku, time:new Date().getTime(), pack:pack },
        function(data) { 
			if (data.returnURL!=undefined) {
				window.location.href= data.returnURL;
				return;
			}
		   	updateCartSummary(data);
    		$("#top #cartad span").html(data.title);
    		$("#top #cartad img").attr("src", data.media);	
		 	$("#top #cartad").fadeIn(1000);
		   	$("#top #cartad").oneTime(5000, function(){ $(this).fadeOut(1000); } );
		}  );
        });
		updateCartButton();
    }

    function readyToCart() { return (disableSizes || selSize>=0) && (disableColors || selColor>=0); }

    function setSelected(obj, i, sel) {
        if (i == sel)
            obj.addClass("sel");
        else
	    	obj.removeClass("sel");
    }

    function setDisabled(obj, val) {
        if (val==undefined)
            obj.fadeTo(500, 0.33);
        else
	    	obj.fadeTo(500, 1.0);
    }

    function updateCartButton() {
		if (readyToCart()) {
		    $("#add2cart").fadeTo(500, 1.0);
		    $("#add2cart").attr("blocktip", "true");
	    } else {	    
	        $("#add2cart").fadeTo(500, 0.4);
		    $("#add2cart").attr("blocktip", "false");
		}
    }

    this.selectColor= function(col) {
        selColor= colors.index(col);
        var i=0;

		if (selColor!=-1 && selSize!=-1 && arr[selSize][selColor]==undefined)
	    	this.selectSize(null);

		colors.each(function() { setSelected($(this), i++, selColor); } );
		i=0;
    	sizes.each(function() {  setDisabled($(this), selColor==-1 ? 42 : arr[i++][selColor]); } );
		updateCartButton();        
		this.setImage(selImage);
    }

    this.setImage= function(idx) {
        selImage= idx;
		this.updateImageButtons(idx);
		$("#magnify").attr('href', '../media/'+images[this.getColor()][idx][0]);
		$("#magnify img").attr('src', '../media/'+images[this.getColor()][idx][1]);
    }

    this.updateImageButtons= function(idx) {
		for (i=0; i< images[this.getColor()].length; i++) {
		    if (idx==i)
		        $("#is"+i).addClass('selected');
		    else
		        $("#is"+i).removeClass('selected');
		} 
    }

    this.getColor= function() { 
		return selColor==-1 ? (defaultColor==-1 ? 0 : defaultColor) : selColor+1; 
    }

    this.selectSize= function(col) {
        selSize= sizes.index(col);
        var i=0;

	if (selColor!=-1 && selSize!=-1 && arr[selSize][selColor]==undefined)
	    this.selectColor(null);

		sizes.each(function() { setSelected($(this), i++, selSize); } );
		i=0;
        colors.each(function() { setDisabled($(this), selSize==-1 ? 42 : arr[selSize][i++]);}); 

		updateCartButton();        
    }
   
}


function Cart(addCartURL, addPackURL) {
    var instance= this;	

    $(".minus").click(function() {
		if ($(this).parent().hasClass("download"))
			return;
		pack= $(this).parent().parent().attr("pack");			
        var id= $(this).attr("id").substring(3);
        instance.changeCartQuantity(id, -1, pack);
    }); 
    
    $(".plus").click(function() {
		if ($(this).parent().hasClass("download"))
			return;	
		pack= $(this).parent().parent().attr("pack");			
        var id= $(this).attr("id").substring(3);
        instance.changeCartQuantity(id, 1, pack);
    });
    
    $(".elim").click(function() {
		pack= $(this).parent().parent().attr("pack");
        var id= $(this).attr("id").substring(3);
        instance.elim(id, pack);
    }); 

    this.updateCartButtons= function() {    
	    $(".cartctrl").each(function() { 
        	id= $(this).attr("id").substring(1);
	    	q= $(this).parent().hasClass("download") ? 0 : $(this).html()*1;
	    	max= $(this).parent().hasClass("download") ? 0 : $(this).attr("max")*1;	
	    	$("#min"+id).fadeTo(500, q<=1 ? 0.4 : 1.0);
	    	$("#mas"+id).fadeTo(500, q>=max ? 0.4 : 1.0);
        });
    }

    this.elim= function(id, pack) {
        quant= $("#cart-body #q"+id).html()*1;
        setCartQuantity(id, -quant, pack);
		$("#tr"+id+", .pack"+pack).fadeOut(500);
    }

    this.changeCartQuantity= function(id, q, pack) {
		quant= $("#cart-body #q"+id).html()*1;
		max= $("#cart-body #q"+id).attr("max")*1;	

		if (quant+q<1 || quant+q>max)
	    return;
        setCartQuantity(id, q, pack);
    }

    function setCartQuantity(id, q, pack) {
        $("#cart-body #q"+id).html( $("#cart-body #q"+id).html()*1+ q);
        idx= id.indexOf("-");
        var mconId= id.substring(0, idx);
        var sku= id.substring(idx+1); 

        $.getJSON(pack=="null" ? addCartURL : addPackURL, 
			{ mcon_id:mconId, sku:sku, quantity:q, time:new Date().getTime(), pack:pack=="null" ? "" : pack },
            function(data) { 
	       		updateCartSummary(data);
	        	instance.updateCartButtons();
                $("#cart-body #tot"+id).html(data.price);
	        	$("#cart-body #subtotal").html(data.carttotal);	
		}  );
    }
	
    this.updateCartButtons();	
}

function updateCartSummary(data) {
    $("#top #cartsum #items").html(data.cartitems);
    $("#top #cartsum #total").html(data.carttotal);
}


/**
*
*	simpleTooltip jQuery plugin, by Marius ILIE
*	visit http://dev.mariusilie.net for details
*       Added "block" funcionality by Zentense
*
**/
(function($){ $.fn.simpletooltip = function(){
	return this.each(function() {
		var text = $(this).attr("title");
		$(this).attr("title", "");
		if(text != undefined) {
			$(this).hover(function(e){
				$(this).attr("title", "");
				if ($(this).attr("blocktip")=="true")
				    return;
				var tipX = e.pageX + 12;
				var tipY = e.pageY + 12; 
				$("body").append("<div id='simpleTooltip' style='position: absolute; z-index: 100; display: none;'>" + text + "</div>");
				if($.browser.msie) var tipWidth = $("#simpleTooltip").outerWidth(true)
				else var tipWidth = $("#simpleTooltip").width()
				$("#simpleTooltip").width(tipWidth);
				$("#simpleTooltip").css("left", tipX).css("top", tipY).fadeTo(500, 0.87);
			}, function(){
				$("#simpleTooltip").remove();
				$(this).attr("title", text);
			});
			$(this).mousemove(function(e){
				var tipX = e.pageX + 12;
				var tipY = e.pageY + 12;
				var tipWidth = $("#simpleTooltip").outerWidth(true);
				var tipHeight = $("#simpleTooltip").outerHeight(true);
				if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
				if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
				$("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
			});
		}
	});
}})(jQuery);

var radioScheduleDay;
function showRadioSchedule(day) {
    if (day<1) day=7;
    if (day>7) day=1;
    radioScheduleDay= day;
    $.getJSON("AJAXradioSched", {day:day}, function(data) {
        $("#radio_schedule #day").html(data.mcon_title);
        $("#radio_schedule #content").html(data.mcon_description);
        $("#radio_schedule").slideDown(500);
    });
}
