$(document).ready(function() {

	$(".addComment").hide();
	$(".comments").hide();
	$('.berzaInfo').hide();
	$('.youroffer').hide();
	
	
	$(".leaveComment").live('click', function(e) {
		Cthis = $(this);
		e.preventDefault();
		
		if($(".berzaNo_"+Cthis.parent().parent().parent().attr('id')+" .addComment").is(":visible")) {
			$(".berzaNo_"+Cthis.parent().parent().parent().attr('id')+" .addComment").slideUp();
		} else {
			$(".berzaNo_"+Cthis.parent().parent().parent().attr('id')+" .addComment").slideDown();
		}
	
	});
	
	$(".viewComments").live('click', function(e) {
		Cthis = $(this);
		e.preventDefault();
		
		if($(".berzaNo_"+Cthis.parent().parent().parent().attr('id')+" .comments").is(":visible")) {
			$(".berzaNo_"+Cthis.parent().parent().parent().attr('id')+" .comments").slideUp();
		} else {
			$(".berzaNo_"+Cthis.parent().parent().parent().attr('id')+" .comments").slideDown();
		}
	
	});
	
	$(".inputSubmit").live('click', function(e) {
		e.preventDefault();
		//alert("OK");
		ID = $(this).parent().parent().parent().attr('id');
		$.post("js/php/berza_dodaj_komentar.php", {'IDponude': ID, 'Name': $(this).parent().children('input[name="Name"]').val(), 'Email':$(this).parent().children('input[name="Email"]').val(), 'Comment':$(this).parent().children('textarea[name="Comment"]').val()}, function(data) {
			if(data=="OK") {
				jAlert('Vaš komentar je uspešno dodat. Hvala Vam.', 'Dodavanje komentara');
				$('.comments_div').hide().load('js/php/berza_comments_div.php', {'IDponude': ID}, function() {$(this).slideDown();});
				$(".berzaNo_"+ID+" .addComment").slideUp();
			} else {
				jAlert('Vaš komentar nije dodat. Došlo je do greške.'+data, 'Dodavanje komentara');
			}
		});
	});
	
	$(".BerzaMark").live('click', function(e) {
		Mthis = $(this);
		e.preventDefault();
		$.post("js/php/berza_azuriraj_ocenu.php", {'IDponude': $(this).parent().parent().parent().parent().attr('id'), 'Ocena': $(this).attr('id')}, function(data) {
			$(".berzaNo_"+Mthis.parent().parent().parent().attr('id')+" .stars").load("js/php/berza_pisi_zvezde.php", {'IDponude': Mthis.parent().parent().parent().attr('id')});

			if(data=="OK") {
				jAlert('Vaša ocena je uspešno upisana. Hvala Vam.', 'Ocenjivanje ponude');
			} else if(data="OneVote") {
				jAlert('Vaša ocena je već upisana. Možete glasati samo jednom. Hvala Vam.', 'Ocenjivanje ponude');
			} else {
				jAlert('Vaša ocena nije upisana. Došlo je do greške.', 'Ocenjivanje ponude');
			}
			
		});
	});
	
	
	$('.berzaInfoLink').live('click', function(e) {
	
		ID = $(this).parent().parent().parent().attr("id");
		//alert(ID);
		e.preventDefault();
		
		if($(".berzaNo_"+ID+ ' .berzaInfo').is(":visible")) {

			$(".berzaNo_"+ID+ ' .berzaInfo').slideUp("slow");
		
		} else {
		
			$('.berzaNo_'+ID+' .berzaInfoLink').append('<img src="pics/layout/preloader-berza.gif" style="margin:0 0 0 10px;" class="preloader-image" />');
			
			
			$('.berzaNo_'+ID+' .berzaInfo').load('js/php/berza_info.php', {'IDponude': ID, 'lang':$("body").attr('id')}, function() {
				
				$('.preloader-image').remove();
				$('#'+ID+' .berza_elements_info').load("js/php/berza_pisi_zvezde.php", {'IDponude': ID});
				$(this).slideDown("slow");
			
			});
			
		}
	
	});
	
	$(".reg_discount").tipTip({defaultPosition: "right", edgeOffset:8, maxWidth:"350px"});
	
	$(".super_discount").tipTip({defaultPosition: "right", edgeOffset:8, maxWidth:"350px"});
	
	$('#offerlink').live('click', function(e) {
		e.preventDefault();
		if($('.youroffer').is(":visible")) {
			$('.youroffer').slideUp("slow");
		} else {
			$('.youroffer').load("js/php/berza_offer_form.php", function() {
				$(this).slideDown("slow");
			});
		}
	});
	
	$('.reg_form_berza').live('click', function(e) {
		e.preventDefault();
		if($('.youroffer').is(":visible")) {
			$('.youroffer').slideUp("slow");
		} else {
			$('.youroffer').load("js/php/berza_offer_form.php", function() {
				$(this).slideDown("slow");
			});
		}
	});
	
	$('.inputOfferSubmit').live('click', function(e) {
		e.preventDefault();
		$.post("js/php/berza_offer_action.php", {
		
				'offer_name': $('textarea[name="offer_name"]').val(),
				'offer_desc': $('textarea[name="offer_desc"]').val(),
				'discount': $('input[name="discount"]').val(),
				'special_discount': $('textarea[name="special_discount"]').val(),
				
				'firm': $('input[name="firm"]').val(),
				'firm_desc': $('textarea[name="firm_desc"]').val(),
				'website': $('input[name="website"]').val(),
				'address': $('textarea[name="address"]').val(),
				'phone': $('input[name="phone"]').val(),
				'fax': $('input[name="fax"]').val(),
				'email': $('input[name="email"]').val(),
				'contact': $('input[name="contact"]').val(),
				'position': $('input[name="position"]').val()
			
			
			}, function(data) {
				//alert(data);
		});
		
	});
	
	$('.inputOfferCancel').live('click', function(e) {
		e.preventDefault();
		$('.youroffer').slideUp("slow");
	});
});
