/* Declare a namespace for the site */
var Site = window.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {
	
	//same as $(document).ready();
	$(function() {


		$( ".accordion" ).accordion({
			autoHeight: false,
			active: $(".sidenav li.active").index()
		});
		$( ".fancybox" ).fancybox({overlayColor : "#000", titlePosition : 'outside',overlayOpacity: 0.9, 'transitionIn'	: 'none','transitionOut' : 'none'});
		$(".fancybox").each(function(i, f){
			$.data(f, 'title', $(f).attr('title'));
			$(f).attr('title', '');
		});
		$("#contact_form").validate();
		$(".sc-scrubber").wrap("<div class=\"hidden\"/>");
		
        if($("#artist").length>0){
			$(window).load(function() {
				$("#artist #events .active a").trigger("click");
				if($("#artist #events .active").length == 0){
					$("#artist #events .events-item .event-body").slideUp();
				}
			});
			$("#artist #events .events-item a").click(function(){
				$(this).closest(".events-item").find(".event-body").slideDown();
				$(this).closest(".events-item").siblings().each(function(i, sibling){
					$(sibling).find(".event-body").slideUp();
				});
				return false;
			});
			$(".view-more-events-btn").click(function(){
				$(this).slideUp();
				$("#artist #events .events-item:hidden .event-body").hide();
				$("#artist #events .events-item:hidden").slideDown();
				return false;
			});
		}
		
        // artists specific
        if($("#artists").length>0){
			$(window).load(
			    function() {
		            if($("#artists-categories").children(".artists-category").length>1){
		                function change(){
		                    //$("#artists-categories").children().css("position", "absolute");
		                    var nextItem = $("#artists-categories").children(":visible").next();
		                    if(nextItem.length<1){
		                        nextItem = $("#artists-categories").children(":first");
		                    }
		                    $("#artists-categories").children(":visible").fadeOut(function(){
		                        $(".btns li").removeClass("current");
								var nextLi = $(".btns li:nth-child("+(nextItem.index()+1)+")");
		                        nextLi.addClass("current");
		                        nextItem.fadeIn();
								$( ".accordion" ).accordion("activate" , $("#"+nextLi.attr("id").replace("btn-", "li-")).index());
		                        setTimer();
		                    });
		                }
		
		                function setTimer(){
		                    clearInterval(timer);
		                    var secs = $("#artists-categories").children(":visible").children().length;
		                    if(secs < 4){
		                        secs = 3;
		                    }else if(secs > 6){
		                        secs = 6;
		                    }
		                    timer = setInterval( change, (secs*1000));
		                }
		
		                var timer = null;
		                setTimer();
						$(".btns li:first").addClass("current");
		            }
			    }
			);
        }
        if ($("#artists").length > 0 || $("#artists-categories").length > 0) {
			$(window).load(
			    function() {
					$("#artists-categories a[class^=artist-], #artists a[class^=artist-], #sidebar a[class^=artist-]").hover(function(){
						var key = getArtistKey(this);
						$("#sidebar ."+key).addClass('sidebarHover');
						$("#artists-categories a."+key+", #artists a."+key+"").fadeTo("normal", 1.5);
					}, function(){
						var key = getArtistKey(this);
						$("#sidebar ."+key).removeClass('sidebarHover');
						$("#artists-categories a."+key+", #artists a."+key+"").fadeTo("normal", 1);
					});
					
					function getArtistKey(link){
						var artistKey = "";
						$($(link).attr('class').split(' ')).each(function() { 
					        if (this !== '' && this.indexOf("artist-") === 0) {
								artistKey = this;
								return false;
					        }    
					    });
						return artistKey;
					}
			    }
			);
		}

		if($("#home").length > 0){
			$("#home-promo, #features-of-the-month, #footer, #news-flash, #looking-ahead").hide();
			$("li.controls").disableTextSelect();
			$(window).load( function(){
				var tickerImages = $(".photo-ticker-container .imgs img");
				var tickerImgWidth = 86+11;
				$(".photo-ticker-container .imgs").css("width", (tickerImages.length * tickerImgWidth)+"px");
				function photoTicker(){
					$(".photo-ticker-container .imgs").animate({
						"left": "-="+($(".photo-ticker-container .imgs").width())
					}, 260000, "linear", function(){
						$(this).css("left", (4 * tickerImgWidth)+"px");
						photoTicker();
					});
				}
				photoTicker();
				$("#pre-image-loader, .caption").hide();
				$("#header, #home-promo").fadeIn("fast");
				setTimeout( "$(\"#news-flash\").fadeIn(\"fast\", function(){$('#js-news').ticker({titleText: ''});});", (0.2*1000));
				setTimeout( "$(\"#features-of-the-month\").fadeIn(\"fast\");", (0.2*1000));
				setTimeout( "$(\"#looking-ahead\").fadeIn(\"fast\");", (0.2*1000));
				setTimeout( "$(\"#footer\").fadeIn(\"fast\");", (0.5*1000));
				var intervalId;
				function slide(id, element){
					var $active = $('#' + id + ' ' + element + '.active');
					
					if ($active.length == 0) 
						$active = $('#' + id + ' ' + element + ':last');
					
					var $next = $active.next().length ? $active.next() : $('#' + id + ' ' + element + ':first');
					
					$active.addClass('last-active');
					
					$next.css({
						opacity: 0.0
					}).addClass('active').animate({
						opacity: 1.0
					}, 1000, function(){
						$active.removeClass('active last-active');
					});
				}
				
				$("#slideshow").hover(function(){
					$(this).find(".caption").fadeIn();
				}, function(){
					$(this).find(".caption").fadeOut();
				});
				
				function slideSwitch(){
					slide("slideshow", "div");
					clearInterval(intervalId);
					intervalId = setInterval(slideSwitch, 9000);
				}
				intervalId = setInterval(slideSwitch, 12000);
				
				
				function romSwitch(){
					slide("rom", "img");
					slide("rom", "span");
				}
				if($(".rom-content").length>1){
					setInterval(romSwitch, 7000);
				}
				
				function aomSwitch(){
					slide("aom-images", "a");
					slide("aom-read-more", "a");
					slide("aom-text", "div");
				}
				if($(".aom-img").length>1){
					setInterval(aomSwitch, 10000);
				}
				
				function flashSwitch(){
					slide("news-flash-items", "div");
				}
				setInterval(flashSwitch, 7000);
				
				$("#news-flash .next").click(function(){
					var nextFlash = $("#news-flash-items div.active").next();
					if(nextFlash.length == 0){
						nextFlash = $("#news-flash-items div").first();
					}
					$("#news-flash-items div").removeClass("active");
					nextFlash.addClass("active");
					return false;
				})
				
				$("#news-flash .prev").click(function(){
					var prevFlash = $("#news-flash-items div.active").prev();
					if(prevFlash.length == 0){
						prevFlash = $("#news-flash-items div:last");
					}
					$("#news-flash-items div").removeClass("active");
					prevFlash.addClass("active");
					return false;
				});
			});
		}
		
	});


	$(window).bind("load", function() {
		
		
	
	});
	
    $.extend($.fn.disableTextSelect = function() {
        return this.each(function(){
            if($.browser.mozilla){//Firefox
                $(this).css('MozUserSelect','none');
            }else if($.browser.msie){//IE
                $(this).bind('selectstart',function(){return false;});
            }else{//Opera, etc.
                $(this).mousedown(function(){return false;});
            }
        });
    });
	
})(jQuery);
