// JavaScript Document

$(document).ready(function() {		
	/********** CAMERA GALLERY IMAGES WHEN CLICKED **********/			   
	$("a[id^='get_camera']").livequery('click', function(event) {													
		$("#clicked").val("clicked");												 
		var temp = $(this).attr("id");
		temp = temp.split("%");
		point_id = temp[1];
		var point_tmp_id = temp[3];
		point_photo = 'cams/' + temp[2];
		$("img[name='changeme']").attr("src",point_photo);
		$("img[name='changeme']").attr("id",point_id);	
		$("div[id='" + point_tmp_id + "']").css("backgroundColor", "#F2C505");	
		add_view();
	});	
	
	/********** CAMERA GALLERY IMAGES ON MOUSEOVER **********/	
	$(".gallery").livequery('mouseover', function(event) {
		$(".cg").each(function (i) {				
			$(".cg").css("backgroundColor", "#FFFFFF");
		});
		closeWindow();										  
		var temp = $(this).attr("id");
		temp = temp.split("%");
		var point_id = temp[1];
		var point_tmp_id = temp[3];
		point_photo = temp[2].substr(1);
		point_photo = 'C' + point_photo;
		point_photo = 'cams/' + point_photo;	
		$("img[name='thumb_" + point_tmp_id + "']").attr("src",point_photo);
		$("img[name='thumb_" + point_tmp_id + "']").attr("id",point_id);	
		$("img[name='thumb_" + point_tmp_id + "']").attr("width","120px;");
		$("img[name='thumb_" + point_tmp_id + "']").attr("height","81px;");
		$("div[id='thumbnail_" + point_tmp_id + "']").show();
		$("div[id='arrow_" + point_tmp_id + "']").show();
		$("div[id='" + point_tmp_id + "']").css("backgroundColor", "#F2C505");
	});

	/********** CAMERA GALLERY IMAGES ON MOUSEOUT **********/			
	$(".gallery").livequery('mouseout', function(event) {										 
		$(".cg").each(function (i) {				
			$(".cg").css("backgroundColor", "#FFFFFF");
			$(".thumbnail").hide();	
			$(".arrow").hide();
		});
		var clicked = $("#clicked").val();
		if (clicked != "") {
			var temp = $(this).attr("id");
			temp = temp.split("%");
			var point_tmp_id = temp[3];	
			$("div[id='" + point_tmp_id + "']").css("backgroundColor", "#F2C505");
			$("#clicked").val("");
		}
	});	
	/********** CAMERA GALLERY SCROLL RIGHT ARROW **********/		
	$(".arrow_down").livequery('click', function(event) {
		var h = $("#high").val();
		if (h == 19) {
			 $("#high").val(1);	
		}
		else {
			var tmp = Number(h) + 1;
			$("#high").val(tmp);	
		}
		var h = $("#high").val();
		var l = $("#low").val();
		if (h != 1 || h != 19) {
			var tmp = Number(l) + 1;
			$("#low").val(tmp);			
		}
		if (h == 1) {
			 $("#low").val(12);	
		}	
		if (h == 8) {
			 $("#low").val(1);	
		}
		ajax();
	});
	/********** CAMERA GALLERY SCROLL LEFT ARROW **********/	
	$(".arrow_up").livequery('click', function(event) {
		var l = $("#low").val();
		if (l == 1) {
			 $("#low").val(19);	
		}
		else {
			var tmp = l - 1;
			$("#low").val(tmp);	
		}
		var h = $("#high").val();
		if (h == 1) {
			$("#high").val(19);
		}
		else {
			var tmp = Number(h) - 1;
			$("#high").val(tmp);	
		}
		ajax();
	});
	/********** REGULAR CAMERAS INDIVIDUAL CHECKBOXES **********/
	$(".cc").click(function(){									   
		showCameras();
	});	
	/********** LARGE CAMERA VIEW INDIVIDUAL CHECKBOXES **********/
	$(".ccc").click(function(){									   
		showFullCameras();
	});	
	
	$("#cameras_22_select_all").click(function(){									   
		$("#cameras_22_column input").attr("checked","checked");
		showCameras();
	});	
	
	$("#cameras_22_deselect_all").click(function(){
		$("#cameras_22_column input").removeAttr("checked");
		showCameras();	
	});	
	
	$("#cameras_78_select_all").click(function(){				
		$("#cameras_78_column input").attr("checked","checked");
		showCameras();
	});
	
	$("#cameras_78_deselect_all").click(function(){
		$("#cameras_78_column input").removeAttr("checked");
		showCameras();	
	});	

	$("#cameras_22_select_all_full").livequery('click', function(event) {									   
		$("#22_cams input").attr("checked","checked");
		showFullCameras();	
	});	
	
	$("#cameras_22_deselect_all_full").livequery('click', function(event) {
		$("#22_cams input").removeAttr("checked");
		showFullCameras();		
	});	
	
	$("#cameras_78_select_all_full").livequery('click', function(event) {				
		$("#78_cams input").attr("checked","checked");
		showFullCameras();	
	});
	
	$("#cameras_78_deselect_all_full").livequery('click', function(event) {
		$("#78_cams input").removeAttr("checked");
		showFullCameras();		
	});	

	$("#cameras_select_all").click(function(){				
		$("#cameras_all input").attr("checked","checked");	
		$("#list").val("1,2,3,4,5,6,8,9,10,11,12,14,15,16,17,19,20,22,23");
		showCameras();	
	});	
	
	$("#cameras_deselect_all").click(function(){
		$("#cameras_all input").removeAttr("checked");
		$("#list").val("");
		showCameras();	
	});

	$("#cameras_22_select_all_customize").click(function(){									   
		$("#cameras_22_column input").attr("checked","checked");
		customizeCameras();
	});	
	
	$("#cameras_22_deselect_all_customize").click(function(){
		$("#cameras_22_column input").removeAttr("checked");
		customizeCameras();
	});	
	
	$("#cameras_78_select_all_customize").click(function(){				
		$("#cameras_78_column input").attr("checked","checked");
		customizeCameras();
	});
	
	$("#cameras_78_deselect_all_customize").click(function(){
		$("#cameras_78_column input").removeAttr("checked");
		customizeCameras();
	});	
	
	$("#cameras_select_all_customize").click(function(){				
		$("#cameras_all input").attr("checked","checked");
		customizeCameras();
	});	
	
	$("#cameras_deselect_all_customize").click(function(){
		$("#cameras_all input").removeAttr("checked");	
		customizeCameras();
	});
	
	$(".customize_cameras").click(function(){	
		var traffic_id = $("#traffic_id").val();
		var user_cameras_22 = "";
		var user_cameras_78 = "";
		$("#cameras_22_column .cc").each(function (i) {
			var id = $(this).val();	
			if ($("input[id='" + id + "']").is(":checked")) {
				user_cameras_22 = (user_cameras_22 == "") ? id :  user_cameras_22 + ',' + id;	
			}		
		});
		$("#cameras_78_column .cc").each(function (i) {
			var id = $(this).val();	
			if ($("input[id='" + id + "']").is(":checked")) {
				user_cameras_78 = (user_cameras_78 == "") ? id :  user_cameras_78 + ',' + id;	
			}		
		});	
		var string = "&traffic_id=" + traffic_id + '&user_cameras_22=' + user_cameras_22 + '&user_cameras_78=' + user_cameras_78;
		$.ajax({
			type: "POST",
			url: "models/ajax-cameras.php",
			data: string,
			success: function(html){
				$("#show_msg").show();
				$("#show_msg").replaceWith('<div id="show_msg">' + html + '</div>');
			}
		});	
	});	
	
	$(".customize_cameras_full").click(function(){	
		var traffic_id = $("#traffic_id").val();
		var user_cameras_22 = "";
		var user_cameras_78 = "";
		$("#22_cams .ccc").each(function (i) {
			var id = $(this).val();	
			if ($("input[id='" + id + "']").is(":checked")) {
				user_cameras_22 = (user_cameras_22 == "") ? id :  user_cameras_22 + ',' + id;	
			}		
		});
		$("#78_cams .ccc").each(function (i) {
			var id = $(this).val();	
			if ($("input[id='" + id + "']").is(":checked")) {
				user_cameras_78 = (user_cameras_78 == "") ? id :  user_cameras_78 + ',' + id;	
			}		
		});	
		var string = "&traffic_id=" + traffic_id + '&user_cameras_22=' + user_cameras_22 + '&user_cameras_78=' + user_cameras_78;
		$.ajax({
			type: "POST",
			url: "models/ajax-cameras.php",
			data: string,
			success: function(html){
				$("#ci").show();	
				$("#ci").replaceWith('<div id="ci">' + html + '</div>');
			}
		});	
	});	
		
});

