function play_movie(movie_path){
	var so = new SWFObject(player_path, "movie", "367", "215", "8", "#ffffff");
	so.addParam("flashvars",'src=' + movie_path);
	so.addParam("scale", "noscale");
	so.addParam("allowfullscreen", "true");
	so.addParam("menu", "false");
	so.write("player");
	$('.bigpic img').fadeOut('slow',function(){
		$('#player').fadeIn('slow');
	});
}

$(document).ready(function(){
	//item's gallery
	$('.gallery  .thumbs img,.gallery .thumbs .player-btn').click(function(){
		if($(this).is('.flv_item')){
			var movie_path = $(this).attr('flv');
			play_movie(movie_path);
			return true;	
			
		}else if($(this).is('.player-btn')){
			
			var movie_path = $(this).parent().next('img').attr('flv');
			play_movie(movie_path);
			return true;
		}
		
		
		var path = $(this).attr('src').replace('_thumb','');
		$('#player').fadeOut('fast');
		$('.gallery > .bigpic').find('img').fadeOut('slow').attr('src',path).load(function(){
			$(this).fadeIn('slow');
		});
	}).hover(function(){
		$(this).fadeTo('fast','0.5');
	},function(){
		$(this).fadeTo('fast','1');
	});
	
	$('.add_comment').click(function(){
		$('#add_comment_form').slideToggle('slow');
		$('.send_form_holder').fadeOut('slow');
	});
	
	if(typeof console != 'undefined') {
		console.log('Hier!');
	}
	$('.real-estate-contact').click(function(){
		if(typeof console != 'undefined') {
			console.log('Here');
		}
		var contact_div = $('.contact-div');
		
		if(contact_div.is(':hidden')){
			var pos = $(this).offset();
			contact_div.remove().appendTo($('body')).css({
				top:pos.top + 20,
				left:pos.left
			}).fadeIn('slow').find('#contact_form').validate({
				errorPlacement: function(error, element) {
			     	error.appendTo(element.parent("td").next("td"));
			   }
			   ,submitHandler:function(form){
			   		$(form).ajaxSubmit({
			   			url: 'http://'+document.domain+_dir_root + '/_ajax/ajax.index.php',
						type:'post',
						success :  function(data,status){
							alert(data);
							$('.contact-div').fadeOut('slow').resetForm();
						}
			   		});
			   		//prevent un-ajax submitting 
			   		return false;
			   }
			});
		}else{
			contact_div.fadeOut('slow');
		}
	});
	
	$(document).keypress(function(e){
		if(e.keyCode == 27){//esc
			$('.contact-div').fadeOut('slow');
		}
	});
	
	$('.comment_btns > img').hover(function(){
		$(this).fadeTo('fast','0.6');
	},function(){
		$(this).fadeTo('fast','1');	
	});
	
	//comment form submition
	$('.commentFrm').submit(function(){
		if($(this).find('[name=user_name]').val()==''){
			alert('Please enter your name.');
			return false;
		}
		if($(this).find('[name=title]').val()==''){
			alert('Please enter a title.');
			return false;
		}
		return true;
	});
	
	//comments slide
	$('.init_display').click(function(){
			$(this).hide().next('.open_display').show();
	});
	
	$('.open_display > .inner_comment').click(function(){
			$(this).parent().hide().prev('.init_display').show();
	});
	
	$('.add_respond').click(function(){
		$(this).parent().next('.comment_form').slideToggle('slow');
	});
	
	//share an item
	$('.send_to_friend').click(function(){
		var form = $('.send_form_holder');
		
		if(form.is(':hidden')){
			$('.send_form_holder').fadeIn('slow');
			$('#add_comment_form').slideUp('slow');
		}else{
			$('.send_form_holder').fadeOut('slow');
		}
	});
	
	$('.send_to_friend_form').ajaxForm({
		url:_dir_root  + '/_ajax/ajax.index.php',
		type:'post',
		beforeSubmit:function(formData, jqForm, options){
			formData = jqForm[0];
			if(formData.yourname.value == ''){
				alert('Please enter your name.');
				formData.yourname.focus();
				return false;
			}
			
			if(!is_email(formData.youremail.value)){
				alert('Please enter your email');
				formData.youremail.focus();
				return false;
			}
			
			if(formData.friendname.value == ''){
				alert('Please enter friend\'s name.');
				formData.friendname.focus();
				return false;
			}
			
			if(!is_email(formData.friendemail.value)){
				alert('Please enter friend\'s email');
				formData.friendemail.focus();
				return false;
			}
			$('.send_form_holder').fadeOut('slow',function(){
				$('.send_to_friend_form').resetForm();
			});
			return true;
		},
		success :  function(data,status){
			alert(data);
		}
	});
	
	$('.print').click(function(){
		var url = $(this).attr('id');
		window.open(_dir_root + '/' + url,'print_view','width=600,height=800');
	});
	
	function report(){
		report = $(this).unbind('click',report);
		var cid = report.html('Hang on...').attr('id').split('_').slice(-1);
		$.ajax({
			url: 'http://'+document.domain+_dir_root + '/_ajax/ajax.index.php',
			type : 'post',
			data:{
				file:'comments.report.ajax.php',
				id:cid
			},
			success:function(){
				report.fadeOut('fast',function(){
					$(this).html('<b>Thank you for your report.</b>');
				})
				.fadeIn('slow')
				.css({
					'text-decoration':'none',
					'cursor':'auto'
				});
			}
		});
		
	}
	
	$('.report').click(report);
});