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, genre) {
    		/*
    		var params = encodeURI(
    			'/dbName/' + dbName +
    			'/tableName/' + tableName +
    			'/contentId/' + contentId +
    			'/currentPage/' + currentPage +
    			'/itemsPerPage/' + itemsPerPage +
    			'/numPagesToDisplay/' + numPagesToDisplay +
    			'/container/' + container +
    			'/filter/' + filter +
    			'/genre/' + genre
    		);
    		
    		$('#' + 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 +
    			'&genre=' + genre
			;

			$.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);
    	},
    	
    	/*
    	showMore: function(dbName, tableName, contentId, itemsPerPage, numPagesToDisplay, currentPage, baseUrl, container, filter, previousCommentCount) {
			// first, get and append the next set of comments and then update the commentlist footer
			Comments.display(dbName, tableName, contentId, itemsPerPage, numPagesToDisplay, currentPage, baseUrl, container, filter);
			Comments.displayFooter(dbName, tableName, contentId, itemsPerPage, numPagesToDisplay, currentPage, baseUrl, 'comment_footer', filter, previousCommentCount);
    	},
    	*/
    	
		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 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 content = encodeURIComponent($('#txt_content').attr('value'));
	    	var active  = $('#active').attr('value');
	    	
	    	var private_key = $('#com_private_key').attr('value');
	    	var public_key  = $('#public_key').attr('value');
	    	var privatedb_key  = $('#private_key').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;
	    	} else if (private_key != privatedb_key && private_key != privatedb_key.toUpperCase()) {
	    		 errorText = errorText + "Sorry, code does not match\n";
	    		 formError = true;
	    	}
	
	    	if(formError) {
	    		window.alert(errorText);
	    		return;
	    	};
	    	
	    	document.getElementById('btn_submitcomment').value = "Submitting";
	    	document.getElementById('btn_submitcomment').disabled = true;
	    	
	    	// set up the post variables
	    	var data =
	    		'contentId=' + contentId +
	    		'&parentId=' + parentId +
	    		'&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();
    	},
		
		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 Utility = function() {
    return {
    	initSharelinks: function() {
	    	$('#sharecontent').hide(); //container
	    	$('a#sharelink').toggle(   //link + click functionality 
	    		function () { 
	    			$('#sharecontent').animate({
	    				opacity: 'toggle',
	    				height: 'toggle'
	    			}, 'slow'); 
	    			return false;
	    		},
	    		function () {
	    			$('#sharecontent').animate({opacity: 'toggle', height: 'toggle'}, 'normal'); 
	    			return false;
	    		}
	    	);
    	},
    	
    	initTooltips: function() {
    		$('.tooltip-target').each(function(i, obj){
    			var id = obj.id;
    			var name = id.split('-')[0];
    			var rid = '#' +  name + '-section-' + id.split('-')[2];

    			$(obj).mouseover(function() {
    				$(rid).css('display', 'block');
    			});

    			$(obj).mouseout(function() {
    				$(rid).css('display', 'none');
    			});
    		});
    	},
    	
    	limitText: function(limitField, limitCount, limitNum) {
			if (limitField.value.length > limitNum) {
				 limitField.value = limitField.value.substring(0, limitNum);
			} else {
				limitCount.value = limitNum - limitField.value.length;
			}
		}
    };
}();

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() {
	Utility.initSharelinks();
	Utility.initTooltips();
});

