 /**
 * @fileoverview Common Javascript Functionality for Arctic Computer Shop
 * for reusable javascript functionality
 * JavaScript.
 * @copyright Copyright 2011, NssY Wanyonyi <wanyonyi@hasoftkenya.com>
 * @author NssY Wanyonyi
 * @version 1.0
 * Date Created: 16th June 2011
 * Date Revised: 29th July 2011
 */

// Create Beautiful Tooltips on hover
function simple_tooltips(elems){
  var h = jQuery(window).height();
  var w = jQuery(window).width();
  var offsetY = 20 , offsetX = 20;
  var html = '<div class="tooltip_window rounder shadow fancybg" style="position:absolute" />';
  jQuery('body').append(html).children(".tooltip_window").hide();
  var tt_win = jQuery('.tooltip_window');
  elems.each(function(){
    var me = jQuery(this);
    me.hover(
    // Mouse Over
    function(evt){
      var h = me.css('cursor', 'pointer').attr('tooltip');
      evt.stopPropagation();
      tt_win.hide().html(h)
        .css('top', evt.pageY + offsetY)
        .css('left', evt.pageX + offsetX)
        .fadeIn(500);
      var tw = tt_win.outerWidth();
      var th = tt_win.outerHeight();
      me.mousemove(function(evt){
        var tX = tw + offsetX + evt.pageX;
        var tY = evt.pageY + offsetY;

        // We need to make sure our tooltip window is not outside the page
        var posX;
        if (tX > w){
          posX = -tw - offsetX;
        } else {
          posX = offsetX;
        }
        var posY;
        if (tY > h){
          posY = th;
        }else{
          posY = offsetY+th;
        }
        tt_win
        .css('top', evt.pageY - posY)
        .css('left', evt.pageX + posX);
      });
    },
    // Mouse Out
    function(){
      tt_win.empty();
    });
  });
}
// Set the equal height of a group of elements
function equalHeight (group) {
  var tallest = 0;
  var h;
  group.each(function() {
    h = jQuery(this).height();
    tallest = Math.max(h, tallest);
  });
  group.height(tallest);
}
// Display a map of our current Location
function init_location() {
  var latlng = new google.maps.LatLng(-1.2829596435698636, 36.82146191596985)
  var settings = {
    zoom: 16,
    center: latlng,
    mapTypeControl: true,
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
    navigationControl: true,
    navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
    mapTypeId: google.maps.MapTypeId.ROADMAP};
  var map = new google.maps.Map(document.getElementById("arctic-map"), settings);
  var contentString = '<div id="content">'+
    '<div id="siteNotice">'+
    '</div>'+
    '<h1 id="firstHeading" class="firstHeading">Arctic Computer Shop</h1>'+
    '<div id="bodyContent">'+
    '<p>2nd floor Kenwood House, Kimathi Street<br> Phone: +254 20 315138, Mobile: +254 722 267 947</p>'+
    '</div>'+
    '</div>';
  var infowindow = new google.maps.InfoWindow({
    content: contentString
  });

  var companyImage = new google.maps.MarkerImage('images/map/logo.png',
    new google.maps.Size(100,50),
    new google.maps.Point(0,0),
    new google.maps.Point(50,50)
  );

  var companyShadow = new google.maps.MarkerImage('images/map/logo_shadow.png',
    new google.maps.Size(130,50),
    new google.maps.Point(0,0),
    new google.maps.Point(65, 50));

  var companyPos = new google.maps.LatLng(-1.2829596435698636, 36.82146191596985)

  var companyMarker = new google.maps.Marker({
    position: companyPos,
    map: map,
    icon: companyImage,
    shadow: companyShadow,
    title:"Arctic Computer Shop",
    zIndex: 3});

  google.maps.event.addListener(companyMarker, 'click', function() {
    infowindow.open(map,companyMarker);
  });
}

// Document Ready
jQuery(function(){

  // Tooltips on any element with tooltip attribute
  simple_tooltips(jQuery('*[tooltip]'));

  // Listings Hover
  jQuery('.listings .content').hover(
    // over
    function(){
      jQuery(this).addClass('hover');
    },
    // out
    function(){
      jQuery(this).removeClass('hover');
  });
  // Listings Links
  jQuery('.listings .content').click(function(){
    var link = jQuery(this).find('h3 a').attr('href');
    location = link;
  });
});

// Document Load
jQuery(window).load(function() {
  if (screen.width > 480){
    // Initialize our Navigation dropdown functionality
    jQuery("ul.menu li").hover(
    // Mouse Over
    function(){
      jQuery(this).stop(true, true)
      .find("> ul")
      .css('display','none')
      .addClass('subhover')
      .slideDown('600');
    },
    // Mouse Out
    function(){
       list = jQuery(this).stop(false, true)
           .find("> ul")
           .slideUp('600',
        // Call Back after slide up
        function(){
          list.removeClass('subhover');
        });
    });
    // Listings Layout
    jQuery('.listings .outer-box:first').css('marginLeft', '0');
    jQuery('.listings .outer-box:nth-child(2)').css('marginLeft', '12px');
    jQuery('.listings .outer-box:nth-child(3)').css('marginLeft', '12px');
    jQuery('.listings .outer-box:last').css({'float': 'right', 'marginRight': '0'});
    // Equal height for products in cat view
    equalHeight(jQuery(".product"));
  }
  else
  {
    // Replace Header Image
    jQuery(".company .logo").attr('src', '/img/arctic_header_m.png');
  }
  // Slider
  jQuery('#slideshow').nivoSlider({pauseTime:5000});

  // Load Our Location map
  if (jQuery('#arctic-map').width() > 0)
  {
    init_location();
  }
});


