//
// Older browser detection.
//function noIE6() {
//	var browser=navigator.appName;
//	var b_version=navigator.appVersion;
//	var version=parseFloat(b_version);
//	if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version<=6)) {
//  		//alert("It's time to upgrade your browser!");
//		//window.location = "noIE6.html";
//  	}
//}
//
// Shadowbox.
Shadowbox.init({
	handleOversize: "drag",
	modal: true
});
//
// Get url variables.
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
//
// jQuery Carousel.
function mycarousel_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
	
}; 
//
// Konami Code!
//(function($) {
//	var konamiListeners = [];
//	
//	var kode = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
//	var progress = 0;
//
//	$.extend({
//		konami: function(listener, sequence) {
//			konamiListeners.push(listener);
//			
//			if (sequence) kode = sequence;
//		}
//	});
//
//	$(document)
//		.bind("keyup", function(event) {
//			var c = event.keyCode;
//			
//			if (c == kode[progress])
//				progress++;
//			else
//				progress = c == kode[0] ? 1 : 0;
//
//			if (progress == kode.length) {
//				if (konamiListeners.length > 0) {
//					$.each(konamiListeners, function(item) {
//						this();
//					});
//				}
//			}
//		});
//})(jQuery);
//
//$.konami(function() {
//    //console.log ('Cheater!!');
//	//
//	// Shadow box for Contra video.
//	Shadowbox.open({
//        content:    '<div><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/YBMQhKK3Ctk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/YBMQhKK3Ctk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></div>',
//        player:     "html",
//        title:      "Contra speed run (NES)",
//        height:     385,
//        width:      480
//    });
//	//
//	// Change logo to Contra logo.
//	$('#hdr_logo').html('<img src="images/konami/contra_logo.png" alt="" name="logo" width="395" height="206" id="logo" />');
//	// Change header nav to Contra nav.
//	$('a#home').html('<a href="http://www.scott-m-brown.com"><img src="images/konami/team_icon.png" width="65" height="50" align="absmiddle" />&nbsp;30 Lives!</a>');
//	$('a#folio').html('<a href="http://www.scott-m-brown.com"><img src="images/konami/team_icon.png" width="65" height="50" align="absmiddle" />&nbsp;30 Lives!</a>');
//	$('a#contact').html('<a href="http://www.scott-m-brown.com"><img src="images/konami/team_icon.png" width="65" height="50" align="absmiddle" />&nbsp;30 Lives!</a>');
//	// Change image carousel to ... ya you guessed it, Contra.
//	$('#rcnt').html('<ul id="mycarousel" class="jcarousel-skin-tango"><li><a href="#"><img src="images/konami/screen_load.png" width="320" height="227" /></a></li><li><a href="#"><img src="images/konami/screen_lvl1.png" width="320" height="227" /></a></li><li><a href="#"><img src="images/konami/screen_lvl2.png" width="320" height="227" /></a></li><li><a href="#"><img src="images/konami/screen_lvl3.png" width="320" height="227" /></a></li><li><a href="#"><img src="images/konami/screen_lvl4.png" width="320" height="227" /></a></li><li><a href="#"><img src="images/konami/screen_lvl5.png" width="320" height="227" /></a></li><li><a href="#"><img src="images/konami/screen_lvl6.png" width="320" height="227" /></a></li><li><a href="#"><img src="images/konami/screen_lvl7.png" width="320" height="227" /></a></li><li><a href="#"><img src="images/konami/screen_lvl8.png" width="320" height="227" /></a></li></ul>');
//	// Reload carousel.
//	jQuery('#mycarousel').jcarousel({
//		auto: 6,
//		wrap: 'last',
//		animation: 'slow',
//		initCallback: mycarousel_initCallback 
//	})
//	// Make all the h1 tags power ups.
//	$('h1').html('<img src="images/konami/power-up.gif" align="absmiddle" />&nbsp;Power up!');
//	// Some cool pixelated fire.
//	$('#rcnt_wrp').addClass('fire')
//	// Hoooopty!!!
//	$('div.me').addClass('show')
//});
//
// Doc ready jQuery.
jQuery(document).ready(function() {
	//
	// jQuery png fix.
	$(document).pngFix();
	//
	// Boot user if running IE 6.
	if ( $.browser.msie ) {
		//alert( $.browser.version );
		if ($.browser.version <= 6) {
			window.location.href = "noIE6.html";	
		}
	}
	//
	// Preload CSS images.
	$.preloadCssImages();
	//
	// Jquery tooltips (qTip)
	$('#social a[title]').qtip({ 
		style: { 
			name: 'cream', 
			tip: true,
			padding: 10,
			'font-size': 14,
			'font-weight': 'bold',
			background: '#a28554',
			color: '#ffffff',
			border: {
				radius: 3,
				color: '#7A5E2F'
			}
		},
		position: {
		  corner: {
			 target: 'topMiddle',
			 tooltip: 'bottomRight'
		  }
   		}
	})
	$('#kuler a[title]').qtip({ 
		style: { 
			name: 'cream', 
			tip: true,
			padding: 6,
			'font-size': 12,
			//'font-weight': 'bold',
			background: '#303030',
			color: '#ffffff',
			border: {
				radius: 2,
				color: '#161616'
			},
			width: {
				max: 300	
			}
		},
		position: {
		  corner: {
			 target: 'center',
			 tooltip: 'topLeft'
		  }
   		}
	})
	
	// Poplight pop-up (contact form). 
	// Many thanks to Soh Tanaka. (www.sohtanaka.com).
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
	
		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value
	
		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/icons/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
	
		//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
	
		//Apply Margin to Popup
		$('#' + popID).css({
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
	
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
	
		return false;
	});
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
		$('#fade , .popup_block').fadeOut(); //fade them both out
		$('#fade').remove();
		return false;
	});
	
});
//
// Default text clear.
function clearDefault(e) {
	if(e.defaultValue == e.value) {
		e.value = "";
	}
	$(e).setStyle({
		borderColor: '#0CF',
		color: '#FFB31F'
	});
}
function restoreDefault(e) {
	if(e.value == "") {
		e.value = e.defaultValue;
	}
	$(e).setStyle({
		borderColor: '#333',
		color: '#666'
	});
}
//
// Validate CSS.
function validate(str) {
	url = window.location;
	if (str == "css") {
		window.open('http://jigsaw.w3.org/css-validator/validator?uri=' + url + '&profile=css3&usermedium=all&warning=1&lang=en', 'W3C CSS Validation');
	} else if (str == "xhtml") {
		//window.open('http://jigsaw.w3.org/css-validator/validator?uri=' + url, 'W3C CSS Validation');	
	}
}
//
// Smooth Scroll
var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) && 
          ( (lnk.pathname == location.pathname) ||
	    ('/'+lnk.pathname == location.pathname) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },

  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }
  
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;
  
    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }
    if (!destinationLink) destinationLink = document.getElementById(anchor);

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;
  
    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
  
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);
  
    cypos = ss.getCurrentYPos();
  
    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL =
setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } 
}

ss.STEPS = 25;

ss.addEvent(window,"load",ss.fixAllLinks);
//
