// Document: ready event
jQuery(document).ready(function(){
	openExternalLinks();
	showFavorite();
	if(document.getElementById("avatar_ul")) avatarBar();
	designElems();
	//popupFn();
});

function openExternalLinks() {
	jQuery('.authors_box a').attr("target", "_blank");
}

// Box on main page (and subpages)
function selectCategoriesOnMain( boxId ) {
	jQuery("#main-box .tabs li").removeClass( 'active' );
	jQuery("#cat_li"+boxId).addClass( 'active' );
	jQuery(".tabed .hideable").hide();
	jQuery("#cats_"+boxId).show();
}


// Favourites
function showFavorite() {
	jQuery('#favorite-inside').width(jQuery('#favorite-actions').width()-4);
	jQuery('#favorite-toggle, #favorite-inside').bind( 'mouseenter', function(){jQuery('#favorite-inside').removeClass('slideUp').addClass('slideDown'); setTimeout(function(){if ( jQuery('#favorite-inside').hasClass('slideDown') ) { jQuery('#favorite-inside').slideDown(100); jQuery('#favorite-first').addClass('slide-down'); }}, 200) } );
	jQuery('#favorite-toggle, #favorite-inside').bind( 'mouseleave', function(){jQuery('#favorite-inside').removeClass('slideDown').addClass('slideUp'); setTimeout(function(){if ( jQuery('#favorite-inside').hasClass('slideUp') ) { jQuery('#favorite-inside').slideUp(100, function(){ jQuery('#favorite-first').removeClass('slide-down'); } ); }}, 300) } );
}


// Avatar bar (slideable)
function avatarBar() {
	var numUl = jQuery('#avatar_ul').attr("class");
	re = /\d+/;
	var getNumUl = re.exec(numUl);
	//console.log(getNumUl);
	jQuery('#avatar_next').click(function() {
		var leftUl = parseInt(jQuery('#avatar_ul').css('left'));
		if(leftUl == (getNumUl - 9)*100*(-1)) {
			jQuery('#avatar_ul').animate({'left': 0}, 400);
		} else {
			jQuery('#avatar_ul').animate({'left': leftUl - 100}, 400);
		}
		return false;
	});
	jQuery('#avatar_prev').click(function() {
		var leftUl = parseInt(jQuery('#avatar_ul').css('left'));
		if(leftUl != 0) {
			jQuery('#avatar_ul').animate({'left': leftUl + 100}, 400);
		} else {
			jQuery('#avatar_ul').animate({'left': "-"+(getNumUl-9) * 100}, 400);
		}
		return false;
	});
}


// To hide empty <li/> in box with tab "Search by category"
function designElems() {
	jQuery('ul.categories_ul').find('li').each(function () {
		if(jQuery(this).text() == 0) jQuery(this).css("display", "none");
	});
}

/*
// to show popup for action "Republish this page"
function popupFn() {
	
	jQuery('a#republishPage').click(function() {
		jQuery('#lightbox').show();
		jQuery('a.popupExit').click(closePopup);
		
		return false;
	});
	
}
*/

// to close popup "Republish this page"
function closePopup() {
	jQuery('#lightbox').hide();
	return false;
}


// Submits RSS feeds by keyword
function submitRssKeywordSearch() {
	var $keyword = jQuery("input#s").val();
	if( $keyword.length > 0 ) {
		document.location.href='/tag/'+$keyword+'/feed/';
		return false;
	}
	alert( "Please fill in the keyword for a custom RSS channel." );
	return false;
}

// Hooks for registration page
function registrationHooks() {
	registerRegistrationEvent( "user_name", "Username" );
	registerRegistrationEvent( "user_email", "E-mail" );
}


function registerRegistrationEvent( $fieldName, $textName ) {
	jQuery("#"+$fieldName).bind( "keyup focus",  function(e){
		var $val = jQuery("#"+$fieldName).val();
		switch( true ) {
			case $val.length > 0 && $val.length <= 3:
				//addRegistrationNotice( $fieldName, $textName+" is too short" );
				break;
			case $val.length > 3:
				jQuery.getJSON( "/ajax/register_"+$fieldName+"/"+$val+"/",
					function(data) {
						switch( data[0] ) {
							case "true":
								addRegistrationNotice( $fieldName, $textName+" is already used" );
								break;
							default:
								addRegistrationNotice( $fieldName, "" );
								break;
						}
					});
				break;
		}
	});
}


function addRegistrationNotice( $fieldName, $notice ) {
	if( $notice == "" ) {
		jQuery("#"+$fieldName+"_notice").remove();
		return;
	}
	var $elem = jQuery("#"+$fieldName+"_notice").html();
	if( $elem != null ) {
		jQuery("#"+$fieldName+"_notice").text($notice);
		return;
	}
	jQuery("#"+$fieldName).after('<span id="'+$fieldName+'_notice" class="notice">'+$notice+'</span>');
}


// Prepare widget
function setupWidget() {
	var $width = jQuery("#wwidth").val();
	var $height = jQuery("#wheight").val();
	var $sort = jQuery("#wsortyby").val();
	// setup width
	if( $width < 180 ) {
		jQuery("#wwidth").val( "180" );
	}
	if( $width >= 1000 ) {
		jQuery("#wwidth").val( "999" );
	}
	// setup height
	if( $height < 140 ) {
		jQuery("#wheight").val( "140" );
	}
	if( $height >= 1000 ) {
		jQuery("#wheight").val( "999" );
	}
	// sortby
	if( $sort <= 0 || $sort > 2 ) {
		$sort = 1;
	}
	// set widget
	setWidgetDetails( $width, $height, $sort );
}


// Print widget
function setWidgetDetails( $width, $height, $sort ) {
	$html = '<iframe border="0" frameborder="0" src="'+$domain+'/get_widget/'+$userId+'/width='+$width+';height='+$height+';sort='+$sort+';" width="'+$width+'" height="'+$height+'" scrolling="no" allowtransparency="true" style="background-color: transparent;"></iframe>';
	jQuery("#widget-contents").html($html);
	jQuery("#widgetbox").html(htmlspecialchars($html));

}

