// JavaScript Document

//Headline Scroller

jQuery(document).ready(function($){
	
	var ANIM_SPEED = 300;
	var ANIM_DISTANCE = 117;
	var curPos = 0;
	var animating = false;
	
	var animOff = $('#animBlock').offset();
	
	function moveNext() {
		
		if ( !animating ) {
			
			animating = true;
			$('headlineNext').unbind('click');
			
			if ( curPos > 0 ) {				
				$('#animBlock').animate({top: '+=' + ANIM_DISTANCE}, ANIM_SPEED, function() { animating = false; $('#headlineNext').click(moveNext); });	
				curPos--;
			}
			else {
				$('#animBlock').animate({top: -ANIM_DISTANCE * 4}, ANIM_SPEED, function() { animating = false; $('#headlineNext').click(moveNext); });
				curPos = 4;	
			}
		}
	}
	
	function movePrev(){
		
		if ( !animating ) {	
			
			animating = true;
				
			if ( curPos < 4 ) {
				$('#animBlock').animate({top: '-=' +  ANIM_DISTANCE}, ANIM_SPEED, function() { animating = false; $('#headlinePrev').click(movePrev); });	
				curPos++;	
			}
			else {
				$('#animBlock').animate({top: 0}, ANIM_SPEED, function() { animating = false; $('#headlinePrev').click(movePrev); });
				curPos = 0;
			}
		}
	}
	
	$('#headlineNext').click(moveNext);
	
	$('#headlinePrev').click(movePrev);
});

// Align Footer boxes

jQuery(document).ready(function($){
	
	var height = 0;
	
	$('.footerBlock').each(function(index, element) {
        
		if ( $(this).height() > height )
			height = $(this).height();
    });
	
	$('.footerBlock').height(height);
});

// Home Dock Nav

jQuery(document).ready(function($) {
    
	var ANIM_SPEED = 300;
	var ANIM_DISTANCE = 102;
	
	
	$('#dockNav .left').css('cursor', 'pointer');
	$('#dockNav .right').css('cursor', 'pointer');
	
	/*$('<div style="position: fixed; float: left; top: 50px; left: 50px; z-index: 1000; direction: ltr;" id="dump"/>').appendTo('#base');
	$('#dump').text('Left Position: ');*/
	
//	$('#dockNav .right').hide();

	if ( $('#dock ul').position() != null ) {
	
	var base = $('#dock ul').position().left;
	var itemCount = $('#dock ul li').size();
	var navWidth = $('#dock').width();	
	var offset = 0;
	
	
	$('.dockThumb').hover(
		function(e){
			
			var prop = {width: '+=12', height: '+=12', 'margin-top': '-=0', 'margin-right': '-=6'};	
			var propImg = {width: '+=12', height: '+=12', 'margin-top': '-=0', 'margin-right': '+=0'};
			$(this).animate(prop, 100);
			$(this).children('img').animate(propImg, 100);
			
		}, function(e) {
			
			var deProp = {width: '-=12', height: '-=12', 'margin-top': '+=0', 'margin-right': '+=6'};
			var dePropImg = {width: '-=12', height: '-=12', 'margin-top': '+=0', 'margin-right': '+=0'};
			$(this).animate(deProp, 100);
			$(this).children('img').animate(dePropImg, 100);
	});
	
	$('#dockNav .left').click(scrollLeft);	
	$('#dockNav .right').click(scrollRight);
	
	updateButtons( offset, base, itemCount );
	
	}
	
//	alert(itemCount * ANIM_DISTANCE + ", " + ((itemCount * ANIM_DISTANCE) - navWidth));

	function scrollRight() {
		offset++;
		var dist = '-=' + ANIM_DISTANCE;
		$('#dock ul').animate({left: dist}, ANIM_SPEED);
			//$('#dump').text('Left Position: ' + $('#dock ul').position().left );
			updateButtons( offset, base, itemCount );
//		});
	}
	
	function scrollLeft() {
		offset--;
		var dist = '+=' + ANIM_DISTANCE;
		$('#dock ul').animate({left: dist}, ANIM_SPEED);
			//$('#dump').text('Left Position: ' + $('#dock ul').position().left );
			updateButtons( offset, base, itemCount );
//		});
	}
	
	function updateButtons(value, base, count) {
		
		$('#dockNav .right').unbind('click');
		$('#dockNav .left').unbind('click');
		
		if ((value + 8) >= count) {
			$('#dockNav .right').fadeTo(200, 0.5);
			$('#dockNav .left').bind('click', scrollLeft);
		}
		
		else if (value <= 0) {
			$('#dockNav .left').fadeTo(200, 0.5);
			$('#dockNav .right').bind('click',scrollRight);
		}
		
		else {
			$('#dockNav .left').fadeTo(200, 1);
			$('#dockNav .right').fadeTo(200, 1);
			
			$('#dockNav .left').bind('click', scrollLeft);	
			$('#dockNav .right').bind('click',scrollRight);
		}
	}
	
	/*if (!Modernizr.borderradius) {
		$('.dockThumb').corner('90px');
	}*/
});

// FEATURED BUBBLES

