// JavaScript Document
// Author: Damjan Gataric, EtonDIGITAL (www.etondigital.com)
// Code for Marketplace module

$(document).ready(function() {

	//css subtitute
	$('#marketplaceContent ul.breadcrumb li:first').children('a').css({backgroundImage: 'none', padding: '0 0 0 5px'});
	$('#marketplaceContent div.itemList div.item.inactive div.leftColumn').css('opacity',0.5);
	$('#marketplaceContent div.itemList div.item.inactive div.info').css('opacity',0.5);


	//categories expanding
	var $categoriesList = $('#marketplaceContent div.categories ul');	
	$categoriesList.each(function() {
		if ($(this).children('li').length>10) {
			$(this).children('li').slice(9).hide();
			$(this).append('<li class="more"><a href="#" class="more">More</a></li>');
		}
	});	
	$('#marketplaceContent div.categories a.more').livequery('click', function(event) {
		if ($(this).hasClass('less')) {
			$(this).removeClass('less').text('More');
			$(this).parent().parent().children('li').slice(9).not('.more').slideUp();
		} else {
			$(this).addClass('less').text('Less');
			$(this).parent().parent().children('li').slice(9).not('.more').slideDown();
		}
		return false;
	});
	
	$('#marketplaceContent a.contact').livequery('click', function(event) {
		var recipientId = $(this).attr('id');
		var itemId = $(this).attr('rel');
		var member = $(this).attr('title');
		if (itemId=="") {
			jModalWindowForProtectedPhoto('You must be signed in to contact '+member+'.', 'Information', function(res) { if(res) window.location = '/signin';});
		} else {
			var form = '<form name="sendMessageForm" class="sendMessage" action=""><label for="subject">Subject</label><input type="text" name="subject" class="txt" />'
			form = form + '<label for="message">Your message</label><textarea cols="10" rows="6" name="message"></textarea>'
			form = form + '<input type="hidden" name="recipientId" value="'+recipientId+'" /><input type="hidden" name="itemId" value="'+itemId+'" /></form>'
			jSendMessageToBuy(form,'Contact '+member);
		}
		return false;
	});
	
	
	//item delete
	$('#marketplaceAction').change(function() {
	
		if (this.value == 'remove') { 
			this.selectedIndex = 0; 
			
			if (jQuery('div.item_photo input:checked').length==0) {
				jAlert('No photos were selected.', 'Warning'); 
			} else { 
				jConfirm('Do you realy want to delete?', 'Please Confirm', function(res) { 
						 if (res) { 
						 	//jQuery('#action_message').text('Performing selected action...'); 
							var itemsData = jQuery('#item_photos').serialize(); 
							jQuery.ajax({ 
										type: 'POST', 
										url: '/marketplace/deleteItemPhotos', 
										data: itemsData, 
										success: function(status) { 
											if(status = 'SUCCESS') { 
												jQuery('div.item_photo input:checked').parent('div').fadeOut('normal', 
														function(){
															jQuery(this).remove(); 
															//jQuery('#action_message').text(''); 
															//jQuery('span.album_number span.number').text($('li.photo_li').length); 
														}
												); 
											} 
										} 
							});
						} 
				});
			}		
		}		
	});

	//carousel
	if ($('#marketplaceCarousel').length>0) {
		$('#marketplaceCarousel').jCarouselLite({
			btnNext: "div.marketplaceCarouselWrap a.next",
			btnPrev: "div.marketplaceCarouselWrap a.prev",
			visible: 4,
			scroll: 1,
			circular: false
		});
	}
	
	$('#marketplaceContent div.rightColumn a.activateItem').hover(
	function() {
		$(this).parent().parent().parent().siblings('div.leftColumn, div.info').css('opacity',1);
	},
	function() {
		$(this).parent().parent().parent().siblings('div.leftColumn, div.info').css('opacity',0.5);
	});
		
	$('#marketplaceContent div.rightColumn a.deactivateItem').hover(
	function() {
		$(this).parent().parent().parent().siblings('div.leftColumn, div.info').css('opacity',0.5);
	},
	function() {
		$(this).parent().parent().parent().siblings('div.leftColumn, div.info').css('opacity',1);
	});
	
	$('div#marketplaceContent #filter_by').change(function(){
		//if ($(this).val() <> ''){
			$('form.marketplace_filter_by').submit();
		//}
		//window.location = '/marketplace/list?filter_by='+$(this).val();
		
	});


});
