// Define a global namespace object
var Rowdii = window.Rowdii || {};

var baseUrl = "http://www.rowdii.com";
var imgBaseUrl = baseUrl + "/library/img";

//-----------------------------------------------------------------
function getQuote()
{
	var txt = '';
	if( document.selection )
	{ 	
		txt = document.selection.createRange().text;
	}
	else if( document.getSelection() != '' )
	{ 
		txt = document.getSelection();
	}
	return txt;
}

$(document).ready(function() {
	$('.clearInput').hover(
		function() {
			var keyword = ['websitename.com', 'Enter email address', 'Enter password'];
			if( jQuery.inArray($(this).val(), keyword) > -1 )
				$(this).attr( 'value', '' );
		},
		function() {
			var keyword = ['websitename.com', 'Enter email address', 'Enter password'];
			var classes = ['searchInput', 'signInEmail', 'signInPassword'];
			for( i=0; i<classes.length; i++ )
			{
				if( $(this).attr( 'class' ).indexOf( classes[i] ) > -1 && $(this).val() == '' )
				{
					$(this).attr( 'value', keyword[i] );
					i=classes.length;
				}
			}
		}
	);
});
	
/********************************************************************
 Class: Forum
*********************************************************************/
Rowdii.Forum = function()
{	
	//-----------------------------------------------------------------
	function exitFrameSearchBtn(e)
	{
		if (parent.frames.length > 0) {
			parent.location.href = $('#searchPath').attr( 'value' ) + $('.searchInput').attr( 'value' );
		}
		else
		{
			location.href = $('#searchPath').attr( 'value' ) + $('.searchInput').attr( 'value' );
		}
	}
	
	//-----------------------------------------------------------------
	function exitFrameLink(e)
	{
		if (parent.frames.length > 0) {
			$(this).attr( 'target', '_parent' )
		}
	}
	
	//-----------------------------------------------------------------
	function appendReturnUrl(e)
	{
		if (parent.frames.length > 0) {
			var el = $(this);
			var orgUrl = el.attr( 'href' );
			el.attr( 'href', orgUrl + "?ReturnUrl=" + escape(parent.location.href) );
		}
	}
	
	//----------------------------------------------------------------
	function updateCurrentPageUrl(e)
	{
		var newUrl = escape( $(this).attr( 'href' ) );
		$('.hasCurrUrl').each(function()
			{ 
				var oldUrl = $(this).attr( 'href' );
				var path = oldUrl.substring( 0, oldUrl.indexOf( "?url=" ) + 5 );
				$(this).attr( 'href', path + newUrl );
			});
			
		$('.urlInfo').attr( 'readonly', '' );
		$('.urlInfo').val( $(this).attr( 'href' ) );
		$('.urlInfo').attr( 'readonly', 'readonly' );
	}
	
	//------------------------------------------------------------------
	function showHideAddLinkedSiteForm(e)
	{
		e.preventDefault();
		e.stopPropagation();
	
		var currentStyle = $('#addLinkedSiteLinkWrapper').attr( "style" );
		if( currentStyle.toLowerCase().indexOf( "display: none" ) > -1 || currentStyle.toLowerCase().indexOf( "display:none" ) > -1 )
		{
			$('#addLinkedSiteLinkWrapper').hide().slideDown();
			return;
		}
		$('#addLinkedSiteLinkWrapper').show().slideUp();
	}
	
	//------------------------------------------------------------------
	function showAddPostForm(e)
	{
		//e.preventDefault();
		e.stopPropagation();
		
		var type = "reply";
		var postId = $(this).attr("id").replace('replyLink-', '');
		if( isNaN( postId ) )
		{
			postId = $(this).attr("id").replace('quoteLink-', '');
			type = "replyWithQuote";
		}
		
		if(type == 'replyWithQuote') {
			var quote = getQuote() != "" ? "[quote]" + getQuote() + "[/quote]" : "";
			var msg = $('.messageInput').val();
			$('.messageInput').val( msg + quote );			
		}
		else {
			$('#quoteField').hide();
		}
		//$('#addPostWrapper').hide().insertAfter( $('#formSpace-' + postId) ).slideDown();
		$('#addPostWrapper').hide().slideDown();
	}
	
	//-----------------------------------------------------------------
	function displayPostWrapper(e)
	{	
		$('#addPostWrapper').hide().slideDown();
		$('.urlInfo').attr( 'readonly', 'readonly' );
	}
	
	return {
		//--------------------------------------------------------------
		initExitFrame : function()
		{
			$('.searchButton').bind( 'click', exitFrameSearchBtn );
			$('.site' ).bind( 'click', exitFrameLink );
		},
		
		initAppendReturnUrl: function()
		{
			$('#signin').bind( 'click', appendReturnUrl );
			$('.appendReturnUrl').bind('click', appendReturnUrl )
		},
		
		initUpdateCurrentPageUrl : function()
		{
			$('.url > a').bind( 'click', updateCurrentPageUrl );
		},
		
		initReplyLink : function()
		{
			$('.replyLink').bind( 'click', showAddPostForm );
			$('.replyWithQuoteLink').bind( 'click', showAddPostForm );
		},
		
		initAddLinkedSiteLink : function()
		{
			$('.addLinkedSiteLink').bind( 'click', showHideAddLinkedSiteForm );
		},
		
		initShowPostFormWrapper : function()
		{
			displayPostWrapper();
		}
	}
}();

