/**
 * Javascript for SBS World News
 * Jeff Lowder - 0419 350 760
 * www.different.com.au
 */
var TWN = function(){
    // private variables - only available to SBS method
    
    return {
        changeFontSize: function(divName) {
			var currentFontSize = $("p").css('font-size'); // currently targets universal <p>
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			
			if ($(divName).hasClass("increaseFont")) {
				// decrease the font size
				var newFontSize = currentFontSizeNum - 2;
			} else {
				// increase the font by default
				var newFontSize = currentFontSizeNum + 2;
			}
			$("p").css('font-size', newFontSize); // currently targets universal <p>
			$(divName).toggleClass("increaseFont");
		},
        
        printPage: function(print_type, article_id, baseUrl, currentProgram, instance) {
            /**
             * put whatever call you'd like in here, but here's a starter
             */
            w = 680;
            h = 600; 
            LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
			TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
			window.open(baseUrl+'/'+currentProgram+'/print/index/printType/'+print_type+'/i/'+instance+'/articleId/'+article_id, 'print_view', 'width=680,height=600,top='+TopPosition+',left='+LeftPosition+',resizable=yes,scrollbars=yes');
        },
        
        reportthisPage: function(article_id, baseUrl) {
            /**
             * put whatever call you'd like in here, but here's a starter
             */
             w = 675;
             h = 495;
             LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
			 TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
			window.open(baseUrl+'/reportthis/'+article_id, 'reportthis_view', 'width=675,height=495,top='+TopPosition+',left='+LeftPosition+',resizable=yes,scrollbars=auto');
        },
        
        getPollResult: function() {
            var url = 'includes/_poll.php';
            selection = $RF('poll_form', 'poll');
            var pars = 'poll=' + selection;
            var target = 'opinion_poll';
            // if a radio button has been selected - get the result
            if(selection !== null) {
                var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
            }
        },
        
        initialise: function(){
            /**
             * if theres a video container in DOM - load the videos
             */
            if($('videos')){
                $$('#videos li a').each(function(link){
                   link.onclick = function(){
                       // this.href.substring((this.href.indexOf('#') + 1)) will return the string after the # 
                       // e.g. #videos1 - it will return 'videos1'
                       TWN.loadVideos(this.rel, this.href.substring((this.href.indexOf('#') + 1)));
                   };
                });
                new Fabtabs('videos');
                TWN.loadVideos('latest','videos1');
            }
            /**
             * if theres a popular news component - set the tabs
             */
            if($('popular_news_tabs')){
                new Fabtabs('popular_news_tabs');
            }
            /**
             * if there's a photo gallery - load the ajax content and add the onclick functions
             */
            if($('photo_gallery')){
                $$('#photo_gallery li a').each(function(photo){
                    photo.onclick = function(){
                        TWN.loadPhotoContent(this.rel,this);
                    }
                });
                TWN.startCarousel('horizontal_carousel');
                TWN.loadPhotoContent('photo1');
            }
            /**
             * if there's 'Audio Stories - add the onclick for expand/collapse
             */
            if($('audio_stories')){
                $('audio_stories_control').onclick = function(){
                    TWN.accordion('audio_story_list',this);
                };
            }
            /**
             * if there's an opinion poll component - load the ajax content and add the onclick functions
             */
            if($('opinion_poll')){
                var url = 'includes/_poll.php';
                var target = $('opinion_poll');
                var myAjax = new Ajax.Updater(target, url, {
                    method: 'get',
                    onComplete: function(){
                        $$('#poll_form input[type=radio]').each(function(input){
                            input.onclick = function(){
                                TWN.getPollResult();
                            }
                        });
                    }
                });
            }
            /**
             * add the onclick events to the links in the banner/search area
             */
            $('search_type_news').onclick = function(){
                TWN.setSearchType('news');
            };
            $('search_type_sbs').onclick = function(){
                TWN.setSearchType('sbs');
            };
        }
    };
    
}();

/*
document.observe("dom:loaded", function() {
    // initialise the TWN stuff
    TWN.initialise();
});
*/

// function for the poll ajax
function $RF(el, radioGroup){
    if ($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    }
    else 
        if ($(el).tagName.toLowerCase() != 'form') {
            return false;
        }
    var checked = $(el).getInputs('radio', radioGroup).find(function(re){
        return re.checked;
    });
    return (checked) ? $F(checked) : null;
}
