﻿String.prototype.startsWith = function(str){return (this.match("^"+str)==str)}
String.prototype.endsWith = function(str){return (this.match(str+"$")==str)}
var converter = new Showdown.converter();

$(document).ready( function(){

	//readmode & editmode
	if($("#MSOLayout_InDesignMode").val() == "1")  
		$("html").addClass("editmode");
	else
		$("html").addClass("readmode");


	//SEARCH
	$("#absoluteSearch input").keypress(function(event) { if (event.keyCode==13){searchThis($(this).val());return(false);} });
	$("#absoluteSearch a").click(function() { searchThis($(this).siblings("input").eq(0).val());return(false); });

	//search effects
	$("#absoluteSearch input").focus( function(){ $(this).parent().addClass("focus"); });
	$("#absoluteSearch input").blur ( function(){ if ($("#absoluteSearch input").val() == "") $(this).parent().removeClass("focus"); });
	$("#absoluteSearch input, #absoluteSearch a").hover( function(){ $(this).parent().addClass("hover"); }, function(){ $(this).parent().removeClass("hover"); } ); 


	//HOME
	$("#page-content.home").parent().siblings("a#bottom-left-badge").css("display", "none");
	$("#page-content.home").parent().siblings("#page-footer").css("top", "0");
	
	$("a[rel^='prettyPhoto']").prettyPhoto({theme: "mychk", opacity: "0.75", showTitle: false, autoplay: true});

	//Card Backgrounds//
	//hide card images with the background class and copy the path to the parent's background image style
	$(".wildcard img.background").css("display", "none");
	$(".wildcard img.background").parent().each(function (i) {
	    $(this).css("background-image", "url("+  $(this).find("img.background").attr("src") +")");
    });


	//Operations Map Navigation Popups //
	$(".operationsNav li a").hover( function(){ $( ".operationsMap dl." + $(this).attr("rel") ).toggleClass("hoverShow"); } );

	$(".operationsNav li a").each( function(){
		if( $(this).attr("href").toLowerCase().endsWith("operations") ||
			$(this).attr("href").toLowerCase().endsWith("operations/pages/default.aspx") ||
			$(this).attr("href").toLowerCase().endsWith("operations/pages/information.aspx") ){
			$(this).attr("href", "javascript:;");
			$(this).parent().addClass("noClickyClicky");
		}
	});


	/*FERC 720	*/
	$("td.ms-gb").each( function(i) {		
		var $textNodes = $(this).contents().filter( isTextNode );
		var sFolder = $textNodes.text().replace(":","");
		var $anchorNodes = $(this).find("a");
	    $anchorNodes.eq(1).text( getDisplayMonthCommaYear( trim10(sFolder) ) ).css("padding", "0 5px");
	    $textNodes.remove();
	});

	ExpCollGroup("1-1_","img_1-1_");
	
	// Markdown / Showdown //
	if( $( ".markdown" ).size() > 0 ) {
		$( ".markdown" ).each( function( i ){
			$(this).html( converter.makeHtml( $(this).html() ) );
		});
	}
	
	var activeSet = false;	
	$("ul.imageGalleryCategoryList li.filterItem").removeClass("active").each(function(i){
		if(window.location.search.replace("%20"," ") == $(this).find("a").eq(0).attr("href") ){
			$(this).addClass("active");
			activeSet = true;
		}
	});
	if (!activeSet){
		if($("ul.imageGalleryCategoryList li.filterItem").size() == 1){
			$("ul.imageGalleryCategoryList li.filterItem").eq(0).addClass("active");
		}else{
			$("ul.imageGalleryCategoryList li.allImages").eq(0).addClass("active");
		}
	}
	
	

	/* Igam Map */
	
	$('.editmode ul.IgamMap li').css("background-color", "pink");
	$('.editmode ul.IgamMap li').css("opacity", ".5");
	$('.editmode ul.IgamMap li').css("border", "1px solid black");

	$('.editmode ul.IgamMap').mousemove(function(e){
      
		var pos = $(this).position();
		pos.left = parseInt(pos.left);
		var x = e.pageX - pos.left;     
      	pos.top = parseInt(pos.top);
		var y = e.pageY - pos.top;   
		y = y - 136;
	
		$('div #posX').css("left", x - 40);
		$('div #posX').css("top", y - 25);
		
		$('div #posY').css("left", x + 5);
		$('div #posY').css("top", y - 25);
		
		$('div #posX').html('x: ' + x);
		$('div #posY').html('y: ' + y);	
      
	}); 
	
	$(".editmode ul.IgamMap").mouseenter(function(){
      $(".editmode ul.IgamMap").append('<div id=posX></div>');
      $(".editmode ul.IgamMap").append('<div id=posY></div>');
    }).mouseleave(function(){
      $(".editmode #posX").remove();
      $(".editmode #posY").remove();
    });
	
	$(".editmode ul.IgamMap").mouseup(function(e){
		var pos = $(this).position();
		pos.left = parseInt(pos.left);
		var x = e.pageX - pos.left;     
      	pos.top = parseInt(pos.top);
		var y = e.pageY - pos.top;   
		y = y - 136;

		var boxPos =  $('.editmode #tmpHoverBox').position();
		console.log('pos.left: ' + pos.left + ' boxPos.left: ' + boxPos.left + ' x: ' + x);
		console.log('pos.top: ' + pos.top + ' boxPos.top: ' + boxPos.top + ' y: ' + y);
		boxPos.left = parseInt(boxPos.left);
		var boxWidth = x - boxPos.left;
		boxPos.top = parseInt(boxPos.top);
		var boxHeight = y - boxPos.top;
		
		console.log('Width: ' + boxWidth);
		console.log('Height: ' + boxHeight);
	
		$('.editmode #tmpHoverBox').css("width", boxWidth);
		$('.editmode #tmpHoverBox').css("height", boxHeight);
		
		$('.editmode #tmpHoverInfo').css("height", 60);
		$('.editmode #tmpHoverInfo').css("width", 105);
		$('.editmode #tmpHoverInfo').css("left", x + 1);
		$('.editmode #tmpHoverInfo').css("top", (boxPos.top - boxHeight) -2);
		$('.editmode #tmpHoverInfo').html('Map Hotspot X: ' + boxPos.left + '</br>Map Hotspot Y: ' + boxPos.top + '</br>Height: ' + boxHeight + '</br>Width: ' + boxWidth)

		
    }).mousedown(function(e){
		var pos = $(this).position();
		pos.left = parseInt(pos.left);
		var x = e.pageX - pos.left;     
      	pos.top = parseInt(pos.top);
		var y = e.pageY - pos.top;   
		y = y - 136;
		$('.editmode ul.IgamMap #tmpHoverBox').remove();
		$('.editmode ul.IgamMap #tmpHoverInfo').remove();

		$(".editmode ul.IgamMap").append('<div id=tmpHoverBox></div>');
		$('.editmode #tmpHoverBox').css("left", x);
		$('.editmode #tmpHoverBox').css("top", y);   
		
		$(".editmode ul.IgamMap").append('<div id=tmpHoverInfo></div>');
		   
    });

	
});