/********************************************************************
 Class: Rating
*********************************************************************/
Rowdii.Rate = function()
{
	var nbrOfStar = 5;
	
	//-----------------------------------------------------------------
	function display(e)
	{
		var infoArray = $(this).attr( 'id' ).split( '-' );
		var id = infoArray[1];
		var starIndex = infoArray[2];
		
		for( i=1; i<=starIndex; i++ )
		{
			$('#star-' + id + '-' + i + ' > img' ).attr( 'src', imgBaseUrl + '/icons/greenStar.gif' );
		}
	}
	
	//-----------------------------------------------------------------
	function resetStar(e)
	{
		var infoArray = $(this).attr( 'id' ).split( '-' );
		var id = infoArray[1];
		var starIndex = infoArray[2];
		var currentPostRate = $('#rate-' + id ).val();
		
		for( i=1; i<=nbrOfStar; i++ )
		{
			$('#star-' + id + '-' + i + ' > img' ).attr( 'src', imgBaseUrl + '/icons/greyStar.gif' );
		}
		
		for( i=1; i<=currentPostRate; i++ )
		{
			$('#star-' + id + '-' + i + ' > img' ).attr( 'src', imgBaseUrl + '/icons/greenStar.gif' );
		}
	}
	
	//-----------------------------------------------------------------
	function applySuccess(response)
	{
		var xmlDoc = response.responseXML;
		var comment = getAttrValue( xmlDoc, 'comment' );
		var value = getAttrValue( xmlDoc, 'value' );
		var id = getAttrValue( xmlDoc, 'id' );
		var code = getAttrValue( xmlDoc, 'code' );
		
		$('#rate-' + id ).val( value );
		
		for( i=1; i<=nbrOfStar; i++ )
		{
			$('#star-' + id + '-' + i + ' > img' ).attr( 'src', imgBaseUrl + '/icons/greyStar.gif' );
		}
		
		for( i=1; i<=value; i++ )
		{
			$('#star-' + id + '-' + i + ' > img' ).attr( 'src', imgBaseUrl + '/icons/greenStar.gif' );
		}
		
		if( code != "0" )
			$('#rateThisPostText-' + id).html( '<span class="thanksForRating">Thanks for voting</span>' );
		
		$('#rateCommentSpace-' + id ).html( comment ).hide().slideDown().pulsate().pulsate().pulsate();
	}
	
	//-----------------------------------------------------------------
	function selfClose(e)
	{
		var infoArray = $(this).attr( 'id' ).split( '-' );
		var id = infoArray[1];
	
		$(this).slideUp().fadeTo( 'slow', 0.01 ).html('');
	}
	
	//-----------------------------------------------------------------
	function getAttrValue(xmlDoc, attribute)
	{
		var global = xmlDoc.getElementsByTagName('global');

		return global.length > 0
			? global[0].getAttribute(attribute)
			: '';
	}
	
	//-----------------------------------------------------------------
	function applyRating(e)
	{
		e.preventDefault();
		e.stopPropagation();
		
		var infoArray = $(this).attr( 'id' ).split( '-' );
		var postId = infoArray[1];
		var starIndex = infoArray[2];
		
		$.ajax( {
			type:'GET',
			complete: applySuccess,
			url: baseUrl + '/Utility/Async/Rate.aspx?val=' + starIndex + '&id=' + postId
			});
	}
	
	//-----------------------------------------------------------------
	function applyLinkedSiteRating(e)
	{

		e.preventDefault();
		e.stopPropagation();
		
		var infoArray = $(this).attr( 'id' ).split( '-' );
		var linkedSiteId = infoArray[1];
		var starIndex = infoArray[2];

		$.ajax( {
			type:'GET',
			complete: applySuccess,
			url: baseUrl + '/Utility/Async/LinkedSiteRate.aspx?val=' + starIndex + '&id=' + linkedSiteId
			});
	}

	return {
		//---------------------------------------------------------------
		init : function(e)
		{
			$('.ratingStarLinkSite').bind('mouseover', display ).bind( 'mouseout', resetStar ).bind( 'click', applyLinkedSiteRating );;
			$('.ratingStar').bind('mouseover', display ).bind( 'mouseout', resetStar ).bind( 'click', applyRating );
			$('.rateCommentSpace').bind( 'click', selfClose );
		}
	}
}();

