var init = munky.init;

munkyExt = {

	filterDefault : null,

	init : function(){
		init();
		
		/*----- Accordion venues -----*/
		munky.closeVenues();
		
		$('#photos .venue .label').click(function(){
																$(this).blur();
																var thumbs = $(this).parent().children('.thumbs');
																if(!thumbs.hasClass('open')){
																	munky.closeVenues(true);
																	munky.openVenue($(this).parent(), true);
																}
																else {
																	munky.closeVenues(true);
																}
																return false;
															});
															
		/*----- Update photo via AJAX -----*/
		munky.ajaxChange();
		
		/*----- Vote on who is hotter via AJAX -----*/
		munky.hotter();
	},
	
	hotter : function(){
		$('#comparison .compare .meta a.enlarge').lightBox();
		$('#comparison .compare>a').click(function(){
																if($(this).parent().attr('id')=="hotLeft"){
																	$('#hotRight img').hide();
																}
																else {
																	$('#hotLeft img').hide();
																}
																$(this).blur();
																var href = $(this).attr('href');
																var a = href.match(/a=(\d+)/)[1];
																var b = href.match(/b=(\d+)/)[1];
																var w = href.match(/w=(\d+)/)[1];
																var csrf = href.match(/csrf=([a-f\d]+)/)[1];
																$.getJSON(	"ajax-hotter",
																				{'a' : a, 'b' : b, 'w' : w, 'csrf' : csrf},
																				function(json){
																					if(json=="fail"){
																						location.href = href;
																					}
																					else {
																						$('#comparison').html(json[0]);
																						munky.hotter();																						
																					}
																				});
																return false;
															});
	},
	
	ajaxChange : function(){
		return; //need more page view... sacrificing user experience to keep sponsors happy
		$('#tagForm select option[value=none]').attr('disabled', 'disabled');	
	
		var elements = new Array($('a.change'), $('#mainPhotoLink'), $('#icons a'));
		$(elements).each(function(){
								$(this).click(function(){
										$(this).blur();
										var id = $(this).attr('href').match(/.*?(\d+)$/)[1];
										if(!id){
											return true;
										}
										$.getJSON(	"ajax",
														{'id' : id},
														function(json){
															if(json=="fail"){
																location.href = "./"+id;
															}
															else {
																$('.pages').html(json[0]);
																$('#photoHolder').html(json[1]);
																$('#icons').html(json[2]);
																$('#hotterCTA a').attr('href', json[3]);
																$('#tags').html(json[4]);
																$('#makeProfile').attr('href', json[5]);
																munky.ajaxChange();
															}
														});
										return false;
									});
								});
								
		var tag = $('#doTag');
		tag.click(function(){
			var id = $('#mainPhoto').attr('rel');
			var tagUser = $('#tagForm select').eq(0).val();
			$.getJSON(	"ajax-tag",
							{	'id' : id,
								'tagUser' : tagUser},
							function(json){
								if(json=="fail"){
									//alert("fail");
								}
								else {
									$('#tags').html(json[0]);
								}
							});
			return false;
		});
	},
	
	openVenue : function(ven, slide){
		var thumbs = $(ven).children('.thumbs');
		if(slide){
			thumbs.slideDown();
		}
		else {		
			thumbs.show();
		}
		thumbs.addClass('open');
	},
	
	closeVenues : function(slide){
		if(slide){		
			$('#photos .venue .thumbs').slideUp();
		}
		else {
			$('#photos .venue .thumbs').hide();
		}
		$('#photos .venue .thumbs').removeClass('open');
	}

}

$.extend(munky, munkyExt);