//----------------------------------------------
// Initialize
//----------------------------------------------
$(document).ready(function(){
  //$('#carousel').jcarousel({scroll:9});
  //$("#carousel-border").show();
});

//----------------------------------------------
// SBS Object
//----------------------------------------------
var SBS={
    init:function(){
        $(".search-selector a").click(
            function(){
                $(".search-selector a").removeClass("selected");
                $(this).addClass("selected");
            });

/*
        var clbh = $("#contents-left-block").get(0).clientHeight;
        var crbh = $("#contents-right-block").get(0).clientHeight;
        alert(clbh + " " + crbh);
        var clbh = $("#contents-left-block").height();
        var crbh = $("#contents-right-block").height();
        if(clbh > crbh) {
            $("#contents-right-block").height(clbh);
        }
*/

    }
};

//----------------------------------------------
// Misc. Functions
//----------------------------------------------
function popupVideo(url) {
	window.open(url,'video','height=661,width=951,top=100,left=100,scrollbars=no,toolbars=no');
}

function popupGeneric(url, width, height) {
	window.open(url,'sbspopup','height=' + height + ',width=' + width + ',top=100,left=100,scrollbars=no,toolbars=no');
}

function popupSrollable(url, width, height) {
	window.open(url,'sbspopup','height=' + height + ',width=' + width + ',top=100,left=100,scrollbars=1,toolbars=0,location=0');
}


/**
 * handles validation of comment forms
 * and submits the user-entered data
 * to a php script through ajax
 */
function makeComment(divName, useDivName) 
{
	var errorText = '';
	var formError = false;
	
	// we use this if we have multiple forms on one page
	// to make the fields unique to each form
	var prefix = '';
	if (useDivName) {
		prefix = divName + '_';
	}
	
	var parent_id = document.getElementById(prefix + 'parent_id').value;
	var type = document.getElementById(prefix + 'type').value;
	var headline = document.getElementById(prefix + 'headline').value;
	
	var userName = document.getElementById(prefix + 'txt_name').value;
	var userLocation = document.getElementById(prefix + 'txt_location').value;
	var title = encodeURIComponent(document.getElementById(prefix + 'txt_title').value);
	var url = encodeURIComponent(document.getElementById(prefix + 'txt_website').value);
	var comment = encodeURIComponent(document.getElementById(prefix + 'txt_comment').value);
	
	var private_key = document.getElementById(prefix + 'private_key').value;
	var public_key = document.getElementById(prefix + 'public_key').value;
	

	// do validation
	if(userName == "") {
		 errorText = errorText + "Please enter your name\n";
		 formError = true;
	}
	if(userLocation == "") {
		 errorText = errorText + "Please enter your suburb\n";
		 formError = true;
	}
	if(title == "") {
		 errorText = errorText + "Please enter a title or subject\n";
		 formError = true;
	}
	if(comment == "") {
		 errorText = errorText + "Please enter your comment\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 = 
		'name=' + userName +
		'&location=' + userLocation +
		'&title=' + title +
		'&message=' + comment +
		'&public_key=' + public_key +
		'&private_key=' + private_key +
		'&parent_id=' + parent_id +
		'&type=' + type + 
		'&url=' + url + 
		'&headline=' + headline
	;

	// send the url via ajax to a php script
	// and refresh content after success
	jQuery.ajax({
		type: 'POST',
		url: baseUrl + '/ajax/make-comment/',
		data: data,
		success: function(msg) {
			$('#' + divName).html(msg);
		}

	});
}

/**
  * used to get comments and refresh  when using ajax pagination for the comments section
  * not really used in dateline...
  */
function getComments(article_id, article_type, page, pagelimit, divName, commentCount)
{
	var data = encodeURI(
		'article_id=' + article_id +
		'&article_type=' + article_type +
		'&page=' + page +
		'&pagelimit=' + pagelimit +
		'&commentCount=' + commentCount
	);
	
	// send the url via ajax to a php script
	// and refresh content after success
	jQuery.ajax({
		type: 'POST',
		url: baseUrl + '/ajax/get-comments/',
		data: data,
		success: function(msg) {
			$('#' + divName).html(msg);
			//recordStats(siteUrl + 'submit_comment/article_id/' + article_id);
		}
	});	
}

/**
 * sets the agree/disagree vote for comments
 */
function makeCommentOpinion(divName, comment_id, opinion, type) 
{
	var data = 
		'comment_id=' + comment_id +
		'&opinion=' + opinion +
		'&type=' + type
	;
	
	// send the url via ajax to a php script
	// and refresh content after success
	jQuery.ajax({
		type: 'POST',
		url: baseUrl + '/ajax/make-comment-opinion',
		data: data, 
		success: function(msg) {
			$('#' + divName).html(msg);
		}
	});
}

/**
 * handles validation of link submitting form
 * and submits the user-entered data
 * to a php script through ajax (for FF)
 */
function submitALinkFF(divName) 
{
	var browserName=navigator.appName; 
	
	if (browserName=="Microsoft Internet Explorer"){
		// do nothing!
	}else{
		var errorText = '';
		var formError = false;
		
		var type = document.getElementById('type').value;
		var id = document.getElementById('id').value;
		var link = encodeURIComponent(document.getElementById('link').value);
		var title = encodeURIComponent(document.getElementById('title').value);
		
		// do validation
		if(link == "") {
			 errorText = errorText + "Please enter the URL\n";
			 formError = true;
		}
		if(title == "") {
			 errorText = errorText + "Please enter a title for the link\n";
			 formError = true;
		}
	
		if(formError) {
			window.alert(errorText);
			return;
		};
		
		// set up the post variables
		var data = encodeURI(
			'id=' + id +
			'&type=' + type +
			'&link=' + link +
			'&title=' + title 
		);
		
		// send the url via ajax to a php script
		// and refresh content after success
		jQuery.ajax({
			type: 'POST',
			url: baseUrl + '/ajax/submitlink/',
			data: data,
			success: function(msg) {
				$('#' + divName).html(msg);
			}
		});
	}
}

/**
 * handles validation of link submitting form
 * and submits the user-entered data
 * to a php script through ajax (for IE)
 */
function submitALinkIE(divName) 
{
	
	var browserName=navigator.appName; 
	
	if (browserName=="Microsoft Internet Explorer"){
		var errorText = '';
		var formError = false;
		
		var type = document.getElementById('type').value;
		var id = document.getElementById('id').value;
		var link = encodeURIComponent(document.getElementById('link').value);
		var title = encodeURIComponent(document.getElementById('title').value);
		
		// do validation
		if(link == "") {
			 errorText = errorText + "Please enter the URL\n";
			 formError = true;
		}
		if(title == "") {
			 errorText = errorText + "Please enter a title for the link\n";
			 formError = true;
		}
	
		if(formError) {
			window.alert(errorText);
			return;
		};
		
		// set up the post variables
		var data = encodeURI(
			'id=' + id +
			'&type=' + type +
			'&link=' + link +
			'&title=' + title 
		);
		
		// send the url via ajax to a php script
		// and refresh content after success
		jQuery.ajax({
			type: 'POST',
			url: baseUrl + '/ajax/submitlink/',
			data: data,
			success: function(msg) {
				$('#' + divName).html(msg);
			}
		});
	}
}


/**
 * limits the number of characters allowed to 
 * for a message in the comment form
 */
function limitText(limitField, limitCount, limitNum) 
{
	if (limitField.value.length > limitNum) {
		 limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}