/********************************************************************
 Class: Slider
*********************************************************************/
Rowdii.Slider = function()
{	
	var _windowEventInterval = null;
	var sliderImgBaseUrl = imgBaseUrl + "/layout/slider/";
	var $frameset = parent.$('#_frameSet');
	var $kids = $frameset.children();
	
	var internalFrame = $kids[0];
	var externalFrame = $kids[2];
	
	//-----------------------------------------------------------------
	function closeMessageBoard(e)
	{
		$frameset.attr('rows', '0,0,100' );
	}
	
	//-----------------------------------------------------------------
	function mouseDown(e)
	{
		document.onmousemove = moveSlider;
	}
	
	//-----------------------------------------------------------------
	function mouseUp(e)
	{
		document.onmousemove = null;
	}
	
	//-----------------------------------------------------------------
	function moveSlider(e)
	{
		e = ( e==null || e==undefined) ? event : e;
		var li_current = internalFrame.scrollHeight;
		
		var li_mod = -15 + e.clientY;

		if (li_mod < -1 || li_mod > 1) {
			var li_height = li_current + li_mod;
			li_height = (li_height < 30)? 30: li_height;
			$frameset.attr( 'rows' , li_height + ',27,*' );
			
			if( li_height <= 30 ) {
				document.onmousemove = null;
			}
		}
	}
		
	return {
		//--------------------------------------------------------------
		init : function()
		{	
			$('#close').bind('click', closeMessageBoard );
			//.bind( 'mouseout', mouseUp ).bind('mouseover', mouseUp).bind('click', mouseUp );			
			$('#wrapper').bind('mousedown', mouseDown ).bind( 'mouseup', mouseUp );
			
			//&& navigator.appName == "Netscape"
			if( navigator.appName == "Microsoft Internet Explorer" )
			{
				//$('#wrapper').bind( 'mouseout', setFocus );
				//$('#sliderTable').bind('mousedown', mouseDown ).bind( 'mouseup', mouseUp );
			}
			
			_windowEventInterval = window.setInterval('Rowdii.Slider.delay()', 1000);
		},
		
		//--------------------------------------------------------------
		delay : function()
		{
			window.clearInterval(_windowEventInterval);
			_windowEventInterval = window.setInterval('Rowdii.Slider.slide()', 5);
		},

		//--------------------------------------------------------------
		slide : function()
		{
			var li_current = internalFrame.scrollHeight;

			var li_mod = Math.round((300 - li_current) / 3);
			li_mod = (li_mod <= 1)? 0: li_mod;

			if (li_mod <= 0) {
				window.clearInterval(_windowEventInterval);
				return;
			}
			$frameset.attr( 'rows', (li_current + li_mod) + ',27,*');
		}	
	}
}();

