
var Article = function() {
    return {
		createBookmark: function() {
			title = document.title;
			url = document.location.href; 
			
			if (window.sidebar) { // Mozilla Firefox Bookmark
				window.sidebar.addPanel(title, url,"");
			} else if( window.external ) { // IE Favorite
				window.external.AddFavorite( url, title); 
			} else if(window.opera && window.print) { // Opera Hotlist
				return true; 
			}
		},
		
        changeFontSize: function(link, container) {
			var currentFontSize = $(container).css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			
			if ($(link).hasClass('setLargeFont')) {
				// decrease the font size
				var newFontSize = currentFontSizeNum - 2;
			} else {
				// increase the font by default
				var newFontSize = currentFontSizeNum + 2;
			}
			$(container).css('font-size', newFontSize);
			$(link).toggleClass('setLargeFont');
		},

        print: function(print_type, content_id) {
            width  = 680;
            height = 600; 
            LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
			TopPosition  = (screen.height) ? (screen.height-height)/2 : 0;
			window.open(baseUrl + '/print/index/printType/' + print_type + '/id/' + content_id, 'print_view', 'width=' + width + ',height=' + height + ',top=' + TopPosition + ',left=' + LeftPosition + ',resizable=yes,scrollbars=yes');
        },
        
        sendToFriend: function() {
            width  = 500;
            height = 545; 
            LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
			TopPosition  = (screen.height) ? (screen.height-height)/2 : 0;
			window.open(baseUrl + '/sendemail/displayform/', 'send_to_friend', 'width=' + width + ',height=' + height + ',top=' + TopPosition + ',left=' + LeftPosition + ',resizable=yes,scrollbars=yes');
        }
    };
}();

var Popup = function() {
	defaultWidth = 680;
	defaultHeight = 600;

    return {
    	video: function(url, width, height) {
	    	LeftPosition = (screen.width) ? (screen.width-defaultWidth)/2 : 0;
	        TopPosition  = (screen.height) ? (screen.height-defaultHeight)/2 : 0;
	
    		if (width == null || height == null) {
    			width  = 661;
    			height = 951;
    		}
			window.open(url, 'popup_video', 'width=' + width + ',height=' + height + ',top=' + TopPosition + ',left=' + LeftPosition + ',resizable=yes,scrollbars=no,toolbars=no');
		},

    	generic: function(url, width, height) {
	    	LeftPosition = (screen.width) ? (screen.width-defaultWidth)/2 : 0;
	        TopPosition  = (screen.height) ? (screen.height-defaultHeight)/2 : 0;

    		if (width == null || height == null) {
    			width  = defaultWidth;
    			height = defaultHeight;
    		}
    	
			window.open(url, 'popup_generic', 'width=' + width + ',height=' + height + ',top=' + TopPosition + ',left=' + LeftPosition + ',resizable=yes,scrollbars=no,toolbars=no');
    	},
    	
    	scrollable: function(url, width, height) {
	    	LeftPosition = (screen.width) ? (screen.width-defaultWidth)/2 : 0;
	        TopPosition  = (screen.height) ? (screen.height-defaultHeight)/2 : 0;

    		if (width == null || height == null) {
    			width  = defaultWidth;
    			height = defaultHeight;
    		}
    	
			window.open(url, 'popup_scrollable', 'width=' + width + ',height=' + height + ',top=' + TopPosition + ',left=' + LeftPosition + ',resizable=yes,scrollbars=yes,toolbars=no');
    	}
    };
}();

