// jQuery.preloadImages = function()
// {
//   for(var i = 0; i<arguments.length; i++)
//   {
//     jQuery("<img>").attr("src", arguments[i]);
//   }
// }
// 
// $.preloadImages("/images/banner/BCF(1).jpg", "/images/banner/BCF(2).jpg",
// "/images/banner/BCF(3).jpg", "/images/banner/BCF(4).jpg", "/images/banner/BCF(5).jpg", "/images/banner/BCF(6).jpg");

// 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");

$("#banners img").hide();
$("#banners img").load(function() {
	// $(this).height(223);
	// alert("here");
	$(this).css("display", "block")
});


// Setup JS events when the DOM is ready
$(document).ready(function()
{
	// Ultrasound department product filter
	$("#filter_animal").change(tictoc.website.application_selector);
	
	$('#features').tabs({fxFade: true});
	
	$("input.search_box").focus(tictoc.website.clearbox);
	$("input.search_box").blur(tictoc.website.clearbox);
	
	// Popup links
	$("a.popup").each(tictoc.website.popup);
	
	// Admin links
	$("a.adminedit").click(tictoc.admin.edit);

	// Homepage banner animation
	// $("#banners img").height(0);
	// $("#banners img").hide();
	// $("#banners img").load(function() {
	// 	// $(this).height(223);
	// 	// alert("here");
	// 	$(this).css("display", "block")
	// });
	// 
	if ($("#banners").length > 0) $.slideshow('banners', 4500);
	
	$("#news h2 + p").addClass("afterh2");
	
	if ($("#sites li").length > 0) {
		$("#sites ul").hover(function(){tictoc.website.show_sites()},function(){tictoc.website.hide_sites()});
	}
});


// General website functions
tictoc.website = {
		
	// Select the relevant applications for an animal
	application_selector: function() {
		applications = [[ 'Pregnancy', '1', ['1','3','6','7','9','8','10','11'] ],
						[ 'Fertility', '2', ['1','2','3','7','10'] ],
						[ 'Meat Quality', '4', ['1','4','11'] ],
						[ 'Musculoskeletal', '6', ['2','6','7'] ],
						[ 'Ophthalmic', '7', ['2','6','7'] ],
						[ 'Abdominal General', '5', ['2','6','7'] ],						
						[ 'Abdominal Advanced', '10', ['2','6'] ],
						[ 'Cardiology General', '11', ['2','6','7'] ],
						[ 'Cardiology Advanced', '12', ['2','6','7'] ]
						];
						
		apps = [];
		
		for(var i=0; i<applications.length; i++) 
		{
			for(var j=0; j<applications[i][2].length; j++) 
			{
				if (applications[i][2][j] == this.value) 
				{
					apps.push(applications[i]);
				};
			};
		};
		
		if (apps.length > 0)
		{
			$("#filter_app").empty();
			$("#filter_app").removeAttr("disabled");
			$("#filter_app").append("<option value=\"0\">Any</option>")
		 	$.each( apps, function(i, v){
				$("#filter_app").append("<option value=\""+v[1]+"\">"+v[0]+"</option>")
			});
		}
	},
	
	clear_options: function() {

	},
	
    // 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;
        } 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."
    },

	// Show site menu
	show_sites: function() {
		$("#sites li.active").addClass("normal");
		$("#sites li").show();
	},
	hide_sites: function() {
		$("#sites li").not(".active").hide();
		$("#sites li.active").removeClass("normal");
	}
};


// 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);
}