// Namespace function
function namespace(ns) {
    ns = ns.split('.');
    var cur = window, i;
    while ( i = ns.shift() ) {
        if ( !cur[i] ) cur[i] = {};
        cur = cur[i];
    }
}
// Put all tictoc functions into the tictoc object like:
// quartz.test = function() { alert("Test"); }
namespace("tictoc");


// Setup JS events when the DOM is ready
$(document).ready(function(){

	$(".search_box").focus(tictoc.website.clearbox);
	$(".search_box").blur(tictoc.website.clearbox);

	// Popup links
	$("a.popup").each(tictoc.website.popup);
	
	$("#print_page").click(tictoc.website.printpage);
	
	$('.styleswitch').click(function() 
	{
		switchStylestyle(this.getAttribute("rel"));
		return false;
	});	
	var c = readCookie('reidkerr_style');
	if (c) switchStylestyle(c);
	
    $('#finder .body').tabs({fxFade: true});
        
	// Homepage banner animation
	if ($("#banners").length > 1) $.slideshow('banners', 4500);
    
    $("a#tab_login").click(function(e) 
	{
		$("div#box_login").toggle();
		$("a#tab_login").toggleClass("active");
		e.preventDefault();	
	});
	
	// Admin links
	$("a.adminedit").click(tictoc.admin.edit)
	
	// Fix background image caching problem
    if (jQuery.browser.msie) {
        try { document.execCommand("BackgroundImageCache", false, true);
        } catch(err) {}
    };
	
	// Drop-down menu for IE
	if (document.all) $("#menu li").hoverClass ("sfHover");
	
	
    $('#body a[href*="youtube.com"]').flash(
        { width: 425, height: 350, wmode: 'transparent' },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.src = "http://www.youtube.com/v/" + this.href.match(/v=(.+)/)[1];
            $(this).before($.fn.flash.transform(htmlOptions));
            $(this).remove();
        }
    );

    $('#body a[href*="vimeo.com"]').flash(
        { width: 460, height: 343, quality:'best', allowfullscreen:'true', scale:'showAll' },
        { version: 8 },
        function(htmlOptions) {
            htmlOptions.src = "http://vimeo.com/moogaloop.swf?clip_id=" + this.href.match(/(\d+)/)[1] + "&amp;server=vimeo.com&amp;fullscreen=1&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=008DD3";
            $(this).before($.fn.flash.transform(htmlOptions));
            $(this).remove();
        }
    );
	

});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


// General website functions
tictoc.website = {
  
	// Hide labels, set text to targets value
	hide_labels: function() {
		$(this).find("label").each(function() {
			$('#' + this.htmlFor).val(this.innerHTML);
			$(this).hide();
			$('#' + this.htmlFor).click(tictoc.website.clearbox);
		})
	},

    // Clear default text in an input box
    clearbox: function() {
        if (!this.default_value) this.default_value = this.value;
    
        if (this.value == '') {
            this.value = this.default_value;
			this.select();
        } else if (this.value == this.default_value) {
            this.value = '';
        }
    },
    
    // Jump to URL
    jump_to_url: function(url) {
        if (url == "") return false;
	    location.href = "/" + url;
    },
    
    // Popup link
    popup: function() {
        this.target = "_blank";
        this.title =  this.title ? this.title += ". " : "";
        this.title += "Link opens in a new window."
    },

	printpage: function() {
		window.print();
	}

};


// Admin functions
tictoc.admin = {
    popup_width: 675,
    popup_height: 650,
    
    edit: function() {
        var win = window.open(this.href, "_adminedit","height=" + tictoc.admin.popup_height + ",width=" + tictoc.admin.popup_width + ",resizable=yes,dependent,scrollbars=yes");
	    win.focus();
	    return false;
    }
};


// jQuery fader
// Based on: http://portfolio.gizone.co.uk/applications/slideshow/
$.slideshow = function (containerId, timeout) {
	var current = 0;
	var id = '#' + containerId;
	$(id).css({position:'relative'});
	var slides = $(id).children().get();
	for ( var i = 0; i < slides.length; i++ ) {
		$(slides[i]).css({zIndex:(slides.length - i), position:'absolute', top:'0', left:'0'});
	}
	setTimeout((function(){$.slideshow.next(slides, timeout, current);}), timeout);
}
$.slideshow.next = function (slides, timeout, current) {
	for (var i = 0; i < slides.length; i++) {
		var slide = slides[(current + i) % slides.length];
		$(slide).css({zIndex:(slides.length - i)});
	}
	// IE doesn't seem to support .show() after it has been faded out, so we use .fadeIn("fast")
	$(slides[current]).fadeOut('slow', 
			function(){$(slide).css({zIndex:'0', opacity:1}).fadeIn("fast");}
			);
	
	current = (current + 1) % slides.length;
	setTimeout((function(){$.slideshow.next(slides, timeout, current);}), timeout);
}

// JQuery Style Switcher
function switchStylestyle(styleName)
{
    if(styleName=='text') {
	$('link').each(function(i) {
	    this.disabled = true;
		});
	} else {
	$('link').each(function(i) {
	    this.disabled = false;
		});
	}
	createCookie('reidkerr_style', styleName, 365);
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