jQuery(document).ready(function($) {
	
	//$('.bubble .title').fadeOut(0);
	
	$('.bubble a').each(function(index, element) {
        var bg = $(this).children('img').attr('src');
		$(this).children('img').remove();		
		
		if ( bg != null && bg != 'undefined' ) {
			
			$(this).css('background', 'url(' + bg + ')');	
		}
    });
	
	/*$('.bubble').hover(
		function(e) {
			//$(this).children('.bubbleButton').addClass('hover');
			//$(this).children().children('.title').fadeIn(200);
		},
		function(e) {
			//$(this).children('.bubbleButton').removeClass('hover');	
			//$(this).children().children('.title').fadeOut(200);
		}
	);*/
	
	if ( !Modernizr.borderradius ) {
		//$('.bubble a').css('overflow', 'visible');
		$('<div id="watchLiveOverlay" />').insertAfter('#watchLive a');
		$('<div id="breakingNewsOverlay" />').insertAfter('#breakingNews a');
		$('<div id="waqtLogoOverlay" />').appendTo('#waqtLogo a');
		$('#waqtLogo a').css('overflow', 'visible');
		$('<div id="entertainmentOverlay" />').insertAfter('#entertainment a');
		$('<div id="weatherOverlay" />').insertAfter('#weather a');
		$('<div id="businessOverlay" />').insertAfter('#business a');
		$('<div id="sportsOverlay" />').insertAfter('#sports a');
	}
});

// Video Listing Cat Browser

jQuery(document).ready(function($) {
	
	var urlParams = {};
	(function () {
		var e,
			a = /\+/g,  // Regex for replacing addition symbol with a space
			r = /([^&=]+)=?([^&]*)/g,
			d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
			q = window.location.search.substring(1);
	
		while (e = r.exec(q))
		   urlParams[d(e[1])] = d(e[2]);
	})();
	
    $('#catNav').change(function(e) {
		if ( $('#catNav').val() == 'all-videos' ) {
			location.href = homeUrl + '?';
		} else {			
        	location.href = homeUrl + '/video/?category_name=' + $('#catNav').val();
		}
    });
	
	$('#catNav').val(urlParams['category_name']);
});

// Sharebar

jQuery(document).ready(function($) {
	
	var isShareSet = false;
    
	jQuery.event.add(window, "scroll", function() {
		
		if ( !isShareSet ) {
			isShareSet = true;
			$('ul#sharebar').css('margin-right', $('ul#sharebar').css('margin-left')).css('margin-left', 0);
		}
	});
});

// Shows Description SlideUp 

jQuery(document).ready(function($) {
	/*
	$('.showItem .description').each(function(index, element) {
       	
		var baseMar = $(this).css('margin-top');
		var excH = $(this).children('.excerpt').outerHeight(true);		
		
		baseMar = parseInt(baseMar.replace('px', '')); 
		
		$(this).css('margin-top', (baseMar + excH));
    });
	
	/*$('.showItem').hover(function(e) {
		
		alert($(this).css('margin-top'));
	}, function(e) {
		$(this).children('.description').animate({top: '+28px'}, 200);
		
	});
	*/
});

// News Ticker

jQuery(document).ready(function($) {
	
	var tickInt = setInterval(tickNews, 5000);
	var curIndex = 0;
	
	function tickNews() {
		
		var npos = 0;
		
		if (curIndex < 4) {
			$('div.ticker a').animate({top: '-=36px'}, 300);
			curIndex++;
		} else {
			$('div.ticker a').fadeTo(200, 0.01, function() { 
				$('div.ticker a').animate({top: '0px'}, 10, function() {
					$('div.ticker a').fadeTo(200, 1);
				});
			});
			curIndex = 0;
		}
	}
	
	$('#newsTicker').hover(function() {		
		clearInterval(tickInt);	
	}, function() {
		tickInt = setInterval(tickNews, 5000);
	});
	
	$('#navUp, #navDown').hover(function() {
		$(this).fadeTo(200, 1);	
	}, function() {
		$(this).fadeTo(200, 0.25);	
	}); 
	
	$('#navUp').click(function(){
		if (curIndex > 0) {
			$('div.ticker a').animate({top: '+=36px'}, 200);
			curIndex--;	
		}
	});
	
	$('#navDown').click(function(){
		if (curIndex < 4) {
			$('div.ticker a').animate({top: '-=36px'}, 200);
			curIndex++;	
		}
	});
});

// Most Commented Tabs

jQuery(document).ready(function($){
	
	$('#tabHeader span').css('cursor', 'pointer');
	
	$('ol#mostCommented').fadeOut(0);
	$('#mViews').addClass('selected');
	
	$('#mComments').click(function(e) {
        $(this).addClass('selected');
		$('#mViews').removeClass('selected');
		$('#mostViewed').fadeOut(200, function() {
			$('#mostCommented').fadeIn(200);
		});
    });
	
	$('#mViews').click(function(e) {
        $(this).addClass('selected');
		$('#mComments').removeClass('selected');
		$('#mostCommented').fadeOut(200, function() {
			$('#mostViewed').fadeIn(200);
		});
    });

});
