// JavaScript Document

// jQuery FADE

jQuery.fn.rollFade = function(rollin, rollout) {
	$(this).removeClass('HL').find('a').append('<span class="HOVER" />').each(function () {
		var $span = $('> span.HOVER', this).css('opacity', 0);
		$(this).hover(function () {
			$span.stop().fadeTo(rollin, 1);
			}, function () {
			$span.stop().fadeTo(rollout, 0);
		});
	});
}

// MENU FADEIN
jQuery.fn.menuFade = function() {
	if ( hash_value > 0 ) {
		$(this).fadeIn(125, function() {
			$(this).next("#MENU li").menuFade();
			if ( $.browser.msie )
					this.style.removeAttribute('filter');
		});
	}
	else {
		$("#MENU li").hide();
	}
};

// ANCHOR FIX (HIJAX)
var a_href;
var split_href;
var new_href;
var email;

$("a").live("click", function(event) {
	event.preventDefault();
	
	$(this).blur();
	
	if ( $(this).attr("href") !== undefined ) {
		a_href = $(this).attr("href");
		if ( a_href.indexOf('?p=') !== -1 ) {
			split_href = a_href.split("?p=");
			new_href = split_href[1];
		}
		else {
			new_href = a_href;
		}
		
	}
	
	if ( $(this).hasClass('BLANK') ) {
		window.open(new_href);
	}
	else if ( $(this).hasClass('EMAIL') ) {
		email = $(this).attr("id");
		
		window.location = "mailto:"+email+"@voidsync.com";
	}
	else if ( $(this).hasClass('SPLASH') ) {
		window.location = "http://www.voidsync.com/";
	}
	else {
		window.location.hash = new_href;
	}
});

// CROSS BROWSER SETTINGS
var ban_start;
var ban_left;

if ( $.browser.msie && $.browser.version < 8 ) {
	ban_start = "-324";
	ban_left = "-797";
}
else {
	ban_start = "0";
	ban_left = "-470";
}


// SLIDE FADE (MAIN, SIDE, CONTENT)
var page_m; var page_s;
var page_type;
var this_type;
var loadFade;

var valid_page = new Array("home","services","portfolio","about","contact","hosting","sitemap");
var page_count = valid_page.length;