function limitChars(textid, limit, infodiv)
{
	//alert (textid);
	var text = $('#'+textid).val();
	//alert (text);
	var textlength = text.length;
	//alert (textlength);
	if(textlength > limit)
	{
		$('#' + infodiv).html(limit+' characters reached!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('You have <span>'+(limit - textlength) +'</span> / <b>500</b> characters remaining');
		return true;
	}
}

var Contactus = function() {
    return {    	
		submit: function() {
	    	var errorText = '';
	    	var formError = false;
	    	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	
	    	var dbName    = $('#dbName').attr('value');
	    	var formContainer = $('#formContainer').attr('value');
	    	
	    	var userName = $('#txt_name').attr('value');
	    	var email 	 = $('#txt_email').attr('value');
	    	if (email == 'undefined') {
	    		email = '';
	    	}
	    	
	    	var content = encodeURIComponent($('#txt_content').attr('value'));
	    	
	    	var private_key = $('#com_private_key').attr('value');
	    	var public_key  = $('#public_key').attr('value');
	    	var privatedb_key  = $('#private_key').attr('value');
	
	    	// do validation
	    	if (userName == "") {
	    		 errorText = errorText + "Please enter your name\n";
	    		 formError = true;
	    	}
	    	if (email == "") {
	    		 errorText = errorText + "Please enter your email\n";
	    		 formError = true;
	    	} else {
	        	if($('#txt_email').val().match(emailExp)){
	        		formError = false;
	        	} else {
	        		errorText = errorText + "Please enter a valid email address\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;
	    	} else if (private_key != privatedb_key && private_key != privatedb_key.toUpperCase()) {
	    		 errorText = errorText + "Sorry, code does not match\n";
	    		 formError = true;
	    	}
	
	    	if(formError) {
	    		window.alert(errorText);
	    		return;
	    	};
	    	
	    	document.getElementById('btn_submitcontact').value = "Submitting";
	    	document.getElementById('btn_submitcontact').disabled = true;
	    	
	    	// set up the post variables
	    	var data =
	    		'&name=' + userName +
	    		'&email=' + email +
	    		'&content=' + content +
	    		'&public_key=' + public_key +
	    		'&private_key=' + private_key +
	    		'&dbName=' + dbName + 
	    		'&formContainer=' + formContainer
	    	;
	
	    	$.ajax({
	    		type: 'POST',
	    		url: baseUrl + '/contactus/make-email/',
	    		data: data,
	    		success: function(msg) {
	    			$('#' + formContainer).html(msg);
	    		}
	
	    	});
		}
    };
}();

var Subscribe = function() {
    return {    	
		submit: function() {
    		var focus = null;
	    	var errorText = '';
	    	var formError = false;
	    	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	
	    	// we use the .blanked class here to ensure an attempt has been made to provide a value
	    	var forename = $('#nl_forename.blanked').attr('value');
	    	var surname = $('#nl_surname.blanked').attr('value');
	    	var email 	 = $('#nl_email.blanked').attr('value');
	    	
	    	// do validation
	    	if (forename == "" || forename == undefined) {
	    		 errorText = errorText + "Please enter your first name(s)\n";
	    		 if(!focus) focus = '#nl_forename';
	    		 formError = true;
	    	}
	    	if (surname == "" || surname == undefined) {
	    		 errorText = errorText + "Please enter your surname\n";
	    		 if(!focus) focus = '#nl_surname';
	    		 formError = true;
	    	}
	    	if (email == "" || email == undefined) {
	    		 errorText = errorText + "Please enter your email\n";
	    		 if(!focus) focus = '#nl_email';
	    		 formError = true;
	    	} else if(!email.match(emailExp)){
        		errorText = errorText + "Please enter a valid email address\n";
        		if(!focus) focus = '#nl_email';
             	formError = true;
	        }
	
	    	if(formError) {
	    		window.alert(errorText);
	    		if(focus) $(focus).focus();
	    		return;
	    	};
	    	
	    	if(document.getElementById('btn_submit') != null) { 
	    		document.getElementById('btn_submit').value = "Submitting";
	    		document.getElementById('btn_submit').disabled = true;
	    	}
	    	
	    	if(document.getElementById('img_submit') != null) { 
	    		document.getElementById('img_submit').src = "/web/images/btn/submit_working.png";
	    	}
	    	
	    	// set up the post variables
	    	var data =
	    		'forename=' + forename +
	    		'&surname=' + surname +
	    		'&email=' + email
	    	;
	
	    	$.ajax({
	    		type: 'POST',
	    		url: baseUrl + '/contactus/newsletter-subscribe/',
	    		data: data,
	    		success: function(msg) {
	    			$('.newsletter-container').html(msg);
	    		}
	
	    	});
		}
    };
}();