/**
 * jQuery Image Fader Plugin
 * Copyright (c) 2009 Brent O'Connor
 * http://www.epicserve.com/
 *
 * Dual licensed under the MIT and GPL licenses.
 */
(function($) {

	$.fn.image_fader = function(options) {
		
		if ($.browser.msie && $.browser.version < 7) return;
		
		var image_fader = this;
		
		var o = $.extend({}, $.fn.image_fader.defaults, options);

		function over_action(elem) {
			elem = $(elem);
			span = elem.prepend('<span class="'+o.span_class_name+'" />');
			span.css('cursor', 'pointer');
			
			// This is a workaround because I couldn't get fadding to work for span tags that had
			// a PNG with an alpha transparency background
			if ($.browser.msie && $.browser.version >= 7 && o.has_transparent_png_image === true) {
				span.show();
			} else {
				span = elem.find('span').css('opacity', 0).css('cursor', 'pointer');
				span.stop().fadeTo(o.fade_in_speed, o.fade_in_opacity);
			}
		};
		
		function out_action(elem) {
			elem = $(elem);
			span = elem.find('span');
			span.stop().fadeTo(o.fade_out_speed, o.fade_out_opacity);
			span.remove("*");
		};	
			
		return this.each(function() {
			elem = $(this);
			elem.hover(function() { over_action(this); }, function() { out_action(this); } );
		});
	};
	
	$.fn.image_fader.defaults = {
		span_class_name: 'image-fader',
		fade_in_speed: 200,
		fade_out_speed: "slow",
		fade_in_opacity: 1,
		fade_out_opacity: 0,
		has_transparent_png_image: true
	};
	
})(jQuery);


/**
 * jQuery External Links Plugin
 * Copyright (c) 2009 Brent O'Connor
 * http://www.epicserve.com/
 *
 * Dual licensed under the MIT and GPL licenses.
 */
(function($) {	

	$.fn.external_links = function(options) {
		
		var o = $.extend({
			add_rel: true,
			new_window: true,
			add_class: true,
			add_title: true,
			hostname: location.hostname,
			external_title: 'This link opens in a new window',
			class_name: 'external',
			local_domain_arr: [],
			new_window_callback: open_new_window
		}, options);
		
		function open_new_window () {
			window.open(this.href);
			return false;
		}
		
		function init(elems) {
			
			re = /^(https?:\/\/)([^\/]+).*\/?$/i;
			$(o.local_domain_arr).each(function(k, v) {
				o.local_domain_arr[k] = v.replace(re, "$2");
			});
			
			return elems.each(function() {
			
				var elem          = $(this);
				var external_link = false;
				var valid_link    = false;
	
				if (typeof elem.not("[href^=javascript]")[0] !== 'undefined') valid_link = true;
				
				if (valid_link === true) {
					
					if (elem.attr('rel') === "external") external_link = true;			
					
					// check if the link is external
					if (external_link === false && elem[0].hostname !== o.hostname) {
						if (o.local_domain_arr.length > 0) {
							if ($.inArray(elem[0].hostname, o.local_domain_arr) === -1) {
								external_link = true;
							}
						} else {
							external_link = true;
						}
					}
					
					if (external_link === true) {
						if (o.add_rel === true)    elem.attr('rel', 'external');
						if (o.add_class === true)  elem.addClass(o.class_name);
						if (o.add_title === true)  elem.attr('title', o.external_title);
						if (o.new_window === true) elem.click(o.new_window_callback);
					}
				
				}
			
			});
		}
		
		return init(this);			
	
	};
	
	
})(jQuery);


/**
 * Main menu effects
 */
$("nav.main > ul > li").hover(
	function() {

		$(this).toggleClass("hover");
		$(this).find("ul:first").slideDown(500);
		
	},
	function() {
		
		var li        = $(this);
		var ul_length = $(this).find("ul:first").length;
		
		// test if the menu has any sub menu items
		if (ul_length === 0) {
			li.toggleClass("hover");
		} else {
			$(this).find("ul:first").slideUp(250, function() {
				li.toggleClass("hover");
			});			
		}
		
	}
);

// initialize image fader
$(function() {
	$('#banners li.promo a').image_fader({ fade_in_opacity: 0.15, has_transparent_png_image: false  });
	$('#banners li.dvd-order a').image_fader({ fade_in_speed: 400 });
	$('#followus li a').image_fader({ fade_in_speed: 400 });
	$('footer .logo a').image_fader({ fade_in_speed: 400 });
});

// add hover ability to tables
$('tr').hover(
	function() {
		$(this).addClass('hover');
	},
	function() {
		$(this).removeClass('hover');
	}
);

if ($('#banners li.promo').length !== 0) {
    $('#banners li.promo').cycle({
        'timeout': 6000
    });
}

// initialize form descriptions
$('input[type=text]').descriptions();