﻿var converter = new Showdown.converter();

$(document).ready(function(){

	// GLOBAL READMODE/EDITMODE
	// toggle editmode and readmode classes per editing status
		if($('#MSOLayout_InDesignMode').val() == '1') 
			$('html').addClass('editmode');
		else
			$('html').addClass('readmode');
	
	// GLOBAL DEV & TEST
	// use dev and test watermarks in the background when not in prod
		if(window.location.href.toLowerCase().indexOf("http://dev") != -1){
			$('html').addClass('dev');
		} else if(window.location.href.toLowerCase().indexOf("http://test") != -1){	
			$('html').addClass('test');
		}

	$('li.mapItemPin').hover(function(){
		$('li.mapItemDetails').fadeOut('normal').removeClass('showDetails');
		$('li.active').removeClass('active');
		$(this).addClass('active');	
		$(this).prev().show().addClass('showDetails');
		
	});

	/* Location Coordinate Boxes */
	$('.editmode ul.ContactMap').mouseenter(function(){
      $('.editmode ul.ContactMap').append('<div id=posX></div>');
      $('.editmode ul.ContactMap').append('<div id=posY></div>');
    }).mouseleave(function(){
      $('.editmode #posX').remove();
      $('.editmode #posY').remove();
    });
		
	$('#timeline dt').hover(function(){
		$('#timeline dd.active').fadeOut('normal').removeClass('active');
		$('#timeline dt.active').removeClass('active');
		$(this).addClass('active');	
		$(this).next().fadeIn('normal').addClass('active');
	});
	
	$('ul.ContactMap').mousemove(function(e){
	
      	var pxLeftToMap = parseInt($('ul.ContactMap').position().left) + 8;
      	var pxTopToMap = parseInt($('ul.ContactMap').position().top) + 3;
      	
		var x = e.pageX - pxLeftToMap ;  
		var y = e.pageY - pxTopToMap ;
		var top = (e.pageY - pxTopToMap ) - 185;	
		var left = x;
		
		x = x - 8;
		y = y - 149;
			
		$('div #posX').css("left", left - 50);
		$('div #posX').css("top", top);
		
		$('div #posY').css("left", left - 5);
		$('div #posY').css("top", top);
		
		$('div #posX').html('x: ' + x);
		$('div #posY').html('y: ' + y);	
      
	}); 


// Test Code

		$('ul.ContactMap').mousedown(function(e){
      	// Caluculate X - horizontal
      	var pxLeftToMap = parseInt($('ul.ContactMap').position().left) + 8;	// Pixels from left edge of the browser to the left edge of the map.
      	var pxLeftEdgeToCursor = e.pageX;									// Pixels from left edge of the browser to the cursor.
      	var xCoord = pxLeftEdgeToCursor  - pxLeftToMap;						// X coordinate of the hover box										
		var pxFromRightMapEdge = Math.abs(xCoord - 959);
      		
      	// Calculate Y - vertical 
	  	var pxTopToMap = parseInt($('ul.ContactMap').position().top) + 3;		// Pixels from top edge of the browser to the top edge of the map.
	  	var pxTopEdgeToCursor = e.pageY;									// Pixels from top edge of the browser to the cursor.
		var yCoord = (pxTopEdgeToCursor - pxTopToMap) - 240;				// X coordinate of the hover box
		
		

 //console.log('MouseDown: pxLeftToMap: ' + pxLeftToMap + ', pxFromScreenToCursor: ' + pxLeftEdgeToCursor + ', x: ' + xCoord + ', pxFromRightMapEdge: ' + pxFromRightMapEdge)
 console.log('MouseDown: pxTopToMap : ' + pxTopToMap + ', pxTopEdgeToCursor : ' + pxTopEdgeToCursor + ', y: ' + yCoord)		   
    });


// End Test


	$('.signupBox fieldset').siblings('span.success').fadeTo(600,1,function(){
		$(this).animate({top:"0"},900).fadeTo(2200,1,function(){
			$(this).animate({top:"-26px" },600);
		}).click(function(){
			$(this).stop().animate({top:"-26px"},100);
		});
	});

	$('.signupBox fieldset').siblings('span.failed').fadeTo(600,1,function(){
		$(this).animate({top:"0"},900).fadeTo(2200,1,function(){
			$(this).animate({top:"-42px" },600);
		}).click(function(){
			$(this).stop().animate({top:"-42px"},100);
		});
	});
	
	if( $('.signupBox span.success').length != 0 ) $('.signupBox fieldset input.textbox').val('');	
	
	$('.signupBox input.textbox').siblings('span.validationLabel').siblings('input.textbox').blur(function(){
		if( $(this).val() == '' ) $(this).siblings('span.validationLabel').fadeIn().fadeTo(1500,1,function(){
			$(this).fadeOut();
		})
		$(this).siblings('span.validationLabel').click(function(){
			$(this).stop().fadeOut(100);
		});
	});
	
	// Lightbox //
	// $("a[rel^='prettyPhoto']").prettyPhoto({theme: 'mychk', opacity: '0.75', showTitle: true, autoplay: true});
	
	$("a[rel^='prettyPhoto']").prettyPhoto({ showTitle: false, theme: 'facebook2' });
	
	// Markdown / Showdown //
	if( $( ".markdown" ).size() > 0 ) {
		$( ".markdown" ).each( function( i ){
			$(this).html( converter.makeHtml( $(this).html() ) );
		});
	}

	//Image Gallery Navigation
	var activeSet = false;	
	$('ul.imageGalleryCategoryList a.filterLink').removeClass('active').each(function(i){
		if(window.location.search == $(this).attr('href')){
			$(this).addClass('active');
			activeSet = true;
		}
	});
	if (!activeSet){
		if($('ul.imageGalleryCategoryList a.filterLink').size() == 1){
			$('ul.imageGalleryCategoryList a.filterLink').eq(0).addClass('active');
		}else{
			$('ul.imageGalleryCategoryList li.allImages a.filterLink').eq(0).addClass('active');
		}
	}

});


function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