/**********  PHOTO GALLERY **********/
function ajax() {
	closeWindow();
	var low = $("#low").val();
	var high = $("#high").val();
	var string = "&low=" + low + '&high=' + high;
	$.ajax({
		type: "POST",
		url: "models/ajax-gallery.php",
		data: string,
		success: function(html){
			$("#camera_gallery").replaceWith(html);
		}
	});		
}

/**********  REGULAR CAMERA MARKERS **********/
function createCameraMarker(point, point_title, t_type, point_id ) {  
	var cameraIcon = new GIcon(G_DEFAULT_ICON);
	cameraIcon.image = 'images/map.camera.png';
	cameraIcon.iconSize = new GSize(32, 32);
	cameraIcon.iconAnchor = new GPoint(32, 16);
	cameraIcon.infoWindowAnchor = new GPoint(2, 16);
	cameraIcon.shadow = 'images/icon_shadow.png';
	cameraIcon.shadowSize = new GSize(32, 32);

	var marker = new GMarker(point, {point_title: point_title, type: t_type, point_id: point_id, icon: cameraIcon });	
	GEvent.addListener(marker, 'click', function() {
		description = "The Pennsylvania Department of Transportation serves The Morning Call with 18 real-time cameras in the Lehigh Valley area. These cameras are controlled at the PennDOT District Office on Hamilton Street in Allentown.";	
		$("#camera_information").slideDown();
		$("#camera_information").html('<span class="point_title">' + point_title  + '</span><a href="javascript:void(0);" onClick="closeWindow();"><img id="close" src="images/closebutton.gif" title="Close this window" class="close"/></a><br/><br/><center><img src="cache/' + point_id + '" width="354" height="240"><br/><br/>' + description + '</center>');	
	});		
	return marker;
}
/**********  FULL CAMERA MARKERS **********/
function createFullCameraMarker(point, point_title, t_type, point_id ) {  
	var cameraIcon = new GIcon(G_DEFAULT_ICON);
	cameraIcon.image = 'images/map.camera.png';
	cameraIcon.iconSize = new GSize(32, 32);
	cameraIcon.iconAnchor = new GPoint(32, 16);
	cameraIcon.infoWindowAnchor = new GPoint(2, 16);
	cameraIcon.shadow = 'images/icon_shadow.png';
	cameraIcon.shadowSize = new GSize(32, 32);

	var marker = new GMarker(point, {point_title: point_title, type: t_type, point_id: point_id, icon: cameraIcon });	
	GEvent.addListener(marker, 'click', function() {
		$("#details_container").show();	
		$("#camera_map_full").css("opacity",".2");
		$("#camera_information_full").replaceWith('<div id="camera_information_full"></div>');
		$("#camera_information_full").slideDown();
		$("#camera_information_full").html('<span class="point_title">' + point_title  + '</span><a href="javascript:void(0);" onClick="closeWindowFull();"><img id="close" src="images/closebutton.gif" title="Close this window" class="close"/></a><br/><br/><center><img src="cache/' + point_id + '" width="354" height="240"></center>');	
	});		
	return marker;
}
/********** CLOSE WINDOW ON REGUALR CAMERA VIEW **********/
function closeWindow() {
	$("#camera_information").replaceWith('<div id="camera_information"></div>');
	$("#camera_information").hide();
	$("#camera_gallery").css("opacity","1");
	$("#empty").val("");
}
/********** CLOSE WINDOW ON LARGE CAMERA VIEW **********/
function closeWindowFull() {
	$("#camera_information_full").replaceWith('<div id="camera_information_full"></div>');
	$("#camera_information_full").hide();
	$("#details_container").hide();
	$("#camera_map_full").css("opacity","1");
	$("#empty").val("");
}
/********** SHOW NEW CAMERA POINTS ON MAP **********/
function customizeCameras() {
	closeWindow();
	var user_cameras_22 = "";
	var user_cameras_78 = "";
	$("#cameras_22_column .cc").each(function (i) {
		var id = $(this).val();	
		if ($("input[id='" + id + "']").is(":checked")) {
			user_cameras_22 = (user_cameras_22 == "") ? id :  user_cameras_22 + ',' + id;	
		}		
	});
	$("#cameras_78_column .cc").each(function (i) {
		var id = $(this).val();	
		if ($("input[id='" + id + "']").is(":checked")) {
			user_cameras_78 = (user_cameras_78 == "") ? id :  user_cameras_78 + ',' + id;	
		}		
	});	
	var user_cameras = user_cameras_22 + ':' + user_cameras_78;
	$("#traffic_cameras").val(user_cameras);
}

