$(function() {	
// Plugin: Animate Background Position
(function ($) {
    if (!document.defaultView || !document.defaultView.getComputedStyle) { // IE6-IE8
        var oldCurCSS = $.curCSS;
        $.curCSS = function (elem, name, force) {
            if (name === 'background-position') {
                name = 'backgroundPosition';
            }
            if (name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[name]) {
                return oldCurCSS.apply(this, arguments);
            }
            var style = elem.style;
            if (!force && style && style[name]) {
                return style[name];
            }
            return oldCurCSS(elem, 'backgroundPositionX', force) + ' ' + oldCurCSS(elem, 'backgroundPositionY', force);
        };
    }

    var oldAnim = $.fn.animate;
    $.fn.animate = function (prop) {
        if ('background-position' in prop) {
            prop.backgroundPosition = prop['background-position'];
            delete prop['background-position'];
        }
        if ('backgroundPosition' in prop) {
            prop.backgroundPosition = '(' + prop.backgroundPosition;
        }
        return oldAnim.apply(this, arguments);
    };

    function toArray(strg) {
        strg = strg.replace(/left|top/g, '0px');
        strg = strg.replace(/right|bottom/g, '100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, "$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
    }

    $.fx.step.backgroundPosition = function (fx) {
        if (!fx.bgPosReady) {
            var start = $.curCSS(fx.elem, 'backgroundPosition');
            if (!start) {//FF2 no inline-style fallback
                start = '0px 0px';
            }

            start = toArray(start);
            fx.start = [start[0], start[2]];
            var end = toArray(fx.end);
            fx.end = [end[0], end[2]];

            fx.unit = [end[1], end[3]];
            fx.bgPosReady = true;
        }
        //return;
        var nowPosX = [];
        nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
        nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
        fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];

    };
})(jQuery);
	
	// Plugin: Shadowbox
	Shadowbox.init();
	
	/* Rollovers for Top Navigation
	****************************************/
	$("#social_links li a img").mouseover(function() {
		var onSrc = $(this).attr("src").replace("_off", "_on");
		$(this).attr("src", onSrc);
	}).mouseout(function() {
		var offSrc = $(this).attr("src").replace("_on", "_off");
		$(this).attr("src", offSrc);
	});
	
	/* Navigation buttons
	*****************************************/
	$("#content_nav li a, #link_projects, #link_relics").click(function() {
		// determine target		
		var section = $(this).attr('href').substring(1)
		var target = "#content_" + section;
		$('#content').scrollTo(target, 800);		
		
		// reset relics and projects
		if(section == "projects" || section == "relics") {
			resetProjects();
		}
		
		// scroll to target
		var xPos = 0;
		switch(section) {
			case "home": xPos = 0; break;
			case "projects": xPos = -2000; break;
			case "relics": xPos = -4000; break;
			case "about": xPos = -6000; break;
			default: xPos = 0; break;
		}
		
		// animate BG colors
		$("#body_wrap").animate({ backgroundPosition: xPos + 'px 0px' }, 1200);
		
		return false;
	});
	
	/* Twitter feed
	*******************************************/
	$("#tweet_list").tweet({
	  join_text: "auto",
	  username: "zubawing",
	  avatar_size: 24,
	  count: 3,
	  auto_join_text_default: "",
	  auto_join_text_ed: "",
	  auto_join_text_ing: "",
	  auto_join_text_reply: "",
	  auto_join_text_url: "",
	  loading_text: "loading tweets..."
	});
	
	/* Click project/relic link
	***********************************************/
	var currSlide = 1;
	var totalSlides = 1;
	$("#projects_top a, #projects_bottom a").click(function() {
		// reset relics and projects
		resetProjects();
		
		// get project info
		var projectName = $(this).attr("href").substring(1);
		
		// hide main div
		$(this).parent("div").parent(".section").hide();
		
		// load project div
		$("#content_projects .section:last-child").show();
		$("#content_projects .section:last-child").load("project.php?id=" + projectName, function(response, status, xhr) {
  			if (status == "error") {
    			var msg = "Oops! The selected content could not be found!";
  			}
			else {
				totalSlides = parseInt($("#total_slides").html());
			}
		});
				
		return false;
	});
	
	$("#relics_top a, #relics_bottom a").click(function() {
		// reset relics and projects
		resetProjects();
		
		// get relic info
		var relicName = $(this).attr("href").substring(1);
		
		// hide main div
		$(this).parent("div").parent(".section").hide();
		
		// show loading
		$("#content_projects #projects_circle").show();
		
		// load project div
		$("#content_relics .section:last-child").show();
		$("#content_relics .section:last-child").load("project.php?id=" + relicName, function(response, status, xhr) {
  			if (status == "error") {
    			var msg = "Oops! The selected content could not be found!";
  			}
			else {
				totalSlides = parseInt($("#total_slides").html());
				
				// remove loading
				$("#content_projects #projects_circle").hide();
			}
		});
		
		return false;
	});
	
	$(".section").delegate("div #project_back","click",function() {
		resetProjects();
		return false;
	});
	
	function resetProjects() {
		// remove project info
		$("#content_projects .section:last-child").html("");
		$("#content_relics .section:last-child").html("");
		
		// restore main divs
		$("#content_projects .section:first-child").show();
		$("#content_relics .section:first-child").show();
		
		// reset slide
		currSlide = 1;
	}
	
	/* Project prev/next slide
	*************************************************/
	$(".section").delegate("div #s_arrowright", "click", function() {
		if(currSlide < totalSlides) {
			currSlide++;
			switchSlide(currSlide);
		}
		return false;
	});
	
	$(".section").delegate("div #s_arrowleft", "click", function() {
		if(currSlide > 1) {
			currSlide--;
			switchSlide(currSlide);
		}
		return false;
	});
	
	function switchSlide(slideNum) {
		$("#curr_slide").html(currSlide);
		var yPos = 385 * (currSlide - 1);
		$('#slideshow').scrollTo(yPos, 200);
	}
	
	/* Manually open Shadowbox
	*****************************************************/
	$(".section").delegate("div div ul li #shadowbox_ecard","click",function(){
		Shadowbox.open({
				player:     'iframe',
				title:      'E-Card',
				content:    'src/ecard.htm',
				height:     520,
				width:      780
			});
		return false;
	});
	$(".section").delegate("div div ul li #shadowbox_sieo","click",function(){
		Shadowbox.open({
				player:     'iframe',
				title:      'SIEO Bulletin Board',
				content:    'src/sieo/index.htm',
				height:     700,
				width:      990
			});
		return false;
	});
	$(".section").delegate("div div ul li #shadowbox_jatitan","click",function(){
		Shadowbox.open({
				player:     'iframe',
				title:      'J.A. Titan Poster Ad',
				content:    'src/jatitan.jpg',
				height:		437,
				width:		790
			});
		return false;
	});
});