/********************************************************************
 Class: TextareaExpander
*********************************************************************/
Rowdii.TextareaExpander = function()
{
	var _minIcon = imgBaseUrl + '/misc/button_minimize.gif';
	var _maxIcon = imgBaseUrl + '/misc/button_maximize.gif';
	var _multiplier = 4;

	return {
		//---------------------------------------------------------------
		setup : function(textarea)
		{
			// Define custom attribute (default number of rows)
			$(textarea).attr('defaultRows', textarea.rows);

			// Create link and insert after textarea
			var expandLink = $('<a />')
				.attr({href: '#', title: "Maximize/minimize the size of the text box", tabindex: -1})
				.addClass('img')
				.insertAfter($(textarea));

			// Create image and append to link
			var expandImg = $('<img />')
				.attr({id: 'button_' + textarea.id, alt: "Maximize/minimize", src: _maxIcon, width: 9, height: 9, tabindex: -1})
				.addClass('expandButton').bind('click', Rowdii.TextareaExpander.apply)
				.appendTo(expandLink);
		},

		//---------------------------------------------------------------
		apply : function(e)
		{
			e.preventDefault();

			var textarea = $(this).parent().prev('textarea.expand');

			if(textarea.attr('rows') == textarea.attr('defaultRows')) {
				textarea.attr('rows', textarea.attr('rows') * _multiplier);
				$(this).attr('src', _minIcon);
			}
			else {
				textarea.attr('rows', textarea.attr('defaultRows'));
				$(this).attr('src', _maxIcon);
			}
		}
	}
}();

/********************************************************************
 Class: Page
*********************************************************************/
Rowdii.Page = function()
{
	return {
		//---------------------------------------------------------------
		init : function(e)
		{
			// Add 'expand' button to associated textareas
			$('textarea.expand').each(function(){Rowdii.TextareaExpander.setup(this);});
			$('.urlInfo').attr( 'readonly', 'readonly' );
			
			$('.profileMenu').hover(
				function() { $(this).addClass('profileMenuHover'); },
				function() { $(this).removeClass('profileMenuHover'); }
			);
		}
	}
}();

/********************************************************************
 Class: Button
*********************************************************************/
Rowdii.Button = function()
{
	//-----------------------------------------------------------------
	function showHoverImage(e)
	{
		var imgSrc = $(this).attr('src' ).replace(".gif", "Hover.gif");
		$(this).attr('src', imgSrc );
	}
	
	//-----------------------------------------------------------------
	function resetImage(e)
	{
		var imgSrc = $(this).attr('src' ).replace("Hover.gif", ".gif");
		$(this).attr('src', imgSrc );
	}
	
	//-----------------------------------------------------------------
	function back(e)
	{
		e.preventDefault();
		e.stopPropagation();
		history.back(-1);
	}

	return {
		//---------------------------------------------------------------
		init : function(e)
		{
			$('.imageButton').bind('mouseover', showHoverImage).bind( 'mouseout', resetImage );
			$('.back').bind('click', back );
		}
	}
}();