var Comments = function() {
    return {
    	display: function(dbName, tableName, contentId, itemsPerPage, numPagesToDisplay, currentPage, baseUrl, container, filter, pageType) {
    		/*
    		var params = encodeURI(
    			'/dbName/' + dbName +
    			'/tableName/' + tableName +
    			'/contentId/' + contentId +
    			'/currentPage/' + currentPage +
    			'/itemsPerPage/' + itemsPerPage +
    			'/numPagesToDisplay/' + numPagesToDisplay +
    			'/container/' + container +
    			'/filter/' + filter
    		);
    		
    		$('#' + container).load(baseUrl + '/comment/get-comments' + params);
    		*/
    	
			var params = 
    			'dbName=' + dbName +
    			'&tableName=' + tableName +
    			'&contentId=' + contentId +
    			'&currentPage=' + currentPage +
    			'&itemsPerPage=' + itemsPerPage +
    			'&numPagesToDisplay=' + numPagesToDisplay +
    			'&container=' + container +
    			'&filter=' + filter +
    			'&pageType=' + pageType
			;

			$.ajax({
				type: 'GET',
				url: baseUrl + '/comment/get-comments',
				data: params, 
				beforeSend: function(){
					//$('#' + container).html('<div style="height: 300px; line-height: 300px; text-align: center"><img style="margin-top: 150px;" src="' + baseUrl + '/web/images/ajax-loader.gif" alt="loading data..." /></div>');
					$('#' + container).html('<div style="width: 640px; height: 300px; text-align: center;"><div style="margin: 0 auto; padding-top: 100px;"><img src="' + baseUrl + '/web/images/ajax-loader.gif" alt="loading data..." /><p>Loading... Please wait...</p></div></div>');
				},
				success: function(msg) {
					$('#' + container).html(msg);
					
					// first, remove all filter selected classes
					$('.filter_bar p').removeClass('selected');
					$('#filter_' + filter).addClass('selected');
				}
			});
    		
    	},
    	
    	displayFooter: function(dbName, tableName, contentId, itemsPerPage, numPagesToDisplay, currentPage, baseUrl, container, filter) {
    		var params = encodeURI(
    			'/dbName/' + dbName +
    			'/tableName/' + tableName +
    			'/contentId/' + contentId +
    			'/currentPage/' + currentPage +
    			'/itemsPerPage/' + itemsPerPage +
    			'/numPagesToDisplay/' + numPagesToDisplay +
    			'/container/' + container +
    			'/filter/' + filter
    		);
    		
    		$('#' + container).load(baseUrl + '/comment/get-comments-footer' + params);
    	},
    	
		submitOpinion: function(commentId, opinion, dbName, tableName, baseUrl, container) {
			var data = 
				'commentId=' + commentId +
				'&opinion=' + opinion +
				'&dbName=' + dbName +
				'&tableName=' + tableName +
				'&container=' + container
			;
			
			$.ajax({
				type: 'POST',
				url: baseUrl + '/comment/make-comment-opinion',
				data: data, 
				success: function(msg) {
					$('#' + container).html(msg);
				}
			});
		},
    	
		submit: function() {
	    	var errorText = '';
	    	var formError = false;
	
	    	var contentId = $('#contentId').attr('value');
	    	var parentId  = $('#parentId').attr('value');
	    	var dbName    = $('#dbName').attr('value');
	    	var tableName = $('#tableName').attr('value');
	    	
	    	var formContainer = $('#formContainer').attr('value');
	    	var refererUrl    = $('#refererUrl').attr('value');
	    	
	    	var memberId     = $('#txt_member_id').attr('value');
	    	
	    	var userName     = $('#txt_name').attr('value');
	    	var userLocation = $('#txt_location').attr('value');
	    	if (userLocation == 'undefined') {
	    		userLocation = '';
	    	}
	    	
	    	var title   = encodeURIComponent($('#txt_title').attr('value'));
	    	if (title == 'undefined') {
	    		title = '';
	    	}
	    	var url     = encodeURIComponent($('#txt_website').attr('value'));
	    	var active  = $('#active').attr('value');
	    	
	    	var private_key = $('#com_private_key').attr('value');
	    	var public_key  = $('#public_key').attr('value');
	
	    	if(parentId>0){
	    		var contentTitle = 'txt_content_' + parentId;
	    	}else{
	    		var contentTitle = 'txt_content';
	    	}
			
			var content = encodeURIComponent($('#' + contentTitle).attr('value'));
			
	    	// do validation
	    	if (userName == "") {
	    		 errorText = errorText + "Please enter your name\n";
	    		 formError = true;
	    	}
	    	if (content == "") {
	    		 errorText = errorText + "Please enter your message\n";
	    		 formError = true;
	    	}
	    	if (private_key == "") {
	    		 errorText = errorText + "Please enter the validation number\n";
	    		 formError = true;
	    	}
	
	    	if(formError) {
	    		window.alert(errorText);
	    		return;
	    	};
	    	
	    	// set up the post variables
	    	var data =
	    		'contentId=' + contentId +
	    		'&parentId=' + parentId +
	    		'&memberId=' + memberId +
	    		'&name=' + userName +
	    		'&location=' + userLocation +
	    		'&title=' + title +
	    		'&website=' + url +
	    		'&content=' + content +
	    		'&active=' + active +
	    		'&public_key=' + public_key +
	    		'&private_key=' + private_key +
	    		'&dbName=' + dbName + 
	    		'&tableName=' + tableName + 
	    		'&refererUrl=' + refererUrl + 
	    		'&formContainer=' + formContainer
	    	;
	
	    	$.ajax({
	    		type: 'POST',
	    		url: baseUrl + '/comment/make-comment/',
	    		data: data,
	    		success: function(msg) {
	    			$('#' + formContainer).html(msg);
	    		}
	
	    	});
		},
		
		getFilledCommentForm: function() {
			var contentId = $('#contentId').attr('value');
			var dbName    = $('#dbName').attr('value');
			var tableName = $('#tableName').attr('value');
			
			var formContainer = $('#formContainer').attr('value');
			var refererUrl    = $('#refererUrl').attr('value');
			
			var userName     = $('#txt_name').attr('value');
			var userLocation = $('#txt_location').attr('value');
			
			var title   = $('#txt_title').attr('value');
			var url     = $('#txt_website').attr('value');
			var content = $('#txt_content').attr('value');
			var active  = $('#active').attr('value');

			var data = encodeURI(
				'contentId=' + contentId +
				'&dbName=' + dbName +
				'&tableName=' + tableName +
				'&formContainer=' + formContainer +
				'&active=' + active +
				'&name=' + userName +
				'&location=' + userLocation +
				'&title=' + title +
				'&website=' + url +
				'&content=' + content + 
				'&refererUrl=' + refererUrl
			);
			
			$.ajax({
				type: 'POST',
				url: baseUrl + '/comment/get-filled-comment-form/',
				data: data,
				success: function(msg) {
					$('#' + formContainer).html(msg);
				}
			});	
		},
		
    	getReplyForm: function(dbName, tableName, contentId, parentId, baseUrl, container) {
    		var params = encodeURI(
    			'/dbName/' + dbName +
    			'/tableName/' + tableName +
    			'/contentId/' + contentId +
    			'/parentId/' + parentId +
    			'/container/' + container
    		);
    		
    		$('#' + container).load(baseUrl + '/comment/get-reply-comment-form' + params);
    		$('#' + container).show();
    	},

    	getMemberName: function(name) {
    		var memberName = '0';
    		var memberLocation = '';
    		var nameEQ = "a_tt=";
    		var ca = document.cookie.split(';');
    		for(var i=0;i < ca.length;i++) {
    			var c = ca[i];
    			while (c.charAt(0)==' ') c = c.substring(1,c.length);
    			if (c.indexOf(nameEQ) == 0){
    				// is logged in
    	    		nameEQ = name + "=";
    	    		ca = document.cookie.split(';');
    	    		for(var i=0;i < ca.length;i++) {
    	    			c = ca[i];
    	    			while (c.charAt(0)==' ') c = c.substring(1,c.length);
    	    			if (c.indexOf(nameEQ) == 0){
    	    				memberHash = c.substring(nameEQ.length,c.length);
    	    				memberHash = Base64.decode(memberHash);
    	    				var memberDetails=memberHash.split('|');
    	    				memberName = memberDetails[0]; 
    	    				memberLocation = memberDetails[1]; 
    	    				memberAvatar = memberDetails[2];
    	    			}
    	    		}
    			}
    		}
    		if(memberName=='0'){
   				// do nothing
    		}else{
    			if((memberAvatar == '')||(memberAvatar == ' ')){
    				var avatarString = '&nbsp;';
    			}else{
    				var avatarString = '<img src="https://www.sbs.com.au/signon/resize/file/' + memberAvatar + '/w/16/h/16/" />';
    			}
    			var htmlString = '<p class="f16 b pb5">' + memberName +  '</p><table>' + 
				'<tr class="r1"><td class="c1">' + avatarString + '</td>' + 
				'<td class="c2"><a class="f11" href="/memberservices/profile/t/1" title="Profile Settings">Profile Settings</a><div class="clear_small"></div>' +
				'<a class="f11" href="/memberservices/profile/t/2" title="Account Settings">Account Settings</a><div class="clear_small"></div>' +
				'<a class="f11" href="/memberservices/profile/t/3" title="Preferences">Preferences</a><div class="clear_small"></div>' +
				'<p class="mt0 f11 b">Not ' + memberName + '?&nbsp;&nbsp;<a class="logout" href="/memberservices/logout" title="Logout"><b>Logout</b></a></p><div class="clear_small"></div>' +
				'<td></tr></table>';
    			// add the member's name in and change the attributes of the fields
    			$('#txt_name').attr('value',memberName).attr('readonly',true).addClass('readonly');
    			$('#txt_location').attr('value',memberLocation).attr('readonly',true).addClass('readonly');
    			$('#txt_member_id').attr('value','1');
    			$('div.user_profile').html(htmlString); 
    		}
    	},
    	
    	backOfTheNetLink: function() {
    		var nameEQ = "a_tt=";
    		var ca = document.cookie.split(';');
    		for(var i=0;i < ca.length;i++) {
    			var c = ca[i];
    			while (c.charAt(0)==' ') c = c.substring(1,c.length);
    			if (c.indexOf(nameEQ) == 0){
    				// is logged in
        			var htmlString = '<a rel="iframe" class="show_overlay botn_sm lh18" href="/backofthenet/overlay" title="Add Your Video">' +
        				'Add Your Video<span style="background-color:#7e9a3e !important;"></span></a>' ;
        			// change the field over
        			$('div#backofthenet_replace').html(htmlString); 
    			}
    		}
    	},
		
		reportThis: function(commentId, contentId, tableName, formContainer) {
    		/*
	    	errorText = "CommentId: " + commentId + "\n";
	    	errorText = errorText + "ContentId: " + contentId + "\n";
	    	errorText = errorText + "TableName: " + tableName + "\n";
	    	alert(errorText);
			*/
	    	
	    	// set up the post variables
	    	var data =
	    		'commentId=' + commentId +
	    		'&contentId=' + contentId +
	    		'&tableName=' + tableName + 
	    		'&formContainer=' + formContainer
	    	;
	
	    	$.ajax({
	    		type: 'POST',
	    		url: baseUrl + '/comment/report-comment/',
	    		data: data,
	    		success: function(msg) {
	    			$('#' + formContainer).html(msg);
	    		}
	
	    	});
		}

    };
}();

