$(function() {

	//var xmlhttp;
	var s;

	function loadXMLDoc(_url, framename)
	{
		  var url = "http://salimsh.hopto.org/SendEmail.aspx?url=";
		  url += _url;
		  document.getElementById(framename).src = "";
		  document.getElementById(framename).src = url;
	}

	//time to shorten the text
	$('div.entryText').each(
		function(){
			if (this.innerHTML.length > 450)
			{	var text = this.innerHTML;
				text = text.substring(0,450) + "...continue reading on original source.";
				this.innerHTML = text;
			}
	});

	/*$('div.top6Box').click(function(){
		$(this).hide();
		$('div.top6Box_hover').show();
		$('div.allHeadlinesBox_hover').hide();
		$('div.allHeadlinesBox').show();
		$('div.top6').fadeIn('slow');
		$('div.homeContainer').hide();
	});

	$('div.allHeadlinesBox').click(function(){
		$(this).hide();
		$('div.allHeadlinesBox_hover').show();
		$('div.top6Box_hover').hide();
		$('div.top6Box').show();
		$('div.homeContainer').fadeIn('slow');
		$('div.top6').hide();
	});*/

	$('div.seeMoreArrow').click(function(){
		$('.allHeadlines').css({"background" : "transparent url('images/arrow.png') no-repeat center"});
		$('.allHeadlines').css({"background-position" : "bottom center"});
		$('div.top6Headlines').css({"background" : "none"});
		$('.allHeadlines').css({'color' : 'white'});
		$('div.top6Headlines').css({'color' : '#8F8F8F'});
		$('div.homeContainer').fadeIn('slow');
		$('div.top6').hide();
		scroll(0,0);
	});

	$('div.goBackArrow').click(function(){
		$('.top6Headlines').css({"background" : "transparent url('images/arrow.png') no-repeat center"});
		$('.top6Headlines').css({"background-position" : "bottom center"});
		$('div.allHeadlines').css({"background" : "none"});
		$('.top6Headlines').css({'color' : 'white'});
		$('div.allHeadlines').css({'color' : '#8F8F8F'});
		$('div.top6').fadeIn('slow');
		$('div.homeContainer').hide();
	});

	$('div.top6Headlines').click(function(){
		$(this).css({"background" : "transparent url('images/arrow.png') no-repeat center"});
		$(this).css({"background-position" : "bottom center"});
		$('div.allHeadlines').css({"background" : "none"});
		$(this).css({'color' : 'white'});
		$('div.allHeadlines').css({'color' : '#8F8F8F'});
		$('div.top6').fadeIn('slow');
		$('div.homeContainer').hide();
	});

	$('div.allHeadlines').click(function(){
		$(this).css({"background" : "transparent url('images/arrow.png') no-repeat center"});
		$(this).css({"background-position" : "bottom center"});
		$('div.top6Headlines').css({"background" : "none"});
		$(this).css({'color' : 'white'});
		$('div.top6Headlines').css({'color' : '#8F8F8F'});
		$('div.homeContainer').fadeIn('slow');
		$('div.top6').hide();
	});


	$("div[rel]").overlay({
		finish : {
			top : 70
		},

		onLoad: function() {
			// this line does the magic. it makes the background image sit on top of the mask 
			//this.getBackgroundImage().expose({color: '#000', closeOnClick : false});
			//$('#overlay').css({"left" : "-400px"}).css({"position" : "absolute"});
			//this.getExposed().css({backgroundColor: 'black'}); 
			//this.getExposed().css({backgroundColor: 'black'});
		},  
		 
		// when overlay is closed take the expose instance and close it as well 
		onClose: function() {
			//$('#overlay').css({"display" : "none"});
			//$.expose.close();
		} 
	});

	$('.toptenButton').click(function(){
		$('div.nothing').hide();
		$('div.success').hide();
		document.getElementById('textBox').value = "";
	});

	$('div.addButtonContainer').click(
		function(){
			$('div.nothing').hide();
			$('div.success').hide();
			url = document.getElementById('textBox').value;
			
			if (url == "")
			{	$('div.nothing').fadeIn("slow");
			}
			else{
				loadXMLDoc(escape(url), 'iFrameAddSite');
				$('div.success').fadeIn("slow");
			}
	});

	function setSourceIcons(){
		$('i.green').each(function(i){
			if($(this).html() == "abcnews_entertainment"){
				$(this).html("abc");
			}
			else if($(this).html() == "movieweb_news"){
				$(this).html("movieweb");
			}
			else if($(this).html() == "billboardnews-daily"){
				$(this).html("billboard");
			}
			else if($(this).html() == "pheedcontent"){
				$(this).html("foxsports");
			}
			else if($(this).html() == "nbcmsnbc"){
				$(this).html("nbcsports");
			}
			else if($(this).html() == "abcnews_sports"){
				$(this).html("abcsports");
			}
			else if($(this).html() == "abcnews_international"){
				$(this).html("abcnews");
			}
		});
		$('.top6source').each(function(){
			if($(this).html() == "abcnews_entertainment"){
				$(this).html("abc");
			}
			else if($(this).html() == "movieweb_news"){
				$(this).html("movieweb");
			}
			else if($(this).html() == "billboardnews-daily"){
				$(this).html("billboard");
			}
			else if($(this).html() == "pheedcontent"){
				$(this).html("foxsports");
			}
			else if($(this).html() == "nbcmsnbc"){
				$(this).html("nbcsports");
			}
			else if($(this).html() == "abcnews_sports"){
				$(this).html("abcsports");
			}
			else if($(this).html() == "abcnews_international"){
				$(this).html("abcnews");
			}
		});
	}
	
	getTime();
	setSourceIcons();
	

	function getTime(){
		var date = new Date();
		var currentTime = date.getMinutes();
		if (currentTime <= 30)
		{	document.getElementById('siteUpdate').innerHTML = (30 - currentTime);
			
			document.getElementById('breakingNews').innerHTML = currentTime + " minutes ago";

		}
		else{
			document.getElementById('siteUpdate').innerHTML = (60 - currentTime);
			document.getElementById('breakingNews').innerHTML =  30 - (60 - currentTime) + " minutes ago";
			if ((60 - currentTime == 29) || (60 - currentTime == 30))
			{	document.getElementById('breakingNews').innerHTML = "1 minute ago";
			}
		}

		if (document.getElementById('siteUpdate').innerHTML == 1 || document.getElementById('siteUpdate').innerHTML == 0)
		{	document.getElementById('siteUpdate').innerHTML = 1 + " minute";
		}
		else{
			document.getElementById('siteUpdate').innerHTML = (document.getElementById('siteUpdate').innerHTML + " minutes");
		}
		var s = setTimeout(function(){ calcTime(); }, 300000);
	}

	function calcTime(){
		var currentTime = document.getElementById('siteUpdate').innerHTML;
		currentTime = currentTime.replace(' minutes','').replace(' minute','');
		if (currentTime > 5)
		{	currentTime = currentTime - 5;
			if (currentTime == 1)
			{	document.getElementById('siteUpdate').innerHTML = currentTime + ' minute';
			}
			else{
				document.getElementById('siteUpdate').innerHTML = currentTime + ' minutes';
			}
			
			s = setTimeout(function(){ calcTime(); }, 300000);
		}
		else{
			s = setTimeout(function(){ $('div.siteUpdate').html('<strong class="orange">This page has been updated! Click here to refresh.</strong>'); }, 300000);
			
		}
		
	}

	$('div.pagination').children().click(function(){
		var pageID = $(this).attr('id');
		$(this).parent().children().removeClass('selectedPage').addClass('unselectedPage');
		$(this).removeClass('unselectedPage').addClass('selectedPage');
		$(this).parent().parent().children('.showBlock').children('.show').animate({"top" : "-" + 115*pageID + "px"}, 500);
		//etPopup = (pageID)*5;

		//alert($(this).parent().parent().children('.showBlock').children('.show').attr('class'));

	});

	$('div.playBtn').click(function(){
		//tick upon click
		//$(this).parent().children('.showBlock').children('.show').animate({"top" : "-=23px"}, 500);
		$(this).hide();
		//alert($(this).attr('id'));
		$(this).parent().children('.pagination').fadeIn();

		//jump to page 2

		$(this).parent().children('.pagination').children('#1').parent().children().removeClass('selectedPage').addClass('unselectedPage');
		$(this).parent().children('.pagination').children('#1').removeClass('unselectedPage').addClass('selectedPage');
		$(this).parent().children('.pagination').children('#1').parent().parent().children('.showBlock').children('.show').animate({"top" : "-" + 115*1 + "px"}, 500);
		//$(this).parent().children('.pauseBtn').fadeIn();
		//$(this).parent().children('.showBlock').children('.overlayPlay').fadeOut();
		
		//ticker($(this).parent().attr('id'));
		//tickerCounter($(this).parent().children('.pauseBtn').attr('id'));
	});

	$('div.nextBtn').mouseup(function(){
		$(this).css({'margin-top' : '0px'}).css({"margin-right" : '2px'});
		$(this).parent().children('.prevBtn').css({"margin-right" : '2px'});
		$(this).parent().children('.prevBtnDisabled').css({"margin-right" : '2px'});
		var currentNum = $(this).parent().children('.pageNumber').html();
		currentNum++;
		$(this).parent().children('.pageNumber').html("" + currentNum);
		if (currentNum < 5)
		{	$(this).parent().children('.prevBtnDisabled').hide();
			$(this).parent().children('.prevBtn').show();
			$(this).parent().parent().children('.showBlock').children('.show').animate({"top" : "-=115px"}, 500);
		}
		else{
			$(this).hide();
			$(this).parent().parent().children('.showBlock').children('.show').animate({"top" : "-=115px"}, 500);
			$(this).parent().children('.nextBtnDisabled').show();
		}

	});

	$('div.nextBtn').mousedown(function(){
		$(this).css({'margin-top' : '2px'}).css({"margin-right" : "0px"});
		$(this).parent().children('.prevBtn').css({"margin-right" : '4px'});
		$(this).parent().children('.prevBtnDisabled').css({"margin-right" : '4px'});
	});

	$('div.nextBtn').mouseout(function(){
		$(this).css({'margin-top' : '0px'}).css({"margin-right" : '2px'});
		$(this).parent().children('.prevBtn').css({"margin-right" : '2px'});
		$(this).parent().children('.prevBtnDisabled').css({"margin-right" : '2px'});
	});

	$('div.prevBtn').mousedown(function(){
		$(this).css({'margin-top' : '2px'}).css({"margin-right" : "0px"});
	});

	$('div.prevBtn').mouseup(function(){
		$(this).css({'margin-top' : '0px'}).css({"margin-right" : '2px'});
		var currentNum = $(this).parent().children('.pageNumber').html();
		currentNum--;
		$(this).parent().children('.pageNumber').html("" + currentNum);
		if (currentNum > 1)
		{	$(this).parent().children('.nextBtnDisabled').hide();
			$(this).parent().children('.nextBtn').show();
			$(this).parent().parent().children('.showBlock').children('.show').animate({"top" : "+=115px"}, 500);
		}
		else{
			$(this).hide();
			$(this).parent().parent().children('.showBlock').children('.show').animate({"top" : "+=115px"}, 500);
			$(this).parent().children('.prevBtnDisabled').show();
		}
	});

	$('div.prevBtn').mouseout(function(){
		$(this).css({'margin-top' : '0px'}).css({"margin-right" : '2px'});
	});

	

	$('div.subCat').children().click(function(){
		//var clicked = $(this).html();
		//alert(clicked);

		//clicking on the edges breaks the code
		if (($(this).attr('class') != 'activeLHS') && ($(this).attr('class') != 'activeRHS') && ($(this).attr('class') != 'nextCategory')) {
			$(this).parent().children('div.activeLHS').remove();
			$(this).parent().children('div.activeRHS').remove();
			$(this).parent().children('div.active').removeClass('active').addClass('inactive');
			//var clicked = ($(this).children('div.active').html());
			//alert(clicked);
			$(this).before('<div class="activeLHS"></div>');// + '<div id="' + $(this).html().toString() + '" class="active">' + clicked +'</div><div class="activeRHS"></div>');
			$(this).after('<div class="activeRHS"></div>');
			//$('#' + clicked).parent().prepend('<div class="activeLHS"></div>');
			//$('#' + clicked).parent().append('<div class="activeRHS"></div>');
			$(this).removeClass('inactive').addClass('active');
			
			var clicked  = document.getElementById(this.id).id;
			//var clicked = $(this).html();
			var currentCategory = $(this).parent().parent().children('div.show').attr('id');
			//alert('clicked ' + clicked + ' current ' + currentCategory);

			//hide current Category
			$(this).parent().parent().children('div.show').removeClass('show').addClass('hide');

			//show the clicked on category
			$(this).parent().parent().children('#' + clicked + 'Column').removeClass('hide').addClass('show');

			//$('#' + (currentCategory)).parent().children('div.show').removeClass('show').addClass('hide');
			//$('#' + (clickedCategory)).parent().children('div.hide').removeClass('hide').addClass('show');
			
			
			/*var subcategories = new Array();
			$(this).children('div.inactive').each(function(j){
				subcategories[j] = $(this).eq(j).html();
			});*/
			//alert(subcategories[0]);
			//$('div.subCat').children().remove();
		}

	});

	$('div.nextCategory').mousedown(function(){
		$(this).css({"margin-top" : "2px"}).css({"margin-right" : "0px"});
	});

	$('div.nextCategory').mouseup(function(){
		$(this).css({"margin-top" : "0px"}).css({"margin-right" : "2px"});
	});

	$('div.nextCategory').mouseout(function(){
		$(this).css({"margin-top" : "0px"}).css({"margin-right" : "2px"});
	});

	$('div.nextCategory').click(function(){
		//first determine how many there are in total
		var numChildren =  $(this).parent().children().length - 2;
		
		//find out the current selected column is
		var thisOne = $(this).parent().children('.active');

		$(this).parent().children('.activeLHS').remove();
		$(this).parent().children('.activeRHS').remove();
		

		if (thisOne.is(":nth-child(" + (numChildren - 1) + ")"))
		{	//currently at the last one -- so fill up the first one over again
			var thisOneId = $(this).parent().children('div').eq(0).attr('id');
			//alert(thisOneId);
			//hide current Category
			$(this).parent().parent().children('div.show').removeClass('show').addClass('hide');

			//show the clicked on category
			$(this).parent().parent().children('#' + thisOneId + 'Column').removeClass('hide').addClass('show');

			thisOne.removeClass('active').addClass('inactive');
			$(this).parent().children('div').eq(0).addClass('active').removeClass('inactive');
			$(this).parent().children('div').eq(0).before('<div class="activeLHS"></div>');
			$(this).parent().children('div').eq(1).after('<div class="activeRHS"></div>');
		}
		else{
			var thisOneId = thisOne.next().attr('id');

			//var currentCategory = $(this).parent().parent().children('div.show').attr('id');
			//alert('clicked ' + clicked + ' current ' + currentCategory);

			//hide current Category
			$(this).parent().parent().children('div.show').removeClass('show').addClass('hide');

			//show the clicked on category
			$(this).parent().parent().children('#' + thisOneId + 'Column').removeClass('hide').addClass('show');

			thisOne.removeClass('active').addClass('inactive');
			thisOne.next().addClass('active').removeClass('inactive');
			thisOne.next().before('<div class="activeLHS"></div>');
			thisOne.next().next().after('<div class="activeRHS"></div>');
		}

		//fix this if its the last one


		//$(this).parent().children('.active').removeClass('.inactive');
	});


}); 

