//Google Map with geocoder 
function getMap(locations) 
{
   var map = new GMap2(document.getElementById('maps'));
   map.addControl(new GSmallMapControl());
   var geocoder = new GClientGeocoder();
   var markers = [];
   var count = 0;
   
   var baseIcon = new GIcon(G_DEFAULT_ICON);
   baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
   baseIcon.iconSize = new GSize(20, 34);
   baseIcon.shadowSize = new GSize(37, 34);
   baseIcon.iconAnchor = new GPoint(9, 34);
   baseIcon.infoWindowAnchor = new GPoint(9, 2);

   $.each(locations, function(index, value){
	   geocoder.getLatLng(value.address, function(point) {
		  if(!point) {
			  $('#maps').hide();
			  $('#google_map_link').hide();
		  }
		  else {
		     if(count == 0)
		     {
		       map.setCenter(point, 12);
		     }
		     
		     var letter = String.fromCharCode(value.letter.charCodeAt(0));
		     var letteredIcon = new GIcon(baseIcon);
		     letteredIcon.image = 'http://www.google.com/mapfiles/marker' + letter + '.png';
		     markerOptions = { icon:letteredIcon };
		     var marker = new GMarker(point, markerOptions);
		     map.addOverlay(marker);
			 markers[index] = marker; 
			 GEvent.addListener(marker, "click", function(){
			    map.panTo(marker.getLatLng());  
			 });   
			 count++;
		  }
	   });
   });

   /*$('.location').click(function(){
	  var mark = $(this).attr('id');
	  map.panTo(markers[mark].getLatLng());  
   });*/
}
 
// Countdown for Featured Deal 
function dealTimer(did) 
{	
  $.getJSON(root + '/timer/' + did, function(d){
	 if(d) {
	 	
	  $('#countdown_dashboard, #countdown_dashboard_footer, #countdown_dashboard_split').each(function(){
		$(this).countDown({
		   targetDate: {
		    'day': 	 d.mday,
		    'month': d.mon,
		    'year':  d.year,
		    'hour':  d.hours,
		    'min': 	 d.minutes,
		    'sec':   d.seconds
		   },
		   'omitWeeks': true,
		   onComplete: function(){
			 $('#featured_deal_buy_btn').html("<img src='/livingdeal/images/deal_closed_btn.png' />");
			 $('#buy_now_checkout_btn').html("<img src='/livingdeal/images/deal_closed_large_btn.png' />");
			 $('#feat_overlay').show();
		   }
		});
	  });
	 }
  });
}


function copyDetails(){
  $('#copy_details').click(function() {
	 var name = $('#deal_company_name').val();
	 var address = $('#deal_company_address').val();
	 $('#deal_payee_name').val(name);
	 $('#deal_payee_address').val(address);
  });
}

function setEmailTemplate(id) {
  $('#preview').click(function(){
	  template_name = $('#email_template').val()+' preview';
	  getEmailTemplate(id, '650px', '840px', template_name,$('#side_deal_day').val(),$('#deal_cat_id').val());
  });
	
  $('#source').click(function(){
	  template_name = $('#email_template').val()+' source';
	  getEmailTemplate(id, '650px', '840px', template_name,$('#side_deal_day').val(),$('#deal_cat_id').val());
  });

  $('#send').click(function(){
	  template_name = $('#email_template').val();
	  getEmailTemplate(id, '120px', '300px', template_name,$('#side_deal_day').val(),$('#deal_cat_id').val(),$('#email').val());
  });
}

function getEmailTemplate(id,height,width,type,side_deal_day,category_ids,email) {	
  $.colorbox({
	 href: root_backend + '/preview/'+id+'?type='+type+'&email='+email+'&side_deal_day='+side_deal_day+'&category_ids='+category_ids,
	 transition : 'none',
	 opacity : '.60',  
	 width: width,
	 height: height,
	 title: type,
	 iframe: true
  });
}



function datePicker() {
	$('#datepicker_from').datepicker(
			{maxDate: '-1',
		     disabled: true});
	$('#datepicker_to').datepicker(
			{maxDate: '0',
		     disabled: true});
}

function toggleFaq() {
	$('.a').toggle('fast');
    $('.q').click(function() {
	  var num = $(this).attr('id');
	  $('#a'+num).toggle('fast');
    });
}
