function init_colorbox(){
	if($('.thumb').length){
		$('.thumb').colorbox();
	}
}

function init_slideshow(){
	var so = new SWFObject("/flash/monoslideshow.swf", "SOmonoSlideshow", "538", "252", "7", "#ffffff");
	so.addVariable('showLogo', 'false');
	so.addVariable('showVersionInfo', 'false');
	so.addVariable('dataFile', '/slideshows/home');
	so.write("slideshow_inner");
}

function init_msg_box(){

	if($('#box_msg').is(':visible')){
		msg_box();
	}
}

function msg_box(){
	$('#box_msg_inner').meerkat({
		position: 'bottom',
		animationIn: 'slide',
		animationOut: 'slide',
		animationSpeed: 500,
		timer: 10
	});
}

function nextPhoto(){

	var $f = $('#listing_photo_holder .photos');
	var $p = null;

	var $c = $('#listing_photo_holder DIV.active').attr('id').split('_')[2];

	if($c==$f.length){
		$c=1;
		$p='0px';
	} else {
		$c++;
		$p='-=265px';
	}

	$('#listing_photo_holder').animate({
			marginLeft: $p
		}, 'slow', function(){
			$($f).removeClass('active');
			$('#listing_photo_'+$c).addClass('active');
		}
	);
}
function previousPhoto(){

	var $f = $('#listing_photo_holder .photos');
	var $p = null;

	var $c = $('#listing_photo_holder DIV.active').attr('id').split('_')[2];

	if($c==1){
		$c=$f.length;
		var position = -(($f.length-1)*265);
		$p=position+'px';
	} else {
		$c--;
		$p='+=265px';
	}

	$('#listing_photo_holder').animate({
			marginLeft: $p
		}, 'slow', function(){
			$($f).removeClass('active');
			$('#listing_photo_'+$c).addClass('active');
		}
	);
}

var geocoder;
var map;

function init_map() {

	if($('#listing_map').length){

		var latlng = new google.maps.LatLng(35.13705, -120.621602);
	    var myOptions = {
	      zoom: 15,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
    	map = new google.maps.Map(document.getElementById("listing_map"), myOptions);
        var marker = new google.maps.Marker({
            map: map, 
            position: latlng
        });

	   markAddress();
	}
}
function markAddress() {

	geocoder = new google.maps.Geocoder();
	var address = $("#listingAddress").val();

    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
}

function email($id){
	
	var triggers = $("#email_"+$id).overlay({

        mask: {
            color: '#ebecff',
            loadSpeed: 200,
            opacity: 0.7,
        },
        closeOnClick: false,
        onBeforeLoad: function() {
        	item_id = this.getTrigger().attr('id').split('_')[1];
        	item_type = this.getTrigger().attr('id').split('_')[0];
    	}
    });

    $("#email_"+$id).overlay().load();
}

function value_send(){
	$('#ValuationAddForm').submit();
}

function value_next(){

	var $f = $('#valuation .frame');
	var $p = null;

	var $c = $('#valuation DIV.active').attr('id').split('_')[1];

	if($c==$f.length){
		$c=1;
		$p='0px';
	} else {
		$c++;
		$p='-=848px';
	}

	$('#valuation_holder').animate({
			marginLeft: $p
		}, 'slow', function(){
			$($f).removeClass('active');
			$('#step_'+$c).addClass('active');
		}
	);
}
function value_back(){

	var $f = $('#valuation .frame');
	var $p = null;

	var $c = $('#valuation DIV.active').attr('id').split('_')[1];

	if($c==0){
		$c=3;
		$p='-1696px';
	} else {
		$c--;
		$p='+=848px';
	}

	$('#valuation_holder').animate({
			marginLeft: $p
		}, 'slow', function(){
			$($f).removeClass('active');
			$('#step_'+$c).addClass('active');
		}
	);
}

var submenu_open = false;

function submenu_toggle(name) {

	var $div = $('#submenu_'+name);

	if(submenu_open){
		if ($div.is(":hidden")) {
			$('#nav_'+submenu_open+' A').removeClass('clicked');
			$('#nav_'+name+' A').addClass('clicked');
			
			$('#submenu_'+submenu_open).slideUp("fast", function(){
				$div.slideDown("slow");
			});
		} else {
			$('#nav_'+name+' A').removeClass('clicked');
			$('#submenu_'+submenu_open).slideUp("fast", function(){
				$div.slideUp("fast");
			});	
		}
		
	} else {
		$('#nav_'+name+' A').addClass('clicked');
		$div.slideDown("slow");
	}

	submenu_open = name;
}

function attach_submit_review_click(){
	$('#box_reviews #btn_save').click(function(){
		submit_review();
	});
}

function submit_review(){
	$.ajax({
		url: "/reviews/add",
		type: "POST",
		data: $('#box_reviews FORM').serialize(),
		success: function(){
			$('#box_reviews .box_inside').html("<p class='msg'>You review has been sent.  Thanks!</p>");
		},
		complete: function(){
			
		},
		error: function(data){
			alert("your review couldn't be saved");
		}
	});
}

function init_placeholder_fix(){
	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
	    input.val('');
	    input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
	    input.addClass('placeholder');
	    input.val(input.attr('placeholder'));
	  }
	}).blur();
}

$(document).ready(function(){

	init_map();
	init_msg_box();
	init_slideshow();
	init_colorbox();
	init_placeholder_fix();
	attach_submit_review_click();

});