var Gallery = function() {
    return {
    	getGallery: function(galleryId, albumId, galleryType, galleryName, divName, baseUrl) {
			var data = 
				'galleryId=' + galleryId +
				'albumId=' + albumId +
				'&galleryType=' + galleryType +
				'&galleryName=' + galleryName +
				'&divName=' + divName
			;
			
			$.ajax({
				type: 'POST',
				url: baseUrl + '/slideshow/getgallery' + 
				'/galleryId/' + galleryId +
				'/albumId/' + albumId +
				'/galleryType/' + galleryType +
				'/galleryName/' + galleryName,
				data: data, 
				success: function(msg) {
					$('#' + divName).html(msg);
				}
			});
    	}
    };
}();

var Poll = function() {
    return {
    	voteAction: function(pollId, answerId, divName, baseUrl, template, action) { 
			var data = 
				'&pollId=' + pollId +
				'&answerId=' + answerId +
				'&divName=' + divName +
				'&template=' + template
			;
			$.ajax({
				type: 'POST',
				url:  baseUrl + '/polls/' + action + '/poll/' + pollId + '/ansid/' + answerId + '/template/' + template,
				data: data, 
				success: function(msg) {
					$('#' + divName).html(msg);
				}
			});
    	}
    };
}();

function getPolls(divName, currentPage)
{
	// set up the post variables
	var data =
		'divName=' + divName +
		'&currentPage=' + currentPage 
	;
	$.ajax({
		type: 'POST',
		url: baseUrl + '/polls/poll-list/',
		data: data,
		success: function(msg) {
			$('#' + divName).html(msg);
		}
	});
}

