//REGEX FOR EXTERNAL LINKS
var regex = RegExp('^(?:f|ht)tps?://(?!' + location.hostname + ')');

$('#outer').removeClass('nojs');

$(document).ready(function(){

// MODIFY HTML CONTENT
	// DISPLAY JAVASCRIPT FRIENDLY STYLES

	// ADD HEADERS & FOOTERS TO CONTENT BOXES
	$('#body').css({padding:0}).each(function(){
		var c = $(this).html();
		$(this).html('<div class="head"><div class="foot">' + c + '</div></div>');
	});

	$('#nav').css({margin:0}).each(function(){
		var c = $(this).html();
		$(this).html('<div class="head"><div class="foot">' + c + '</div></div>');
	});

	// ANIMATE NAVIGATION DROP DOWNS
	$('#navcolumn > ul li').each(function(){

		$(this).mouseenter(function(){
			$(this).find("ul:first").css({zIndex:10}).stop(true,true).slideRight(150);
		});

		$(this).mouseleave(function(){
			$(this).find("ul:first").css({zIndex:0}).stop(true,true).delay(150).slideUp(150);
		});

	});

	//ADD LISTING LINKS TO li ELEMENT
	$('ul.listing li').css({cursor:'pointer'});
	$('ul.listing li').click(function(){window.location = $(this).find('a:first').attr('href'); return false;});

	$("a[href$=pdf]").addClass('filetype').addClass("pdf");
	$("a[href$=zip]").addClass('filetype').addClass("zip");

	$('a.enlarge').colorbox({
		title:function(){ 
			var original = $(this).attr('rel')
			return '<a href="'+original+'" target="_blank">Download Original</a>';
		},
		rel:'listingthumbs',
		photo:true
	});
	$('#container img:not(.system, form img, .buttonbar img, .buttons img, #gallery img)').each(function(){
		if($(this).attr('width') != ''){$(this).css({border:$(this).attr('width')+'px'});}
		if($(this).attr('height') != ''){$(this).css({border:$(this).attr('height')+'px'});}
		if($(this).attr('border') != ''){$(this).css({border:$(this).attr('border')+'px solid #000000'}).removeAttr('border');}
		if($(this).attr('vspace') != ''){$(this).css({marginTop:$(this).attr('vspace')+'px',marginBottom:$(this).attr('vspace')+'px'}).removeAttr('vspace');}
		if($(this).attr('hspace') != '' && $(this).attr('align') == 'left'){$(this).css({marginRight:$(this).attr('hspace')+'px'});}
		if($(this).attr('hspace') != '' && $(this).attr('align') == 'right'){$(this).css({marginLeft:$(this).attr('hspace')+'px'});}
		if($(this).attr('hspace') != '' && $(this).attr('align') == ''){$(this).css({marginRight:$(this).attr('hspace')+'px',marginLeft:$(this).attr('hspace')+'px'});}
		$(this).removeAttr('hspace');
		if($(this).attr('align') != ''){$(this).css({float:$(this).attr('align')}).removeAttr('align');}
	});
	$('#container img:not(.system, form img, .buttonbar img, .buttons img, #gallery img, #container.dartford-mp-working-for-you img)').each(function(){
		$(this).capty({height:47});
	});
	$('#container img').lazyload();

	if($('#map img').length > 0){
		$('#map').html($('#map').find('a').html()).append('<form id="searchByNameForm_Setup" method="post" action="http://www.election-maps.co.uk/searchByName.jsf" enctype="application/x-www-form-urlencoded" target="electionmap"><input type="hidden" name="searchByNameForm:mainsearchquery" value="dartford" /><input type="hidden" name="searchByNameForm:_id0" value="CON" /><input type="hidden" name="searchByNameForm:tempBoundaryAreaIDName" value="Dartford Co Const" /><input type="hidden" name="searchByNameForm" value="searchByNameForm" /><input type="hidden" name="searchByNameForm:_idcl" value="searchByNameForm:link" /><input type="hidden" name="boundaryid" value="" /></form><form id="searchByNameForm" method="post" action="http://www.election-maps.co.uk/electmaps.jsf" enctype="application/x-www-form-urlencoded" target="electionmap"><input type="hidden" name="searchByNameForm:mainsearchquery" value="dartford" /><input type="hidden" name="searchByNameForm:_id0" value="CON" /><input type="hidden" name="searchByNameForm:tempBoundaryAreaIDName" value="Dartford Co Const" /><input type="hidden" name="searchByNameForm" value="searchByNameForm" /><input type="hidden" name="searchByNameForm:_idcl" value="searchByNameForm:boundaryIDLink" /><input type="hidden" name="boundaryid" value="WMC07127" /></form>');
	$('#map').css({'cursor':'pointer'});
	$('#map').click(function(){
		$.fn.colorbox({
			iframe:true,
			fastIframe:false,
			innerWidth:780,
			innerHeight:700,
			href:'http://www.election-maps.co.uk/searchByName.jsf',
			onComplete:function(){
				$('#cboxLoadingOverlay').css('display','block');
				$('#cboxLoadingGraphic').css('display','block');
				$('#cboxContent iframe').css('display','none');
				var cboxframeid = $('#cboxContent iframe').attr('name');
				$('#cboxContent iframe').attr('id',cboxframeid);
				$('#searchByNameForm_Setup').attr('target',cboxframeid);
				$('#searchByNameForm').attr('target',cboxframeid);
				document.getElementById('searchByNameForm_Setup').submit();
				setTimeout('document.getElementById(\'searchByNameForm\').submit();',1500);
				setTimeout("composecolorbox('"+cboxframeid+"');",1500);
				}
			});
});
	}

}); // END DOCUMENT READY

function composecolorbox(thisiframe){
	$('#cboxLoadingOverlay').css('display','none');
	$('#cboxLoadingGraphic').css('display','none');
	$('#'+thisiframe).css('display','block');
}

//SCROLL MAIN NAVIGATION WHEN IT HITS WINDOW TOP
var nav = $('#navcolumn');
var navheight = nav.height();
var navpos = nav.offset();
var navposstart = navpos.top;
var contentheight = $('#banner').height()+$('#body').height();
var footerheight = $('#footer').height();

$(document).scroll(function(){
	if ((document.documentElement.scrollTop > navposstart || self.pageYOffset > navposstart ) && navheight < $(window).height()-footerheight && navheight < contentheight) {
		nav.css({position:'fixed'});
		nav.css({top:'0px'});
	} else if (document.documentElement.scrollTop < navposstart || self.pageYOffset < navposstart) {
		nav.css({position:'static'});
		nav.css({top:navposstart+'px'});
	}
});

//SCROLL 'BACK TO TOP'
$('#footer span.tab a').click(function(e){
	e.preventDefault();
	$('html').animate({scrollTop:0},1000)
});


