;(function($) {

  MyWheel.cookies = {
    _defaults: {
      expires_in: 360, // days
      path: '/'
    },
    
    data: {},

    init: function() {
      // .*? - lazy algorithm
      var cookie_match = document.cookie.match(/json_cookies=(.*?)(;|$)/);
      if(cookie_match) {
        var json_data = cookie_match[1];
        this.data = $.parseJSON(decodeURIComponent(decodeURIComponent(json_data)));
      };
    },
    save: function(given_options) {
      var options = $.extend({}, this._defaults, given_options);
      var expiration_date = new Date();
      expiration_date.setDate(expiration_date.getDate() + options.expires_in);
      var expiration_date = expiration_date.toUTCString();
      document.cookie = 'json_cookies=' + encodeURIComponent(encodeURIComponent($.toJSON(this.data))) + ';path=' + options.path + ';expires=' + expiration_date;
    }
  };

  MyWheel.cookies.init();
 
})(jQuery);
String.prototype.capitalize = function() {
  return this.slice(0,1).toUpperCase() + this.slice(1);
};

String.prototype.escape_html = function() {
  this.replace(/&/g, '&amp;'); 
  this.replace(/"/g, '&quot;'); 
  this.replace(/>/g, '&gt;'); 
  this.replace(/</g, '&lt;'); 
  return this;
};

Array.prototype.remove = function(value) {
  var new_array = []; 
  for(var i = 0; i < this.length; i++) {
    if(this[i] != value) {
      new_array.push(this[i]);
    };
  };
  return new_array;
};


(function() { 

  MyWheel.activate_runners = function() {
    var template = MyWheel.templates[MyWheel.controller][MyWheel.action];
    var blocks = template.blocks;
    var mixins = template.mixins;
    for(var i = 0; i < blocks.length; i++) {
      MyWheel.runners.blocks[blocks[i]](); 
    };
  };

})();

(function() {

  MyWheel.templates = {

	home: {
      index: {
        blocks: [ 'wheel', 'popup_birthdate_changer' ]
      }
    }
  };

})();



(function($) {
  
  var classes = {
    self: 'b-wheelmain',
    another_sign: 'b-wheelmain_another-sign',
    western_sign: 'b-wheelmain_western-sign',
    big: 'b-wheelmain_big',
    current_sign: 'b-wheelmain_current-sign',
    current_type: 'b-wheelmain_current-type',
    horoscopes: 'horoscopes',
    header: 'b-wheelmain_header',
    sign: 'sign',
    selected_horoscopes: 'selected-horoscopes',
    selected_sign: 'selected-sign'
  };

  var types = {
    'city-break': 'CityBreak',
    'croaziere': 'Croaziere',
    'sejur-plaja': 'SejurPlaja',
    'sejur-romantic': 'SejurRomantic',
    'spa': 'SPA',
	'vacante': 'Vacante'
  }

  $.fn.b_wheel_homepage = function() {
    return this.each(function() {
      var self = $(this);
      init_sign_select(self);
    });
  };


  $.fn.b_horoscopes_set_current_horoscope = function(name) {
	  // MAKE DIVS
	
	if(firstp == 1){
		var luna = document.forms['wheel'].elements['luna'].value;
		var anul = document.forms['wheel'].elements['anul'].value;
		var ziua = document.forms['wheel'].elements['ziua'].value;
		window.location = 'http://horoscopulcalatoriilor.perfect-tour.ro/?anul='+anul+'&luna='+luna+'&ziua='+ziua;
	} else {
		document.getElementById('city-break').style.display = 'none';
		document.getElementById('croaziere').style.display = 'none';
		document.getElementById('sejur-plaja').style.display = 'none';
		document.getElementById('sejur-romantic').style.display = 'none';
		document.getElementById('spa').style.display = 'none';
		document.getElementById('vacante').style.display = 'none';
		document.getElementById(name).style.display = 'block';
	}
  };
 
})(jQuery);




// Description of the block. Please, show example of HTML of the block here
;MyWheel.runners.blocks.horoscopes_horoscopes = function() {
  jQuery('#b-wheelmain_horoscopes').b_horoscopes_horoscopes();
};

(function($) {
  
  var classes = {
    western_sign: 'b-wheelmain_western-sign',
    spinner: 'b-wheelmain_spinner',
    yesterday: 'b-wheelmain_other-date_yesterday',
    tomorrow: 'b-wheelmain_other-date_tomorrow',
    featured: 'b-wheelmain_featured'
  };


  var trackings = {
    sign_select: 'ast:horoscopes:hero-signdd',
    date_click: 'ast:horoscopes:hero-anotherdate',
    featured_link_click: 'ast:horoscopes:hero-miniperslist'
  }

    
  $.fn.b_horoscopes_horoscopes = function() {
    return this.each(function() {
      var self = $(this);
      init_sign_select(self);
      init_links_tracking(self);
    });
  };


  function init_sign_select(self) {
    var western_sign = self.find('.' + classes.western_sign);
    western_sign.change(function() { 
      var value = $(this).val();
      if(value == "") { return false; };
      self.find('.' + classes.spinner).css('display', 'inline-block');
      return false;
    });
  };


  function init_links_tracking(self) { //mAGIC

  }

 
})(jQuery);



// Popup window for changing user's birthdate or looking at friend's birthdate (p.91 of F.Spec)
// You have to pass 'opener' to b_popup_birthdate_changer (jQuery object, the parent container 
// of the b-popup-birthdate-changer block, if user clicks on them, the window will be opened).
;MyWheel.runners.blocks.popup_birthdate_changer = function() {
  var kaleidoscope = jQuery('#b-wheel');
  kaleidoscope.find('.b-popup-birthdate-changer').b_popup_birthdate_changer({
    opener: kaleidoscope.find('.b-wheel_date-container')  
  });

  var hero_numerology = jQuery('#b-hero');
  hero_numerology.find('.b-popup-birthdate-changer').b_popup_birthdate_changer({
    opener: hero_numerology.find('.b-hero_birthdate_edit')  
  });
};

(function($) {
  
  var classes = {
    close_text: 'b-popup-birthdate-changer_close-text',
    close_sign: 'b-popup-birthdate-changer_close-sign'
  };

  $.fn.b_popup_birthdate_changer = function(options) {
    return this.each(function() {
      var self = $(this);
      init(self, options);
    });
  };


  function init(self, options) {
    options.opener.click(function() { self.parent().show(); return false; });
    self.find("." + classes.close_sign).click(function() { self.parent().hide(); return false; });
    self.find("." + classes.close_text).click(function() { self.parent().hide(); return false; });
  };

})(jQuery);
// Description of the block. Please, show example of HTML of the block here
;MyWheel.runners.blocks.products_form = function() {
  jQuery('#b-products_form').b_products_form();
};

(function($) {
  
  var classes = {
    self: 'b-products_form',
    for_who: 'b-products_form_for-who',
    for_you: 'b-products_form_for-you',
    not_for_you: 'b-products_form_not-for-you',
    your_info: 'b-products_form_your-info',
    popup: 'b-products_form_popup'
  };
  
  $.fn.b_products_form = function() {
    return this.each(function() {
      var self = $(this);
      init_you_not_you_switcher(self);
      init_description_popups(self);
    });
  };

  function init_description_popups(self) {
    self.find('.row').hover(
      function() {
        $(this).find('.' + classes.popup).css('opacity', 0).show().animate({'opacity': 1}, "fast");
      },
      function() {
        $(this).find('.' + classes.popup).animate({'opacity': 0}, "fast");
      }
    );
  };

  function init_you_not_you_switcher(self) {
    var for_you = self.find('.' + classes.for_you);
    var not_for_you = self.find('.' + classes.not_for_you);
    var your_info = self.find('.' + classes.your_info);

    show_or_hide_your_info(for_you, your_info);
    for_you.click(function() { show_or_hide_your_info(for_you, your_info); });
    not_for_you.click(function() { show_or_hide_your_info(for_you, your_info); });
  };

  function show_or_hide_your_info(for_you, your_info) {
    if(for_you.get(0).checked) {
      your_info.hide();
      your_info.find(':input').each(function() { this.disabled = true; });
    } else {
      your_info.show();
      your_info.find(':input').each(function() { this.disabled = false; });
    };
  };

})(jQuery);

// Scopes slider. 
;MyWheel.runners.blocks.scopes_slider = function() {
  jQuery('#b-scopes-slider').b_scopes_slider();
};

(function($) {
 
  var raw_width_of_element = 165;
  var margin_of_element = 20;
  var width_of_element = raw_width_of_element + margin_of_element;
  var number_of_visible_elements = 4;

  var classes = {
    self: 'b-scopes-slider',
    edit: 'b-scopes-slider_gallery_edit',
    edit_button: 'b-scopes-slider_edit-button',
    tooltip: 'b-scopes-slider_gallery_tooltip',
    list: 'b-scopes-slider_gallery_list',
    item: 'b-scopes-slider_gallery_list_item',
    item_header: 'b-scopes-slider_gallery_list_item_header',
    item_header_title: 'b-scopes-slider_gallery_list_item_header_title',
    item_content: 'b-scopes-slider_gallery_list_item_content',
    item_tooltip: 'b-scopes-slider_gallery_list_item_header_tooltip',
    popup: 'b-scopes-slider_gallery_popup',
    popup_wrapper: 'b-scopes-slider_gallery_popup_wrapper',
    prev: 'b-scopes-slider_gallery_prev',
    next: 'b-scopes-slider_gallery_next',
    delete_button: 'b-scopes-slider_delete',
    save: 'b-scopes-slider_save',
    cancel: 'b-scopes-slider_cancel',
    opened: 'b-scopes-slider_opened',
    remove: 'b-scopes-slider_gallery_remove',
    remove_title: 'b-scopes-slider_gallery_remove_title',
    remove_okay: 'b-scopes-slider_gallery_remove_okay',
    read_more: 'b-scopes-slider_gallery_list_item_content_more'
  };

  function visible_items(self, list) {
    var list = list || self.find('.' + classes.list);
    var items = list.children('.' + classes.item + ':visible');
    var offset = parseInt(list.css('left'));
    var first_item = -offset / width_of_element;
    visible_items = items.slice(first_item, first_item + 4);
    return visible_items;
  };


  function init_popups(self) {
    self.find('.' + classes.item_header).click(function() {
      open_popup(self, $(this).parent()); return false;
    });
    self.find('.' + classes.item_header + ' .' + classes.delete_button).click(function() {
      remove_item(self, $(this).closest('.' + classes.item)); return false;
    });
  };




  function init_tooltip(self) {
    self.find('.' + classes.item_header + ' .' + classes.delete_button).hover(
      function() { show_tooltip(self, $(this).closest('.' + classes.item)); },
      function() { hide_tooltip(self, $(this).closest('.' + classes.item)); }
    );
  };

  function init_trackings(self) {
    //magic
  }


  function open_popup(self, item) {
    if(self.data("processing") == false) {
      var popup = self.find('.' + classes.popup);
      var popup_wrapper = popup.find('.' + classes.popup_wrapper);
      // At first, close currently opened window (if it is presented)
      close_popup(self, popup, function() {
        // Then, continue to open current window
        self.data("processing", true);
        popup_wrapper.empty()
        popup.show();
        item.addClass(classes.opened);
        var content = item.find('.' + classes.item_content);
        var new_content = content.clone().appendTo(popup_wrapper).css("opacity", 0);
        var left = (width_of_element / 2) + (width_of_element * get_index_of_item(self, item));
        popup.css("left", left + "px").css("top", "120px");
        popup.animate({width: "410px", height: "240px", top: "-=120px", left: "-=205px"}, "fast", "swing", function() {
          new_content.animate({opacity: 1}, "fast", "linear", function() {
            self.data("processing", false);
            popup.find('.' + classes.delete_button).click(function() {
              close_popup(self, popup); 
              return false;
            });
          });
        });
      });
    };
  };


  function close_popup(self, popup, callback) {
    if(self.data("processing") == false) {
      self.data("processing", true);
      if(popup.is(':visible')) {
        var popup_wrapper = popup.find('.' + classes.popup_wrapper);
        var content = popup_wrapper.children('.' + classes.item_content);
        content.animate({opacity: 0}, "fast", "linear", function() {
          popup.animate({width: "0px", height: "0px", top: "+=120px", left: "+=205px"}, "fast", "swing", function() {
            popup.hide();
            self.find('.' + classes.opened).removeClass(classes.opened);
            self.data("processing", false);
            if(callback) { callback.call(popup); };
          });
        });
      } else {
        if(callback) { callback.call(popup); };
      };
    };
  };




  function show_tooltip(self, item) {
    var tooltip = self.find('.' + classes.tooltip);
    var item_tooltip = item.find('.' + classes.item_tooltip);
    var left = (width_of_element - 65) + (width_of_element * get_index_of_item(self, item));
    tooltip.empty().html(item_tooltip.html());
    tooltip.css('opacity', 0).css('left', left + "px").show().animate({'opacity': 1}, "fast");
  };


  function hide_tooltip(self, item) {
    var tooltip = self.find('.' + classes.tooltip);
    tooltip.animate({'opacity': 0}, "fast", "linear", function() { $(this).hide(); });
  };


  function remove_item(self, item) {
    show_remove_confirmation(self, item, function() {
      hide_item(self, item, function() {
        var list = self.find('.' + classes.list);

        var index = item.attr('class').match(new RegExp(classes.item + "_(\\d+)"))[1]; 
        MyWheel.cookies.data.horoscopes.remove(index);
        MyWheel.cookies.save();
        self.find('#selected_horoscope_' + index).get(0).checked = false;

        var current_left = parseInt(list.css('left'));
        make_list_continuous(self, list, current_left);
      });
    });
  };


  function hide_item(self, item, callback) {
    item.animate({'opacity': 0, 'width': 0}, "fast", "linear", function() { 
      $(this).hide(); 
      if(callback) { callback.call(self); };
    });
  };

})(jQuery);



;MyWheel.runners.blocks.wheel = function() {
  jQuery('#b-wheel').b_wheel();
};

(function($) {
  
  var classes = {
    self: 'b-wheel',
    wheel: 'b-wheel_wheel',
    date_container: 'b-wheel_date-container',
    date_tooltip: 'b-wheel_date-container_tooltip',
    tooltip: 'b-wheel_tooltip',
    birthday_change: 'b-wheel_birthday-change',
    label: 'b-wheel_label'
  };

  var distance_array = [
    [360,60,120,180,240,300],
    [300,360,60,120,180,240],
    [240,300,360,60,120,180],
    [180,240,300,360,60,120],
    [120,180,240,300,360,60],
    [60,120,180,240,300,360]
  ];

  var tooltips = ['','','','','',''];
    
  $.fn.b_wheel = function(options) {
    var opts = $.extend({}, $.fn.b_wheel.defaults, options);
    return this.each(function() {
      var self = $(this);
      init_variables(self, opts);
      init_wheel(self);
      init_random_spinning(self, self.data("wheel"));
	  init_birthday_changing(self);
      set_index(self, self.data("wheel"), 0);
    });
  };


  function init_variables(self, opts) {
    self.data("is_wheel_running", false);
    self.data("current_rotation", null);
    self.data("selected_index", 0);
    self.data("selected_sign", 0);
    self.data("wheel", {});
    self.data("wheel_cycles", opts.wheel_cycles);
  };


  function init_wheel(self) {
    var wheel_dom = self.find("." + classes.wheel).get(0);
    var wheel = {};
    wheel.raphael = Raphael(wheel_dom, 320, 320);
    wheel.image = wheel.raphael.image("../images/h/wheelImage.png", 0, 0, 312, 312);	
    wheel.selected_icon_bg = init_selected_icon_bg(wheel);
    wheel.icon_set = init_icon_set(self, wheel);
    wheel.selected_icon_set = init_selected_icon_set(self, wheel);
    var circle_pointer = wheel.raphael.image("../images/h/circlePointer.png", 104, 105, 113, 103);
    
    self.data("wheel", wheel);
  };

  
  function init_random_spinning(self, wheel) {
    var label = self.find('.' + classes.label);
    label.click(function() { 
      var wheel_length = 6;
      var index = Math.round(Math.random() * (wheel_length - 1));
      rotate(self, wheel, index, 2000, ">");
      return false;
    });
  };


  function rotate(self, wheel, index, time, easing) {
    wheel.selected_icon_bg.hide();
    wheel.selected_icon_set.hide();
    wheel.icon_set.show();
    var angle = distance_array[self.data("selected_index")][index];
    
    if(!self.data("is_wheel_running")) {
      self.data("is_wheel_running", true);
      var angle_with_cycles = angle + self.data("wheel_cycles");
      self.data("current_rotation", wheel.image.rotate());
      
      wheel.image.animate(
        { rotation: [wheel.image.rotate() + angle_with_cycles, 156, 156] },
        time,
        easing,
        function() { finish_animation(self, wheel, index); }
      );
      wheel.icon_set.animate(
        { rotation: [wheel.image.rotate() + angle_with_cycles, 156, 156] }, 
        time, 
        easing
      );				
    };
    wheel.raphael.safari();
  };
  
  function init_birthday_changing(self) {
    var tooltip = self.find("." + classes.date_tooltip);
    var date_container = self.find("." + classes.date_container);
    date_container.hover(function() { tooltip.fadeIn(); }, function() { tooltip.fadeOut(); });
  };


  function init_selected_icon_bg(wheel) {
    var selected_icon_bg = wheel.raphael.image("../images/h/selected.png", 155, 83, 146, 148);
    selected_icon_bg.hide();
    return selected_icon_bg;
  };


  function init_icon_set(self, wheel) {
    var icon_set = wheel.raphael.set();
    icon_set.push(
      wheel.raphael.image("../images/wh_city-break.png", 212, 120, 70, 75),
      wheel.raphael.image("../images/wh_croaziere.png", 168, 40, 70, 50),
      wheel.raphael.image("../images/wh_sejur_plaja.png", 75, 34, 70, 75),
      wheel.raphael.image("../images/wh_sejur_romantic.png", 30, 115, 70, 58), 
      wheel.raphael.image("../images/wh_spa.png", 76, 193, 70, 61),
      wheel.raphael.image("../images/wh_vacante_lux.png", 166, 198, 52, 70)
    );
    var tooltip = $('.' + classes.tooltip);
    $(icon_set).each(function(index) {
      icon_set[index].click(function() {
        rotate(self, wheel, index, 2000, ">");
      });
    });
    return icon_set;
  };


  function init_selected_icon_set(self, wheel) {
    var selected_icon_set = wheel.raphael.set();
    selected_icon_set.push(
      wheel.raphael.image("../images/wh_city-break_on.png", 212, 120, 70, 75),
      wheel.raphael.image("../images/wh_croaziere_on.png", 212, 120, 70, 50),
      wheel.raphael.image("../images/wh_sejur_plaja_on.png", 212, 120, 70, 75),
      wheel.raphael.image("../images/wh_sejur_romantic_on.png", 212, 120, 70, 58),
      wheel.raphael.image("../images/wh_spa_on.png", 212, 120, 70, 61),
      wheel.raphael.image("../images/wh_vacante_lux_on.png", 212, 120, 52, 70)				
    );
    selected_icon_set.hide();
    return selected_icon_set;
  };


  function finish_animation(self, wheel, index) {	
    set_index(self, wheel, index);
    self.data("is_wheel_running", false);
    change_to_selected_zone(self, index);
  };

  function set_index(self, wheel, index) {
    wheel.selected_icon_bg.show();
    wheel.icon_set[index].hide();
    wheel.selected_icon_set[index].show();
    self.data("selected_index", index);
  };

  function change_to_selected_zone(self, index) {
    var name;
    switch(index) {
      case 0: name = 'city-break'; break;
      case 1: name = 'croaziere'; break;
      case 2: name = 'sejur-plaja'; break;
      case 3: name = 'sejur-romantic'; break;
      case 4: name = 'spa'; break;
      case 5: name = 'vacante'; break;
    }
    self.parent().parent().find('.b-wheelmain').b_horoscopes_set_current_horoscope(name);
  };
 

  $.fn.b_wheel.defaults = {
    // set to 360 * (number of desired rotations)
    wheel_cycles: 360 * 2
  };

})(jQuery);


;MyWheel.runners.blocks.popup_birthdate_changer = function() {
  var kaleidoscope = jQuery('#b-wheel');
  kaleidoscope.find('.b-popup-birthdate-changer').b_popup_birthdate_changer({
    opener: kaleidoscope.find('.b-wheel_date-container')  
  });

  var hero_numerology = jQuery('#b-hero');
  hero_numerology.find('.b-popup-birthdate-changer').b_popup_birthdate_changer({
    opener: hero_numerology.find('.b-hero_birthdate_edit')  
  });
};

(function($) {
  
  var classes = {
    close_text: 'b-popup-birthdate-changer_close-text',
    close_sign: 'b-popup-birthdate-changer_close-sign'
  };

  $.fn.b_popup_birthdate_changer = function(options) {
    return this.each(function() {
      var self = $(this);
      init(self, options);
    });
  };


  function init(self, options) {
    options.opener.click(function() { self.parent().show(); return false; });
    self.find("." + classes.close_sign).click(function() { self.parent().hide(); return false; });
    self.find("." + classes.close_text).click(function() { self.parent().hide(); return false; });
  };

})(jQuery);

