// DBCDE javascript

// Time in seconds to swap news stories
var scrollerInterval = 3;
var mainPromotionInterval = 5;
var timer =  [];
var domain = 'http://www.dbcde.gov.au';

var mobile_path = '/mobile/';
var text_path = '/text/';


$(document).ready(function() {
        $('.rounded').corners("4px");
        // This code only executable on homepage.
        if ( document.getElementById('hero-container') ) {
        	initScroller();
		fixHeight();
	}// End if
	//autoDetectMobile();
	//writeViewChangeLinks();
	
	// Javascript is enabled, remove the no-js class to allow elements of the page to display depending on js availability
	$('body').removeClass('no-js');
});

function writeViewChangeLinks() {
	var text_url = domain + text_path;
	var mobile_url = domain + mobile_path;
	if ( window.location.href.substr(0,text_url.length) == text_url ) {
		// We're in text
		//console.log('We are in text');
		url = window.location.href.replace(text_path,mobile_path);
		url = url.replace('?SQ_DESIGN_NAME=text&SQ_ACTION=set_design_name','');
		if ( url.indexOf('?') != -1 ) {
			concat = '&';
		} else {
			concat = '?';
		}
		$('#view-mobile-link a').attr('href',url + concat + 'SQ_DESIGN_NAME=mobile&SQ_ACTION=set_design_name');
		$('#view-graphic-link a').attr('href',url.replace(mobile_url,domain + '/') + concat + 'SQ_ACTION=clear_design_name');
	} else if (window.location.href.substr(0,mobile_url.length) == mobile_url) {
		// We're in mobile
		//console.log('We are in mobile');
		url = window.location.href.replace(mobile_path,text_path);
		url = url.replace('?SQ_DESIGN_NAME=mobile&SQ_ACTION=set_design_name','');
		if ( url.indexOf('?') != -1 ) {
			concat = '&';
		} else {
			concat = '?';
		}
		$('#view-text-link a').attr('href',url + concat + 'SQ_DESIGN_NAME=text&SQ_ACTION=set_design_name');
		$('#view-graphic-link a').attr('href',url.replace(text_url,domain + '/') + concat + 'SQ_ACTION=clear_design_name');
	} else {
		// We're in normal design
		//console.log('We are in normal');
		var uri = window.location.href.replace(domain +'/','');
		//console.log('page uri: %s',uri);
		
		uri = uri.replace('?SQ_ACTION=clear_design_name','');
		uri = uri.replace('&SQ_ACTION=clear_design_name','');
		if ( uri.indexOf('?') != -1 ) {
			concat = '&';
		} else {
			concat = '?';
		}
		var text_link = text_url + uri + concat + 'SQ_DESIGN_NAME=text&SQ_ACTION=set_design_name';
		var mobile_link = mobile_url + uri + concat + 'SQ_DESIGN_NAME=mobile&SQ_ACTION=set_design_name';
		
		$('#view-text-link a').attr('href',text_link);
		$('#view-mobile-link a').attr('href',mobile_link);
	}
}

function autoDetectMobile() {
	switch ($.Q.SQ_DESIGN_NAME) {
	    case 'mobile' :
		return false;
		break;
	    case 'text' :
		return false;
		break;
	    default:
	}

	// If it gets this far, the query string does not exist so we must set the design
	if ( window.location.href.indexOf(domain + '/mobile/') != -1 || window.location.href.indexOf(domain + '/text/') != -1 ){

		if ( document.getElementById('page-container') ) {

			var newUrl = window.location.href + '?SQ_DESIGN_NAME=text&SQ_ACTION=set_design_name';
			window.location = newUrl;
		}
	}
}

jQuery.extend({
    'Q' : window.location.search.length <= 1 ? {}
        : function(a){
            var i = a.length; 
            var r = /%25/g;  // Ensure '%' is properly represented 
            var h = {};      // (Safari auto-encodes '%', Firefox 1.5 does not)
            while(i--) {
                var p = a[i].split('=');
                h[ p[0] ] = r.test( p[1] ) ? decodeURIComponent( p[1] ) : p[1];
            }
            return h;
        }(window.location.search.substr(1).split('&'))
});