/********** REGULAR CAMERA MAP **********/
function showCameras() {
	closeWindow();
	$("#update").replaceWith("");
	if (GBrowserIsCompatible()) {	
		var trafficInfo = new GTrafficOverlay();
		var map = new GMap2(document.getElementById("camera_map"));
		map.setCenter(new GLatLng(0,0),0);
		var bounds = new GLatLngBounds();
		map.removeMapType(G_HYBRID_MAP);
		map.addControl(new ExtMapTypeControl({showTraffic: true, showTrafficKey: true}));
		map.addControl(new GSmallMapControl());
      	
		GDownloadUrl("./cache/cameras.xml", function(data) {									 
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
				var t_type = markers[i].getAttribute("type");
				var point_title = markers[i].getAttribute("title");
				var id = markers[i].getAttribute("id");	
				var marker = createCameraMarker(point, point_title, t_type, id);
				map.addOverlay(marker);
				if ($("input[id='" + id + "']").is(":checked")) {
					$("#empty").val("true");
					marker.show();
					bounds.extend(point);
				}
				else {
					marker.hide();	
				}
			}
			var empty = $("#empty").val();
			if (empty == "" || empty == undefined) {
				map.setCenter(new GLatLng(40.602229,-75.471537), 11);
			}
			else {
				map.setZoom(map.getBoundsZoomLevel(bounds));
				map.setCenter(bounds.getCenter());
			}
		});
	}	
}
/**********  FULL CAMERA MAP **********/
function showFullCameras() {
	closeWindowFull();
	$("#update").replaceWith("");
	if (GBrowserIsCompatible()) {	
		var trafficInfo = new GTrafficOverlay();
		var map = new GMap2(document.getElementById("camera_map_full"));
		map.setCenter(new GLatLng(0,0),0);
		var bounds = new GLatLngBounds();
		map.removeMapType(G_HYBRID_MAP);
		map.addControl(new ExtMapTypeControl({showTraffic: true, showTrafficKey: true}));
		map.addControl(new GLargeMapControl());
		GDownloadUrl("./cache/cameras.xml", function(data) {						 
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
				var t_type = markers[i].getAttribute("type");
				var point_title = markers[i].getAttribute("title");
				var id = markers[i].getAttribute("id");	
				var marker = createFullCameraMarker(point, point_title, t_type, id);
				map.addOverlay(marker);
				if ($("input[id='" + id + "']").is(":checked")) {	
					$("#empty").val("true");
					marker.show();
					bounds.extend(point);
				}
				else {
					marker.hide();	
				}
			}
			var empty = $("#empty").val();
			if (empty == "" || empty == undefined) {
				map.setCenter(new GLatLng(40.602229,-75.471537), 11);
			}
			else {
				map.setZoom(map.getBoundsZoomLevel(bounds));
				map.setCenter(bounds.getCenter());
			}
		});
	}	
}

