/**
 * BCF | BOOM YOUR BRAND
 */
$(document).ready(function() {
	
	
    // Config
    backgrounds_data = "/config/backgrounds.xml";
    slogans_data = "/config/slogans.xml";
    images_data = "/config/images.xml";



    // "Scroll To Here" function
    // Scrolls to the container specified by $(this)
    $.fn.toHere = function toHere() {
        var target = $(this).attr("id");
        var pos = ($("#" + target).offset().top);
        // alert(pos);
        $("html,body").stop().animate(
        {
            scrollTop: pos
        },
        {
            duration: 800,
            easing: 'easeOutCirc'
        });
    }
	
	$("dl a").click(function(e){
		e.preventDefault();
	});

    // Attach a click handler to each
    // matched navigation link
    $(".rightcolumn ul li a").click(function(e) {
        $(this).addClass("selected");
        $(".rightcolumn ul li a").not($(this)).removeClass("selected");

        // Config
        var container = $(this).attr("rel");

        // Move and hide
        $(this).each(function() {
			if ($(this).attr("rel") == "#home-content"){
				$(document).resetView();
			} else if ($(this).attr("rel") == "blog") {
				return false;
			} else {
				$(container).toHere();
				e.preventDefault();
			}
        });
    });

    // Choose Background Combination
    // Picks an image/color combo
    // for the page background
    $.fn.rotateBackground = function() {
        var combos = [];
        $.ajax({
            type: "GET",
            url: backgrounds_data,
            dataType: "xml",
            success: function(xml) {
                $(xml).find("combo").each(function(e) {
                    var id = $(this).attr("id");
                    var image = $(this).find("image").text();
                    var color = $(this).find("color").text();
                    var set = color + " url(\"/assets/backgrounds/" + image + "\")";
                    combos.push(set);
                });

                var i = Math.floor(Math.random() * combos.length) + 1;
                var background = combos[i - 1];

                $("body").css({
                    background: background,
                    backgroundPosition: "top center",
                    backgroundRepeat: "no-repeat",
                    backgroundAttachment: "fixed"
                });
            }
        });
    }

    // Deliver random marketing slogan
    $.fn.rotateSlogan = function(target) {
        var slogans = [];
        $.ajax({
            type: "GET",
            url: slogans_data,
            dataType: "xml",
            success: function(xml) {
                $(xml).find("slogan").each(function(e) {
                    var id = $(this).attr("id");
                    var superhead = $(this).find("superhead").text();
                    var headline = $(this).find("headline").text();
                    var slogan = "<h3>" + superhead + "</h3><h1>" + headline + "</h1>";
                    slogans.push(slogan);
                });
				
                var i = Math.floor(Math.random() * slogans.length) + 1;
                var copy = slogans[i - 1];
				$("#slogan").empty();
				$(copy).appendTo("#slogan");
				$("#slogan").fadeIn(3000);
            }
        });
    }

	// Get images/clips for
	// galleries
	$.fn.displayImages = function(target){
		var target = "#" + $(this).attr("id");
		var images = [];
		$.ajax({
			type: "GET",
			url: images_data,
			dataType: "xml",
			success: function(xml) {
				$(xml).find("image").each(function(e) {
					var title = "<h3>" + $(this).find("title").text() + "</h3>";
					var desc = $(this).find("description").text();
					var group = $(this).find("group").text();
					var fname = "/gallery/" + group + "/" + $(this).find("fname").text();
					var imgSrc = "<a href=\"" + fname + "\"rel=shadowbox[" + group + "] title=\"" + title + desc + "\">";
					images.push(imgSrc);
					// alert(imgSrc);
				});
				$.each(images, function(i, val) {
					$(target).append(val);
				});
			}
		});
	}
	
	// Logo events
	// Reset the scrollbar and light animation
	$("#logo").click(function(e){
		$(document).resetView();
		$(this).animate({top: "15px"}, 0).delay().animate({top: "10px"}, 300);
		$(".rightcolumn ul li#home a").addClass("selected");
        $(".rightcolumn ul li a").not($(".rightcolumn ul li#home a")).removeClass("selected");
	});
	
	//$(".rightcolumn ul li a").css({
	//  		backgroundPosition: '100px 0'
	//		})
	//	.mouseover(function(){
	//		$(this).stop().animate({
	//			backgroundPosition: '-90px 0'
	//		}, 300)
	//	.mouseout(function(){
	//		$(this).stop().animate({
	//			backgroundPosition: '100px 0'
	//		}, 300);
	//	});
	//});
	
	// Quickie: resets the scrollview
	$.fn.resetView = function(){
		$("html,body").animate({scrollTop: 0},{duration: 500, easing: 'easeOutCirc'});
	}
	
	// Parallax scrolling for
    // page backgrounds
    $.scrollingParallax("/assets/backgrounds/bubbles.png", {
        bgRepeat: true
    });
	
    // Run page functions
	$(document).resetView();
    $(document).rotateBackground();
	$("dl#film").displayImages();
    $("#slogan").rotateSlogan();

});