function searchThis(keywordString){ window.location = "/Search/Pages/Results.aspx?s=www.chk.com&k=" + keywordString;  }

function trim12 (str) { var str = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length; while (ws.test(str.charAt(--i))); return str.slice(0, i + 1); } 

function trim10 (str) { //STUPID IE
	var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	for (var i = 0; i < str.length; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}
	for (i = str.length - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function isTextNode( iIndex ) { return this.nodeType == 3; }

function getDisplayMonthCommaYear( yearmo ) {

	var year = yearmo.substring(0,4);
	var mo   = yearmo.substring(4,6);
	var returnValue;
	switch(mo){
		case "01":
			returnValue = "January, " + year;
			break;
		case "02":
			returnValue = "February, " + year;
			break;
		case "03":
			returnValue = "March, " + year;
			break;
		case "04":
			returnValue = "April, " + year;
			break;
		case "05":
			returnValue = "May, " + year;
			break;
		case "06":
			returnValue = "June, " + year;
			break;
		case "07":
			returnValue = "July, " + year;
			break;
		case "08":
			returnValue = "August, " + year;
			break;
		case "09":
			returnValue = "September, " + year;
			break;
		case "10":
			returnValue = "October, " + year;
			break;
		case "11":
			returnValue = "November, " + year;
			break;
		case "12":
			returnValue = "December, " + year;
			break;
		default:
			returnValue = year;
			break;
	}
	return(returnValue);
}
/*
	//Igam Map Navigation Popups //
	$(".IgamNav li a").hover( function(){ $( ".IgamMap dl." + $(this).attr("rel") ).toggleClass("hoverShow"); } );

	$(".IgamNav li a").each( function(){
		if( $(this).attr("href").toLowerCase().endsWith("Igam") ||
			$(this).attr("href").toLowerCase().endsWith("Igam/pages/default.aspx") ||
			$(this).attr("href").toLowerCase().endsWith("Igam/pages/information.aspx") ){
			$(this).attr("href", "javascript:;");
			$(this).parent().addClass("noClickyClicky");
		}
	});
	
	$('ul.IgamMap .hoverBox').css("background-color", "pink");
	*/
