/**
 * Document Init
 */
(function($) {
    $.fn.PacInitialize = function(o) {
        var config = {};
        var options = $.extend(config, o);
        return this.each(function() {
        	$.fn.PacInitialize.externalLinks();
			$.fn.media.defaults.flvPlayer = '/js/mediaplayer.swf';
			$.fn.media.defaults.mp3Player = '/js/mediaplayer.swf';
			$.fn.media.defaults.bgColor = '#000'
			$('.media').media({
				width: 310,
				height: 233, 
				autoplay: false,
				attrs: { scale: 'aspect' },
				flashvars: { 
					controlbar:"none",
					image:"http://pacslo.org/images/uploads/marquee_support_960x260.jpg"
				}
			});
        });
    };
    $.fn.PacInitialize.externalLinks = function() {
        $("a").each(function(i) {
            var $this = $(this);
            if ($this.attr("href") && $this.attr("rel") == "external") {
                $this.attr("target", "_blank");
            }
        });
    };
})(jQuery);
$(document).ready(function() {
	$(document).PacInitialize();
});
/**
 * Cycle
 */
(function($) {
	$.fn.PacCycle = function(o) {
		var config = {
			fx: "scrollLeft",
			timeout: 0
		};
		var options = $.extend(config, o);
		return this.each(function() {
			var $this = $(this);
			$this.cycle(options);
			$(".next", $this.parent()).click(function() {
				$this.cycle('next');
			});
			$(".prev", $this.parent()).click(function() {
				$this.cycle('prev');
			});
		});
	};
})(jQuery);
/**
 * Marquee
 */
(function($) {
	$.fn.PacMarquee = function(o) {
		var config = {
			fx: "fade",
			timeout: 0
		};
		var options = $.extend(config, o);
		return this.each(function() {
			var $this = $(this);
			var media = {};
			
			$this.find(".marquee").each(function(i) {
				media[$(this).parent().attr("id")] = $(this).parent().html();
			});
			options.before = function(c, n, o, f){
				var $c = $(this);
				$c.find(".marquee").each(function(i) {
					$c.html("");
				});
			};
			options.after = function(c, n, o, f){
				var $c = $(this);
				if(media[$c.attr("id")]) {
					$c.html(media[$c.attr("id")]);
					$.fn.PacMarquee.loadMedia($this);
				}
			};
			$this.cycle(options);
			$(".next", $this.parent()).click(function() {
				$this.cycle('next');
			});
			$(".prev", $this.parent()).click(function() {
				$this.cycle('prev');
			});
		});
	};
	$.fn.PacMarquee.media = {};
	$.fn.PacMarquee.loadMedia = function(target) {
		$(".marquee", target).media({
			width: 955,
			height: 400, 
			autoplay: true,
			attrs: { scale: 'aspect' }
		});
	}
})(jQuery);
/**
 * Hover
 */
(function($) {
	$.fn.PacHover = function(o) {
		var config = {};
		var options = $.extend(config, o);
		return this.each(function() {
			$(this).hover(
				function() {
					$(".hover", this).PacFadeIn('fast');
				},
				function() {
					$(".hover", this).PacFadeOut('fast');
				}
			);
		});
	};
})(jQuery);
/**
 * Listing
 */
(function($) {
	$.fn.PacListing = function(o) {
		var config = {
			url: "http://www.pacslo.org/calendar/listing"
		};
		var options = $.extend(config, o);
		this.each(function() {
			var $this = $(this);
			$(".container", $this).hide();
			$(this).find(".genre").each(function(i) {
				$(this).click(function() {
					$.fn.PacListing.request($this, options.url, $(this).attr("rel"));
				});
			});
			if(options.flag == '') {
				$.fn.PacListing.request($this, options.url, 'all');
			} else if(options.flag != 'category') {
				$.fn.PacListing.request($this, options.url, options.flag);
			}
		});
		return this;
	};
	$.fn.PacListing.request = function(target, url, genre) {
		var $target = $(target);
		$(".container", $target).PacFadeOut('slow', function() {
			if(genre == 'all') {
				genre = '';
			}
			$.get(url + "/category/" + genre, function(data) {
				$target.html(data);
				if($(".container:hidden", $(this))) {
					$(".container", $target).PacFadeIn("slow");
				}
				if(genre.length > 0) {
					$("#genre_"+genre).addClass("active");
				} else {
					$("#genre_all").addClass("active");
				}

			});
		});
	};
})(jQuery);
/**
 * Calendar
 */
(function($) {
	$.fn.PacCalendarItems = function(o) {
		var config = {
			
		};
		var options = $.extend(config, o);
		return this.each(function() {
			var $this = $(this);
			$this.find(".active").each(function(i) {
				var path = $(this).find("a").attr("href");
				$(this).find("a").attr("href","Javascript:void(0);");
				if(path) {
					$(this).simpletip({
						fixed: true,
						content: "Loading...",
						onBeforeShow: function() {
							this.load(path);
						}
					});
				}
			});
		});
	};
})(jQuery);
/**
 * Calendar
 */
(function($) {
	$.fn.PacCalendar = function(o) {
		var config = {
			url: "http://www.pacslo.org/modules/calendar"
		};
		var options = $.extend(config, o);
		return this.each(function() {
			var $this = $(this);
			$(".control", $this).click(function() {
				var path = $(this).attr("href");
				$(this).attr("href","Javascript:void(0);");
				$.fn.PacCalendar.load($this,path);
			});
			$this.PacCalendarItems();
		});
	};
	$.fn.PacCalendar.load = function(target, url) {
		$.get(url, function(data) {
			target.html(data);
		});
	}
})(jQuery);
/**
 * A Better Fade
 */
(function($) {
	$.fn.PacFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.PacFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.PacFadeTo = function(speed,to,callback) {
		return this.animate({opacity: to}, speed, function() {
			if (to == 1 && jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback();
		});
	};
})(jQuery);
