/* rx-library.js */

// menu drop down : toggling + callbacks
// 24/08/09
jQuery.fn.RX_menu = function(Q) {
	var $ = jQuery.noConflict();
	return this.each(function() {
		$(this).hover(
			function() {
				var t = $(this),
						i = t.children('ul');

				if(i.size()) {
					t.addClass('rx-selected');
					i.css( {visibility:'visible', display:'none'}).slideDown('fast');
					//Q.onHover();
				}
			},
			function() {
				var t = $(this),
						i = t.children('ul');

				if(i.size()) {
					t.removeClass('rx-selected');
					i.css( {visibility:'hidden' } );
					//Q.onOut();
				}
			}
		);
	});
}
//

// open links in another window
// 01/09/09
jQuery.fn.RX_externalLink = function() {
	var $ = jQuery.noConflict();
	return this.each(function() {
		$(this).click(function() {
			window.open($(this).attr('href'));
			return false;
		})
		return;
	});
}
//


jQuery(document).ready(function($) {
	$('a.rx-el, .rx-el a').RX_externalLink();

	$('.menu-navy li').RX_menu();
	

});

jQuery(document).ready(function($) {
	
	
	
	$('.menu-navy li').each(function(){								 
   		$(this).children('ul').children('li:last').addClass('last');
    });
	
	//create thumbs scrolling
	$('.e-thumbs').each(function() {
		var wrap = $(this);
		//add active class to first li in each post
		//$(this).find('li:first').addClass('active');
		//apply active class to proper thumb
		var init_alt = $(this).closest('.x-post').find('.e-pic img:first').attr('alt');
		//$(this).find('img').each(function() { if($(this).attr('alt') == init_alt) { $(this).parent('li').addClass('active'); return false; } });
		var init_first = $(this).find('li:first');
		if(init_first.find('img').attr('alt') == init_alt) { init_first.addClass('active'); }else { wrap.find('li:last').addClass('active'); }
		
		//apply cycle plugin
		var cprev = $(this).find('.slide-left');
		var cnext = $(this).find('.slide-right');
		
		$(this).find('.e-thumbs-wrap').serialScroll({
			axis:'x',
			prev:cprev,
			next:cnext,
			items:'li',
			cycle:false,
			exclude:4,
			duration:100,
			step:1,
			constant:false
		});
		
		//add the click function to the thumbs, change out image on click
		$(this).find('li').click(function() {
			var x = $(this).closest('.x-post');
			var li = $(this);
			var i = $(this).closest('.x-post').find('.e-pic img:first'); //full image
			var v = $(this).closest('.x-post').find('.e-pic .video');
			
			var left = $(this).closest('.x-post').find('.e-pic .left-hover');
			var right = $(this).closest('.x-post').find('.e-pic .right-hover');
			
			var isVideo = li.hasClass('video');
			
			var image = $(this).find('img');
			
			var pBy1 = x.find('.p-head-photo-by');
			var pBy2 = x.find('.p-photo-by');	

			if (isVideo) {
			
				var videoid = li.html();
				
				left.hide();
				right.hide();
				
				i.hide();
				
				pBy1.hide();
				pBy2.hide();
				
				v.hide()
//				 .html('<object width="898" height="572"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + videoid + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + videoid + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="898" height="572"></embed></object>')
				.html(videoid)
				 .show();
				
			} else {
				var src = image.attr('src');
				var alt = image.attr('alt');
				var title = image.attr('title');
				var newSrc = src.replace('thumbs_', '');
				newSrc = newSrc.replace('thumbs/', '');
				
				pBy1.show();
				pBy2.show();
				
				left.show();
				right.show();
				
				var videoVisible = v.is(':visible');
				if (videoVisible) {
					v.hide();
				}
				
				//now load and replace the existing image
//				if(i.attr('src') != newSrc) {
					i.fadeOut('', function(){$(this).attr('src', newSrc);}).load(function () { i.fadeIn(); });
					//replace alt and title now
					i.attr('alt', alt);
					i.attr('title', title);		
					pBy1.html(image.attr('title'));
					pBy2.html(image.attr('title'));
					Cufon.refresh();
//				}//else do nothing, that pic is already there		
			}
			
			//swap classes on thumbs
			li.closest('.e-thumbs').find('li.active').removeClass('active');
			li.addClass('active');			
			
		});
		
	});
	
	//make head bar and post image open post as well
	$('.p-head-bar, .p-post-title-bar').click(function() {
		$(this).closest('.x-post').find('.post-title').click();
	});
	$('.e-pic').each(function() { $(this).find('img:first').click(function() { $(this).closest('.x-post').find('.post-title').click(); }); });
	
	//replace photo by on first image
	$('.x-post .p-head-photo-by, .x-post .p-photo-by').each(function() {
		$(this).html($(this).closest('.x-post').find('.e-pic img:first').attr('title'));
	});
	
	//function to replace each time
	jQuery.fn.replacePhotoBy = function(text) {
		$(this).html(text);
	}
	
	//open post content on post title click and close all other posts
	$('.post-title').click(function () {
		var x = $(this).closest('.x-post');
		var post = $(this).next('.post-content');
		var comments = post.find('.post-comments');
		var s = post.find('.p-switch');
		var thumbs = x.find('.e-thumbs');
		var headBar = x.find('.p-head-bar');
		var titleBar = x.find('.p-post-title-bar');
		var title = $(this);
		
		if(post.is(':hidden')) {
			//close all others first
			$('.post-content:visible').hide();
			$('.p-switch').hide();
			$('.e-thumbs, .p-post-title-bar').hide();
			$('.p-head-bar').show();
			$().swapText(x, 'close');
			
			//open this one
			post.slideToggle();
			//open comments display
			comments.fadeIn('fast');
			
			//thumbs.slideToggle();
			headBar.slideToggle();
			titleBar.slideToggle();
			
			$(this).swapText(x, 'open');
									
		}else {
			//close post
			headBar.slideToggle();
			titleBar.slideToggle();
			//thumbs.slideToggle();
			post.slideToggle();
			s.fadeOut('fast');
			
			$(this).swapText(x, 'close');
		}
		
		$.scrollTo(x, 1000);
		
	});
	
	jQuery.fn.swapText = function(o, type) {
		var pCity = o.find('.hide-post-city').html();
		var pTitle = o.find('.hide-post-title').html();
		var pDate = o.find('.hide-post-date').html();
		var pCount = o.find('.hide-post-count').html();
		
		var headBar = o.find('.p-head-bar');
		var titleBar = o.find('.p-post-title-bar');
		var title = o.find('.post-title');
		
		if(type == 'open') {
			title.find('p:first').html(pCity);
			title.find('p:last').html('');
		}else {
			title.find('p:first').html(pTitle+' | '+pDate);
			title.find('p:last').html(pCount);
		}
		
		Cufon.refresh();
	}
	
	//left/right nav hover effect on e-pic
	$('.left-hover').hover(function() {
		//on over
		//$(this).find('.h-hide').fadeIn();
		//$(this).find('.h-hide').show();
	}, function() {
		//on out
		//$(this).find('.h-hide').fadeOut();
		//$(this).find('.h-hide').hide();
	});
	
	$('.right-hover').hover(function() {
		//on over
		//$(this).find('.h-hide').fadeIn();
		//$(this).find('.h-hide').show();
	}, function() {
		//on out
		//$(this).find('.h-hide').fadeOut();
		//$(this).find('.h-hide').hide();
	});
	
	$('.left-hover').click(function() {
		var x = $(this).closest('.x-post');
		var li = x.find('.e-thumbs li.active').prev();
		if(li.length < 1) { li = x.find('.e-thumbs li:last'); }
		li.click();
		x.find('.e-thumbs .slide-left').click();
	});
	
	$('.right-hover').click(function() {
		var x = $(this).closest('.x-post');
		var li = x.find('.e-thumbs li.active').next();
		if(li.length < 1) { li = x.find('.e-thumbs li:first'); }
		li.click();
		x.find('.e-thumbs .slide-right').click();
	});
	
	
	$('.e-picnav a').click(function(){
		var fn = $(this);
		var epic = $(this).siblings('e-pic');
		var lnk;
		if (fn.hasClass('next')) {
			lnk = fn.parent().siblings('.e-pic').find('.right-hover');
		} else {
			lnk = fn.parent().siblings('.e-pic').find('.left-hover');
		}
		lnk.trigger('click');
		return false;
	});
	
	
	
	//add open functionality when comment link button is clicked
	$('.comment-box').click(function() {
		var post = $(this).closest('.x-post').find('.post-content');
		var c_form = post.find('.post-comment');
		//if it is main comment box, make sure comment post is opened
		if(post.is(':hidden')) {
			$('.post-content:visible').hide();
			post.slideToggle();
		}else {
			//post is already opened. Make sure no other switches are open
			if(c_form.is(':hidden')) {
				post.find('.p-switch:visible').hide();
			}
		}
		
		//open comment form
		if(c_form.is(':hidden')) {
			c_form.show('fast');
		}
		
	});
	
	//cancel buttons, return to main comment screen
	$('.close').click(function() {
		var s = $(this).closest('.p-switch');
		s.hide('fast').closest('.post-switch').find('.post-comments').show('fast');		
	});
	
	//send to friend button
	$('.send-to-friend').click(function(event) {
		event.preventDefault();
		var p = $(this).closest('.post-content');
		var s_form = p.find('.post-send-to-friend');
		//post has to be opened already, so skip that
		//make sure it isn't already opened
		if(s_form.is(':hidden')) {
			//close others and open it
			p.find('.p-switch').hide('fast');
			s_form.show('fast');
		}//else do nothing, it is already there
	});
	
	//link to post button
	$('.link-to-post').click(function(event) {
		event.preventDefault();
		var p = $(this).closest('.post-content');
		var l_form = p.find('.post-link-to-post');
		//make sure it is not already showing first
		if(l_form.is(':hidden')) {
			p.find('.p-switch').hide('fast');
			l_form.show('fast');
		}//else do nothing, it is showing already
	});
	
	
	//create comments scroll effect
	$('.post-comments').each(function() {
		
		var cup = $(this).find('.c-up');
		var cdown = $(this).find('.c-down');
		
		$(this).find('.comments-wrap').serialScroll({
			axis:'y',
			prev:cup,
			next:cdown,
			items:'.comment',
			cycle:false,
			duration:100
		});
		
	});
		
	//fix stupid page text issue
	$('.page-text').append("<div class='clear'></div>");
	
	//click to view post link
	$('.click-to-view-post').click(function() {
		$(this).closest('.x-post').find('.post-title').click();
	});
	
	//diable right click menu
	$().bind('contextmenu', function(e) { e.preventDefault(); });
		
	//cufon text
	Cufon.replace('.post-author, .click-to-view-post, .post-title, .post-switch h3, .cufon1, .page-title, .e-picnav');
	Cufon.replace('.p-post-title, .p-photo-by, .p-head-bar, .b-archive-list, .b-links, .b-foot-title, .c-row, .close, .submit');
	
});

//preload all the images once the site loads
window.onload = function() {
	jQuery(document).ready(function($) {
		$('.e-thumbs img').each(function() {
			var newSrc = $(this).attr('src').replace('thumbs_', '').replace('thumbs/', '');
			jQuery.preLoadImages(newSrc);
		});
	});
}

jQuery(document).ready(function($){
    $(document).bind("contextmenu",function(e){
        return false;
    });
	
	$('img').mousedown(function(event) { if(event.preventDefault) { event.preventDefault(); } });
		
});

