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) {
    		var params = encodeURI(
    			'/dbName/' + dbName +
    			'/tableName/' + tableName +
    			'/contentId/' + contentId +
    			'/currentPage/' + currentPage +
    			'/itemsPerPage/' + itemsPerPage +
    			'/numPagesToDisplay/' + numPagesToDisplay +
    			'/container/' + container
    		);
    		
    		$('#' + container).load(baseUrl + '/comment/get-comments' + 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 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   = 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');
	
	    	// 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 +
	    		'&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);
				}
			});	
		}

    };
}();

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');
    			});
    		});
    	}
    };
}();

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) {
			var data = 
				'&pollId=' + pollId +
				'&answerId=' + answerId +
				'&divName=' + divName
			;
			$.ajax({
				type: 'POST',
				url:  baseUrl + '/polls/poll-ajax/poll/' + pollId + '/ansid/' + answerId,
				data: data, 
				success: function(msg) {
					$('#' + divName).html(msg);
				}
			});
    	}
    };
}();

$(function() {
	Utility.initSharelinks();
	Utility.initTooltips();
});