$(window).resize(function() {
	fixHeight();
});

function getMaxHeight(elemExpr) {
	var maxHeight = 1;
	$(elemExpr).each(function() {
		if ( $(this).height() > maxHeight ) {
			maxHeight = $(this).height();
		}
	});
	return maxHeight;
}

// Fix box heights from auto scaling
function fixHeight() {
	if ( document.getElementById("hero-container") ) {
		//$('.feature-box').css('height','auto');
		$('.feature-box-content-inner').css('height','1%');
		$('.feature-box-header').css('height','1%');
		$('.rotator-body').css('height','1%');

		//$('.feature-box').css('height',getMaxHeight('.feature-box') + "px");
		$('.feature-box-content-inner').css('height',getMaxHeight('.feature-box-content-inner') + "px");
		$('.feature-box-header').css('height',getMaxHeight('.feature-box-header') + "px");
		$('.rotator-body').css('height',getMaxHeight('.rotator-body') + "px");

		$('.feature-box-change-content-inner').css('height','1%');
		$('.feature-box-change-header').css('height','1%');

		$('.feature-box-change-content-inner').css('height',getMaxHeight('.feature-box-change-content-inner') + "px");
		$('.feature-box-change-header').css('height',getMaxHeight('.feature-box-change-header') + "px");

	}
}

// Text scroller
function initScroller() {

	$('.rotator-controls').css('display','block');
	$('.rotator-item').css('display','none');
	var media_items = 0;
	$('#media-centre .rotator-item').each(function(i) {
		if ( i == 0 ) {
			$(this).css('display','block');
		}
		media_items ++;
	});
	var news_items = 0;
	$('#news-and-events .rotator-item').each(function(i) {
		if ( i == 0 ) {
			$(this).css('display','block');
		}
		news_items ++;
	});
	var hero_items = 0;
	$('#hero-container .rotator-item').each(function(i) {
		if ( i == 0 ) {
			$(this).css('display','block');
		}
		hero_items ++;
	});
	// This next step to delete child nodes not needed one markup has been approved
	$('.rotator-controls ul li.normal-link').remove();
	$('.rotator-controls ul li.active-link').remove();

	// Start adding controls back on for media
	var media_li = $('#media-centre .previous-link');
	for ( var j = media_items; j >= 1; j -- ) {
		$(media_li).after('<li class="normal-link"><a href="javscript: void(0);" class="centre-control-' + j + '"><span class="hidden">Select media item </span><span class="number">' + j + '</span></a></li>');
		$('#media-centre .centre-control-' + j).click(function() {
			var item = $(this).children('.number').html();
			scrollerDisplaySelectedItem(item,'#media-centre','pause');
		});
		if ( j == 1 ) {
			$('#media-centre .centre-control-' + j).parent().attr('class','active-link');
		}
	}

	// Start adding controls back on for news
	var news_li = $('#news-and-events .previous-link');
	for ( var k = news_items; k >= 1; k -- ) {
		$(news_li).after('<li class="normal-link"><a href="javscript: void(0);" class="centre-control-' + k + '"><span class="hidden">Select news item </span><span class="number">' + k + '</span></a></li>');
		$('#news-and-events .centre-control-' + k).click(function() {
			var item = $(this).children('.number').html();
			scrollerDisplaySelectedItem(item,'#news-and-events','pause');
		});
		if ( k == 1 ) {
			$('#news-and-events .centre-control-' + k).parent().attr('class','active-link');
		}
	}

	// Start adding controls back on for hero articles
	var hero_li = $('#hero-container .previous-link');
	for ( var l = hero_items; l >= 1; l -- ) {
		$(hero_li).after('<li class="normal-link"><a href="javscript: void(0);" class="centre-control-' + l + '"><span class="hidden">Select feature article </span><span class="number">' + l + '</span></a></li>');
		$('#hero-container .centre-control-' + l).click(function() {
			var item = $(this).children('.number').html();
			scrollerDisplaySelectedItem(item,'#hero-container','pause');
		});
		if ( l == 1 ) {
			$('#hero-container .centre-control-' + l).parent().attr('class','active-link');
		}
	}

	$('#hero-container .previous-link').click(function() { scrollerPrevious('#hero-container',null); });
	$('#hero-container .next-link').click(function() { scrollerNext('#hero-container',null); });
	$('#hero-container .play-link').click(function() { scrollerTogglePause('#hero-container','none'); });
	$('#media-centre .previous-link').click(function() { scrollerPrevious('#media-centre',null); });
	$('#media-centre .next-link').click(function() { scrollerNext('#media-centre',null); });
	$('#media-centre .play-link').click(function() { scrollerTogglePause('#media-centre','none'); });
	$('#news-and-events .previous-link').click(function() { scrollerPrevious('#news-and-events',null); });
	$('#news-and-events .next-link').click(function() { scrollerNext('##news-and-events',null); });
	$('#news-and-events .play-link').click(function() { scrollerTogglePause('#news-and-events','none'); });

	$('.rotator-controls ul li a').attr('href','javascript: void(0);');

	//Start playing main item straight away
	scrollerTogglePause('#hero-container','play_long');
}