function submitRating(divName, currentTab, title, artist, rating, originatorId, station)
{
	// set up the post variables
	var data =
		'divName=' + divName +
		'&currentTab=' + currentTab +
		'&title=' + title +
		'&artist=' + artist +
		'&rating=' + rating +
		'&originatorId=' + originatorId +
		'&station=' + station 
	;
	$.ajax({
		type: 'POST',
		url: baseUrl + '/rating/submit/',
		data: data,
		success: function(msg) {
			$('#' + divName + ' p.thanks').html(msg);
		}
	});
}

function refreshTrackInfo(section, tab, time, popup, shows)
{
	// set up the post variables
	var data =
		'section=' + section +
		'&tab=' + tab +
		'&time=' + time +
		'&popup=' + popup +
		'&shows=' + shows
	;
	$.ajax({
		type: 'POST',
		url: baseUrl + '/live/refresh/',
		data: data,
		success: function(msg) {
			if (section == 'image_rating') {
				$('#' + tab + '_' + section).html(msg);
				$('#' + tab + '_' + section + ' img').animate({ opacity: 0.01, left: '-=102px',height: 'toggle'},400).animate({opacity: 1,left: '+=102',height: 'toggle'}, 600);
			}
			else {
				$('#' + tab + '_' + section).hide().html(msg).fadeIn(1000);
			}
		}
	});
}
/* moved to global_menu.js
function getYourLanguageUrl(selectName) {}
*/