jQuery.fn.fadeTo = function(speed, to, callback) {
	return this.animate({opacity: to}, speed, function() {
			if (to == 1 && jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback();
		});
}

jQuery.fn.newFade = function(page_type, callback) {
	$(this).stop(false,true).fadeOut("fast", function() {
		$(this).empty().load(page_type, function() {
			if (jQuery.isFunction(callback))
				callback();
			if ( $.browser.msie ) {
				$(this).show();
				this.style.removeAttribute('filter');
			}
			else {
				$(this).fadeIn("slow");
			}
		});
	});
}

var hash_value;
var current_hash = false;

var hash_page;
var page; var current_page;

var mainSuf = '.php?type=main'; var sideSuf = '.php?type=side';

var change = 0;

var win_ns;

$( function() {
    setInterval(function() {
		
		if ( window.location.hash !== current_hash ) {
			
			current_hash = window.location.hash;
			
			if ( window.location.hash == '' ){
				if ( hash_value !== 1 ) {
					if ( $("#BANNER a").hasClass('CYAN') ) {
						$("#BANNER a").addClass("MAGENTA").removeClass("CYAN");
					}
				}
				else {
					var banner_size = $("#BANNER a").size();
					if ( banner_size > 1 ) {
						$("#BANNER a").stop();
						$("a.CYAN").remove();
					}
					
					if ( loadFade !== null ) {
						clearTimeout(loadFade);
						$("#CAPTION, #CONTENT").stop(false,true);
					}
					
					$("#SPLASH_HEAD, #FOOT, #BANNER, .HOVER").stop();
					if ( $("#BANNER a").hasClass('CYAN') ) {
						$("#BANNER a").addClass("MAGENTA").removeClass("CYAN");
					}
				}
				
				hash_value = 0;
				
				$("#SPLASH_HEAD").css("height","50%");
				$("#BANNER, .HOVER").animate({marginLeft: ban_start}, 0);
				
				$("#CONTAINER").css("height","auto");
				
				$("#CAPTION, #CONTENT, #MENU li").hide();
				
				$("#FOOT").css("display","none");
				
				$("#NEW").html("enter <b>high-bandwidth</b> version");
				$("#SPLASH").show();
			}
			
			else {
			
				if ( hash_value == 2 ) {
					hash_value = 3;
				}
				
				hash_page = current_hash.replace(new RegExp('[#]'),'');
					
				if ( hash_page.indexOf('&') !== -1 ) {
					page_split = hash_page.split('&');
					page = page_split[0];
					split_count = page_split.length;				}
				else {
					page = hash_page;
					split_count = 0;
				}
				
				valid = false;
				
				for ( p = 0; p <= page_count; p++ ){
					if ( valid_page[p] === page ) {
						valid = true;
					}
				}
				
				if (valid!==true) {
					page = "home";
				}
				
				page_m = page + mainSuf;
				page_s = page + sideSuf;
				
				for( i = 1; i < split_count; i++ ) {
					page_m += '&' + page_split[i];
				}
				
				if ( current_page == null ) {
					current_page = page;
				}
				
				if ( hash_value !== 1 ) {
					
					$("#SPLASH_HEAD").css("height","0");
					$("#SPLASH").hide();
					$("#BANNER, .HOVER").animate({marginLeft: ban_left}, 0);
					$("#CAPTION, #CONTENT, #MENU li").show();
					
					if ( $.browser.msie && $.browser.version == 7 ) {
						$("#CONTAINER").css("min-height","100%");
						$("#CONTENT").css("margin-bottom","80px");
						$("#FOOT").css("margin","-80px 0 0 0");
					}
					else {
						$("#CONTAINER").css("height","100%");
					}
					
					$("#FOOT").css("display","inline-block");
					
					if ( $("#BANNER a").hasClass('MAGENTA') ) {
						$("#BANNER a").addClass("CYAN").removeClass("MAGENTA");
					}
					
					if ( hash_value !== 5 ) {
						if ( current_page !== page ) {
							current_page = page;
							$("#SIDE").newFade(page_s, function() {
							$("#HOSTING li").rollFade(150,300);
						});
						}
						
						$("#MAIN").newFade(page_m);
					}
				}
			}
			
			if ( hash_value == 5 ) {
				$("#MAIN").empty().load(page_m);
				$("#SIDE").empty().load(page_s, function() {
					$("#HOSTING li").rollFade(150,300);
				});
				hash_value = 2;
			}
		}
		
		$(".EMAIL").each( function() {
			if ( $(this).attr("title") !== undefined ) {
				$(this).attr("title","");
			}
		});
    }, 10); 
});

$(window).ready( function() {
	
	win_ns = window.location.href;
		
	if ( win_ns.indexOf('?') !== -1 ) {
		window.location = win_ns.replace("?p=","#");
	}
	
	$("link[href*='styles/ns_splash.css'],link[href*='styles/ns_main.css']").remove();
	
	if ( window.location.hash !== '' ) {
		hash_value = 5;
	}
	else {
		hash_value = 0;
	}
	
});

$(document).ready( function() {
	
	if ( $.browser.msie && $.browser.version < 7 ) {
		$("#NEW").hide();
		$("#OLD").show();
		$("#BANNER a").attr("href","");
	}
	else {
		$("#NEW").show();
		$("#OLD").hide();
	}
	
	$("#BANNER li").rollFade(215,350);
	
	$(".MAGENTA").livequery("click", function() {
		this.blur();
		
		if ( hash_value == 0 ) { 
			hash_value = 1;
		
			$("#BANNER li").append('<a class="CYAN" href="#"><span class="HOVER" /></a>');
			$("a.CYAN").css("opacity",0).css("z-index",4).animate( {opacity: 1}, 1350, function() {
				$("a.MAGENTA").removeClass("MAGENTA");
				$("a.CYAN").remove();
				$("#BANNER a").addClass("CYAN");
			});
			$("#SPLASH").fadeOut(400);
			$("#SPLASH_HEAD").animate( {height: 125}, 750, function () {
				
				if ( $.browser.msie && $.browser.version == 7 ) {
					$("#CONTAINER").css("min-height","100%");
					$("#CONTENT").css("margin-bottom","80px");
					$("#FOOT").css("margin","-80px 0 0 0");
				}
				else {
					$("#CONTAINER").css("height","100%");
				}
				
				loadFade = setTimeout( function() { 
					$("#CAPTION").fadeIn(1000, function() {
						if ( $.browser.msie )
							this.style.removeAttribute('filter');
						$("#CONTENT,#MAIN,#SIDE").fadeIn("600", function() {
							if ( $.browser.msie ) {
								this.style.removeAttribute('filter');
								$("#CONTENT,#MAIN,#SIDE").css("display","block");
							}
							hash_value = 2;
						});
					});
					$("#FOOT").fadeIn("slow",function() {
						if ( $.browser.msie )
							this.style.removeAttribute('filter');
					});
				}, 125 );
				$("#BANNER, .HOVER").animate( {marginLeft: ban_left}, 800, function() {
					$("#MENU li:first").menuFade();
				});
			});
		}
	});
});