$(function() {

	/* Only IE6 needs hover support */
	if ($.browser.msie && $.browser.version < 7) {
		$("a[class^=btn-] img").hover(function() {
			$(this).attr("src", $(this).attr("src").replace(".gif", "_hover.gif"));
		}, function() {
			$(this).attr("src", $(this).attr("src").replace("_hover.gif", ".gif"));
		});
		
		$("input[class^=submit-]").hover(function() {
			$(this).attr("src", $(this).attr("src").replace(".gif", "_hover.gif"));
		}, function() {
			$(this).attr("src", $(this).attr("src").replace("_hover.gif", ".gif"));
		});
		
		var btns = $('div.green_button, div.green_bigbuttonnoarrow, div.green_buttonnoarrow, div.white_button_small');
		var selector = btns.selector;
		btns.livequery(function() {
			$(this).hover(function() {
				if (!$(this).data('hoverClass')) {
					var parts = selector.split(',');
					for (var i = 0, n = parts.length; i < n; i++) {
						var cls = parts[i].replace(/ ?[a-z]+\./ig, '');
						if ($(this).hasClass(cls)) {
							$(this).data('hoverClass', cls + '_hover');
							break;
						}
					}
				}
				$(this).addClass($(this).data('hoverClass'));
			}, function() {
				if($(this).data('hoverClass') != '') {
					$(this).removeClass($(this).data('hoverClass'));
				}
			});
		});
		
		/* Summarized above into one livequery function
		$("div.green_button").livequery(function() {
			$(this).hover(function() {
				$(this).addClass("green_button_hover");
			}, function() {
				$(this).removeClass("green_button_hover");
			});
		});
		
		$("div.green_bigbuttonnoarrow").livequery(function() {
			$(this).hover(function() {
				$(this).addClass("green_bigbuttonnoarrow_hover");
			}, function() {
				$(this).removeClass("green_bigbuttonnoarrow_hover");
			});
		});
		
		$("div.green_buttonnoarrow").livequery(function() {
			$(this).hover(function() {
				$(this).addClass("green_buttonnoarrow_hover");
			}, function() {
				$(this).removeClass("green_buttonnoarrow_hover");
			});
		});
		
		$("div.white_button_small").livequery(function() {
			$(this).hover(function() {
				$(this).addClass("white_button_small_hover");
			}, function() {
				$(this).removeClass("white_button_small_hover");
			});
		});
		*/
	}
	
	// Replace submit buttons with markup buttons.
	$("input[class^=btnbig-]").livequery(function() {
		$(this).divButton(['green_button', 'submit_button']);
	});
	$("input[class^=btnbignoarrow-]").livequery(function() {
		$(this).divButton(['green_bigbuttonnoarrow', 'submit_button']);
	});
	$("input[class^=btnnoarrow-]").livequery(function() {
		$(this).divButton(['green_buttonnoarrow', 'submit_button']);
	});
	$("input.white-button-small").livequery(function() {
		$(this).divButton(['white_button_small submit_button']);
	});
	
	$("input[class^=btnbuyreplace-]").livequery(function() {
	    if ($(this).hasClass("show_alert")) { // see if it should use thickbox to show a warning
            var meta = $(this).metadata();
            // this should be rewritten to forward all meta-params, not just fm
            if (meta.param.fm) {
                var metaFm = meta.param.fm;
            } else {
                var metaFm = '';
            }
    		$(this).after('<a class="btnbuyreplace show_alert {param:{fm:\'' + metaFm + '\'}}" href="' + $(this).attr("value") + '"></a>').remove();
	    } else {
    		$(this).after('<a class="btnbuyreplace" href="' + $(this).attr("value") + '"></a>').remove();
	    } 
		/* Replaced with line above
		$(this).each(function() {
			var button_href = $(this).attr("value");
			var html_button = '<a class="btnbuyreplace" href="' + button_href + '"></a>';
			$(this).after(html_button);
			$(this).remove();
		});
		*/
	});
	$("input[class^=btnsmall-]").livequery(function() {
		$(this).after('<div class="white_button_small navigation_button"><a href="' + $(this).attr("rel") + '">' + $(this).attr("value") + '</a></div>').hide();
		/* Replaced with line above
		$(this).each(function() {
			var button_label = $(this).attr("value");
			var html_button = '<div class="white_button_small navigation_button"><a href="' + $(this).attr("rel") + '">' + button_label + '</a></div>';
			$(this).after(html_button);
			$(this).hide();
		});
		*/
	});
	
	$("a[class^=noscript-]").livequery(function() {
		$(this).each(function() {
			var oThis = $(this);
			var button_label = $(this).text();
			var button_classes = $(this).attr("class");
			var html_button = $('<div class="white_button_small ' + button_classes + '"><a href="' + $(this).attr("href") + '">' + button_label + '</a></div>');
			html_button.click(function() {
				oThis.trigger('click');
				if($('#clickedButton').length > 0)
				{
          $('#clickedButton').val('1');			
        }
				return false;
			});
			$(this).after(html_button);
			$(this).hide();
		});
	});
	
	/* Useless?
	$("input:submit").click(function(e) {
		//$(this).parents("input:text").blur();
		var $input = $(this).parents("input:text");
		if ($input.length > 0) 
			$input.blur();
		// since there will alway only be one form for each page, cancel the fancy selections.
		//$('form').submit();
		//return false;
	});
	*/
	
	/*
	 * New ajaxButton methods and callbacks.
	 */
	// new ajaxButton plugin call
	$("a.ajax-button:not(.show_alert)").livequery(function() {
		$(this).ajaxButton();
	});
	//$("a.ajax-button").ajaxButton();
	
	// register ajaxButton callbacks
	/*
	 $.ajaxButton.registerCallback('animateslip', function(e) {
	 //console.log('animateslip');
	 });
	 */
	$('[class*=btngenerikabuy]:not(.show_alert)').livequery('click', function() {
		$(this).addClass('buyactive');
	});
	$('a.ajax-button.buy').livequery('click', function() {
		if ($(this).parents('div.generika').size() < 1) {
			return false;
		}
		else {
			$(this).addClass('buyactive').find(':submit:eq(0)');
		}
	});
});
