var $j = jQuery.noConflict();

$j(document).ready(function() {
	//When page loads...
	if($j('#tabbed_sections').length != 0){
		$j('.tab_content').hide(); 
		$j('ul.tabs li:first').addClass('active').show(); 
		$j('.tab_content:first').show(); 
		
		//On Load Event
		$j('#tab1').load('/inc/app/news.php', { 'type': 'news' } );
	
		//On Click Event
		$j('ul.tabs li').click(function() {
			
			$j('ul.tabs li').removeClass('active'); 
			$j(this).addClass('active'); 
			$j('.tab_content').hide(); 
	
			var activeTab = $j(this).find('a').attr('class'); 
			var type = $j(this).find('a').attr('title');
			$j('#'+activeTab).slideUp().fadeOut();
			loadTab('/inc/app/news.php','#'+activeTab,type);
			$j('#'+activeTab).slideDown().fadeIn();
			return false;
		});
	}
	
	//When page loads...
	if($j('#myaccount_sections').length != 0){
		$j('.tab_content').hide(); 
		$j('ul.tabs li:first').addClass('active').show(); 
		$j('.tab_content:first').show(); 
		
		if($j('#profile_user').val() != ''){
			var user = $j('#profile_user').val();
		}
		else {
			var user = '';
		}

		//On Load Event
		if(user != '' && $j('#tab1').length != 0){
			$j('#tab1').load('/inc/app/profile.php', { 'type': 'sketchpad', 'user': user } );
		}
		else {
			$j('#tab2').load('/inc/app/profile.php', { 'type': 'recentcomments', 'user': user } );
		}
	
		//On Click Event
		$j('ul.tabs li').click(function() {
			
			$j('ul.tabs li').removeClass('active'); 
			$j(this).addClass('active'); 
			$j('.tab_content').hide(); 
	
			var activeTab = $j(this).find('a').attr('class'); 
			var type = $j(this).find('a').attr('title');
			
			if($j('#profile_user').val() != ''){
				var user = $j('#profile_user').val();
			}
			else {
				var user = '';
			}
			
			$j('#'+activeTab).slideUp().fadeOut();
			profileTab('/inc/app/profile.php','#'+activeTab,type,user);
			$j('#'+activeTab).slideDown().fadeIn();
			return false;
		});
	}
	
	//When page loads...
	if($j('#message_sections').length != 0){
		$j('.tab_content').hide(); 
		$j('ul.tabs li:first').addClass('active').show(); 
		$j('.tab_content:first').show(); 
		
		//On Load Event
		$j('#tab1').load('/inc/app/messages.php', { 'type': 'inboxmessages' } );
	
		//On Click Event
		$j('ul.tabs li').click(function() {
			
			$j('ul.tabs li').removeClass('active'); 
			$j(this).addClass('active'); 
			$j('.tab_content').hide(); 
	
			var activeTab = $j(this).find('a').attr('class'); 
			var type = $j(this).find('a').attr('title');
			$j('#'+activeTab).slideUp().fadeOut();
			loadTab('/inc/app/messages.php','#'+activeTab,type);
			$j('#'+activeTab).slideDown().fadeIn();
			return false;
		});
	}
	
	$j('#navigation li').hover(
		function () {
			$j('ul', this).stop(false,true).slideDown('slow', function() {}); 
			$j('li').css("cursor", "pointer");
		}, 
		function () {
			$j('ul', this).stop(false,true).slideUp(0, function() {});
 
		}
	);
	
	if($j('#myaccount_edit_content').length != 0){
		$j('#myaccount_edit_content').hide();
	}

});

function showPreloader(sub) 
{ 
	$j("#preloaderj").show();
} 

function hidePreloader() 
{ 
    $j("#preloaderj").hide(); 
}

function loadTab(url, div, type) {
    $j.ajax( 
    { 
    	type: 'post',
        url: url, 
        cache: false, 
        data: {type:type},
        success: function(message){ 
    		$j(div).empty().append(message); 
        } 
    });
} 

function profileTab(url, div, type, user) {
    $j.ajax( 
    { 
    	type: 'post',
        url: url, 
        cache: false, 
        data: {type:type, user:user},
        success: function(message){ 
    		$j(div).empty().append(message); 
        } 
    });
}

function slideEdit(selection){
	if($j('.'+selection).click){
		$j('.myaccount_edit_content_'+selection).slideToggle('slow');
		if(!$j('.myaccount_edit_content_'+selection).attr('title')){
			$j('.myaccount_edit_content_'+selection).empty().append('<div id="preloaderj">&nbsp;</div>');
			loadTab('/inc/app/profile.php','.myaccount_edit_content_'+selection,selection);
			$j('.myaccount_edit_content_'+selection).attr('title','active');
		}
		else if($j('.myaccount_edit_content_'+selection).attr('title') == 'active'){
			$j('.myaccount_edit_content_'+selection).attr('title','');
		}
	}
}

function slideForward(){
	var count_images = $j('#sketchpad_preview_images > img').size();
	$j('#sketchpad_preview_images > img').each(
		function(i) {
			if($j(this).is(':visible') == true){
				if($j(this).next().is(':hidden') == true){
					$j(this).hide();
					//$j(this).nextAll().hide();
					$j(this).next().show();
					return false;
				}
			}
		}
	);
}

function slideBackwards(){
	var count_images = $j('#sketchpad_preview_images > img').size();
	$j('#sketchpad_preview_images > img').each(
		function(i) {
			if($j(this).is(':visible') == true){
				if($j(this).prev().is(':hidden') == true){
					$j(this).hide();
					//$j(this).prevAll().hide();
					$j(this).prev().show();
					return false;
				}
			}
		}
	);
}