// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

site = {

  init : function() {
    $('#screenshots li.screenshot a').fancyZoom({scaleImg: false, closeOnClick: false, directory: '/images/site/fancyzoom/'});    
    $('#screenshots li.screencast a').fancyZoom({scaleImg: false, closeOnClick: false, directory: '/images/site/fancyzoom/', afterZoom: function(){ init_screencast_iframe() }});    
    dropdown.init('#cities');		
    email_subscribe.init();		 
    $("select, input").uniform();
    $('.grey-out').css('opacity', 0.35);    

		// wrapper height
		resize_wrapper();
		// window.onresize = resize_wrapper;
  }

} 

function resize_wrapper() {
	if($(window).height() > $("#wrap").height()) {
		$("#wrap").height($(window).height() - 70);	
	}	
}

function init_screencast_iframe() {    
  var iframe = $("#zoom_content #screencast_iframe");
  iframe.load(function() {
    start_player(iframe);
  });
} 

function start_player(iframe, src) {
  var e = $(iframe).contents().find('div#video');
  var embeded = $(e).find('embed, object');
  embeded.remove();

  $(e).flash({
     swf: '/images/site/swf/player.swf',
     params: {
       wmode: 'transparent'
     },  
      flashvars: {
        'file': SCREENCAST_FLV,
        'controlbar': 'none',
        'autostart': true            
      },               
     width: 320,
     height: 487
   }
  );  
}

email_subscribe = {

  init : function() {
    $('form#email-subscribe, form#city-vote').submit(function() {

      var form = this;
      var messages_container = $(this).find('div.messages'); 
      $(messages_container).empty(); 

      $.ajax({
        type: 'GET',
        dataType: 'json',
        url: $(this).attr('action'),
        data: $(this).serialize(),
        complete : function(response) {        
          email_subscribe.validate_response(response.responseText, messages_container, form);
        }
      });

      return false;
    });
  },

  validate_response: function(data, messages_container, form) {
    json = jQuery.parseJSON(data);
    var status = json[0].status;

    if(status == false) {
      // append all errors to error container
      $(messages_container).append(json[0].message.toString());
    } else {
      // hide elements and inject success message
      var el = $(form).find('input, label, select, .selector');
      $(el).fadeOut('slow', function() {
        $(messages_container).text(json[0].message);  
      });      
    }
  }                              


}


/*
Main navitation drop down system
Selected with : ul#nav > li
*/

dropdown = {

  init : function(sel) {

    $(sel).mouseover(
      function() { dropdown.over(this); }
    );	             

    $(sel).mouseleave(
      function() { dropdown.out(this); } 
    );  

  },

  over : function(e) {
    var content = $(e).find('ul');
    content.show();
  },

  out : function(e) {
    var content = $(e).find('ul');
    content.hide();
  }	
}

