glowloaded  = false;
doload 		= true;
windowLocationBase = window.location.protocol+'//'+window.location.host;

function setupCommentsState(){
    	//hide the comment form:
	jQuery('.commentForm,').css('display','none');

	//show toggle for appbox menu items
	jQuery('.app-box-menu a').css('display','block');

	//add javascript form item to comment form.
	jQuery('form#comment-form').submit(function(){
		jQuery('form#comment-form #js').attr('value','1');
	});

	//toggle add post
	jQuery('.wall-add-comment').live('click',function(){
		jQuery('.commentForm').slideToggle('fast',function() {
					jQuery(this).toggleClass('collapse', (jQuery(this).css('display')=='none'));
		});
                
		return false;
	});
	
}

jQuery(document).ready(function()
{
	
	jQuery('iFrame.fblike').attr('allowtransparency','true');
	
	jQuery('.itemCommentsPagination .pagination li.more a').live('click', function(e) {
		var contain = '#commentContainer';
		var extra 	= '';
		var itemid = 0;
		if( glowloaded == false ){
			jQuery.getScript('/templates/blank_j15/js/jquery.glow.js', function() {
				glowloaded = true;
			});
		} 
		
		// get the itemid...
		try { 
		 itemid = parseInt(jQuery(contain).attr('class').replace('comments_',''));
		} catch(err) {}
		
		if( itemid <= 0 || doload == false ){
			
			var itemidu = parseInt(jQuery('#wallContent').attr('class').replace('comments_',''));
			if( itemidu > 0  ){
				itemid =  itemidu;
				contain = '#wallContent';
				extra = '&o=u';
			} else {
				return false;
			}
		}
		
		//add loading message
		jQuery(this).css('backgroundImage','url(/media/system/images/spinner.gif)');
	
		//current
		var current = jQuery(contain+' div.post').size();
		jQuery.ajax({
			   type: "GET",
			   url: "/index.php",
			   data: "option=com_k2&view=ajax&task=gc&a="+itemid+"&c="+current+extra,
			   success: function(msg){
				if( msg != '' ) {
					jQuery(contain).append(msg);
					jQuery(contain+' div.post.new blockquote').glow('grey', 500, 100);
					jQuery(contain+' div.post.new').removeClass('new');
					
				} else { 
					doload = false;
					jQuery('.itemCommentsPagination .pagination li.more a').html('');
				}
				//remove loading image.				
				jQuery('.itemCommentsPagination .pagination li.more a').css('backgroundImage','');
			   }

			 });

		return false;
	});
	
	
	
	setupCommentsState();
	

	//show the pagination for the challenges
	jQuery('#challenges_ready .pagination li').css('display','inline');
	jQuery('#challenges_completed .pagination li').css('display','inline');	

	//set the ul to the height of the largest challenge list item.
	setHeight('#challenges_ready');
	setHeight('#challenges_completed');
			 
	//paginate through the challenges
	paginateChallenges('#challenges_ready');
	paginateChallenges('#challenges_completed');
	
	//remove system messages
	if( jQuery('#message').attr('id') == 'message' && jQuery('#message .no-fade').val() !== '' ){
		setTimeout('removeMessageBox()',5000);
	}
	
});


function removeMessageBox(){
	jQuery('#message').fadeOut(1000,function(){
		jQuery('#message').remove();
	});
}

function paginateChallenges(parent){
	jQuery(parent+' .pagination li:not(.challenge_all) a').click(function(){

		curent_item = jQuery(parent+' > ul > li.active');
		
		if( jQuery(this).parent().attr('class') == 'challenge_prev' ){
			
			new_item = jQuery(curent_item).prev();
			if( jQuery(new_item).attr('class') == undefined ){
				new_item = jQuery(parent+' > ul > li:last-child');
			}
			
		} else {
			new_item = jQuery(curent_item).next();
			if( jQuery(new_item).attr('class') == undefined ){
				new_item = jQuery(parent+' > ul > li:first-child');
			}
		}

		jQuery(curent_item).slideUp('fast', function(){
			jQuery(new_item).slideDown('fast');
		});
		
		jQuery(curent_item).addClass('nodisplay');
		jQuery(curent_item).removeClass('active');

		jQuery(new_item).removeClass('nodisplay');
		jQuery(new_item).addClass('active');
		return false;
	});	
}

function setHeight(parent){
	maxHeight = 0;
	jQuery(parent+' > ul > li').each(function(){
		if(jQuery(this).height() > maxHeight ) {
			maxHeight = jQuery(this).height();
		}
	});
	jQuery(parent+' > ul').css('height',maxHeight+'px');	
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function fbfu(_message, callback){
    if( typeof fb_user === 'number' && inputVal){
        try{
            FB.api('/'+fb_user+'/feed', 'post', { message: _message }, function(response) {
                if( typeof callback == 'function' ){
                    callback;
                }
            });
        }catch(err){
        }
    }
    return false;
}

function fbcu(_message,callback,_link,_caption,_picture,_description){

    if( typeof fb_user === 'number' ){
        if( typeof _message == 'undefined' || _message == '' ){
            return false;
        }
        if( typeof _caption == 'undefined' || _caption == '' ){

            if( jQuery('meta[property=og:description]') ){
               _description = jQuery('meta[property=og:title]').attr("content");
            } else {
                _caption = jQuery(document).attr('title');
            }
        }
        if( typeof _description == 'undefined' || _description == '' ){

            if( jQuery('meta[property=og:description]') ){
               _description = jQuery('meta[property=og:description]').attr("content");
            } else {
                _description = jQuery('meta[name=description]').attr("content");
            }
        }
        if( typeof _link == 'undefined' ){
            _link = jQuery('#addthis').attr('addthis:url');
        }
        if( typeof _picture == 'undefined' ){
            if( typeof jQuery('meta[property=og:image]').attr('content') != 'undefined' ){
               _picture = jQuery('meta[property=og:image]').attr("content");
            } else {
                _picture = '';
                //look for the first picture and use that!
                if( jQuery('.catItemIntroText img') ){
                    _picture = jQuery('.catItemIntroText img:first').attr('src');
                }
            }

            if( _picture.indexOf( 'http' ) <=-1 ){
                _picture = windowLocationBase+_picture;
            }
        }
        try{
            FB.api('/'+fb_user+'/feed', 'post',
                {
                    message: _message
                    ,link: _link
                    ,caption: _caption
                    ,description: _description
                    ,picture: _picture
            }, function(response) {
                    if( typeof callback == 'function' ){
                        callback();
                    }
            });
        }catch(err){}
    }
}