function scrollerNext(parentid,action) {
	var current = $(parentid + ' .active-link a .number').html();

	if ( $(parentid + ' .active-link').next().attr('class').indexOf('normal-link') != -1 ) {
		var next = Number(current)+1;
	} else {
		// Back to first
		var next = 1;
	}
	scrollerDisplaySelectedItem(next,parentid,action);
}

function scrollerPrevious(parentid,action) {
	var current = $(parentid + ' .active-link a .number').html();
	if ( $(parentid + ' .active-link').prev().attr('class').indexOf('normal-link') != -1 ) {
		var last = Number(current)-1;
	} else {
		// Back to last
		if ( $(parentid + ' .normal-link').length >= 1 ) {
			var last = $(parentid + ' .normal-link:last a .number').html();
		} else {
			var last = 1;
		}
	}
	scrollerDisplaySelectedItem(last,parentid,action);

}

function scrollerDisplaySelectedItem(itemno,parentid,action) {

	$(parentid + ' .active-link').attr('class','normal-link');
	$(parentid + ' .centre-control-' + itemno).parent().attr('class','active-link');

	var count = 1;
	$(parentid + ' .rotator-item').css('display','none');
	$(parentid + ' .rotator-item').each(function() {
		if ( count == itemno ) {
			//$(this).css('display','block');
			$(this).fadeIn("normal");
		}
		count++;
	});
	if ( action != 'none' ) {
		scrollerTogglePause(parentid,'pause');
	}
}

function scrollerTogglePause(parentid,manual) {
	if ( manual == 'none' ) {
		// Auto detect mode
		if ( $(parentid + ' .play-link').length >= 1 ) {
			manual = 'play';
if ( parentid == '#hero-container' ) {
manual = 'play_long';
}
		} else {
			manual = 'pause';
		}
	}

	if ( manual == 'pause' ) {
		clearInterval(timer[parentid]);
		$(parentid + ' .pause-link').attr('class','play-link');
	}

	if ( manual == 'play' ) {
		timer[parentid] = setInterval("scrollerNext('" + parentid + "','none')",scrollerInterval * 1000);
		$(parentid + ' .play-link').attr('class','pause-link');
	}

        if ( manual == 'play_long' ) {
		timer[parentid] = setInterval("scrollerNext('" + parentid + "','none')",mainPromotionInterval * 1000);
		$(parentid + ' .play-link').attr('class','pause-link');
        }
}