function add_view() {
	s_s2.pageName="The Morning Call / Lehigh Valley Traffic - front.";
	s_s2.server="www.mcall.com"; 
	s_s2.channel="The Morning Call:news:traffic:lehigh valley traffic";
	
	s_s2.prop38="Lehigh Valley Traffic";
	s_s2.eVar21="Lehigh Valley Traffic";
	s_s2.prop20=""
		
	/* E-commerce Variables */
	s_s2.events=""; 
	s_s2.eVar20="";
	
	s_s2.hier1="The Morning Call:news:traffic:lehigh valley traffic"; 
	s_s2.hier2="news:traffic:lehigh valley traffic";
	s_s2.hier4="news:traffic:lehigh valley traffic";
		
	
	//AD Block Detection
	//Description: 	Check if any IMG, IFRAME, or SCRIPT elements are ad blocked (set to not display).
	//		Also check for the occurance of any Norton installed code to block popups or ads ( ie.)SymError ).
	//		Only need to find 1 occurrance of any of the above to confirm ad blocking is used. 
	//		No need to continue searching through other portions of the page.
	
	var imgArray = document.images;
	var iframeArray = document.getElementsByTagName("iframe");
	var scriptArray = document.getElementsByTagName("script");
	var isAdBlocked = 0;
	
	
	if(window.SymError || window.SymWinOpen || window.SymRealWinOpen)
	{	isAdBlocked=1; }
	
	if (!isAdBlocked)
	{	for (var i=0; i<imgArray.length; i++)
		{		if(imgArray[i].style.display == 'none')
			{			isAdBlocked=1;			break;} }	}
	
	if (!isAdBlocked)
	{	for (var j=0; j<iframeArray.length; j++)
		{	if (iframeArray[j].style.display == 'none')
			{			isAdBlocked=1;			break;} }	}
	
	if (!isAdBlocked)
	{	for (var j=0; j<scriptArray.length; j++)
		{		if (scriptArray[j].style.display == 'none')
			{			isAdBlocked=1;			break;} }	}
	
	if(isAdBlocked)
	{	s.prop50="ad blocking";}

}