//Vertical scrolling
$(document).ready(function() {

  $("a.fblink").fancybox({
    'transitionIn'	: 'none',
    'transitionOut'	: 'none'
  });

  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;

        if (navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/Android/i) ){
          //Do not override navigation for mobile devices.
        } else {
          $(this).click(function(event) {
            event.preventDefault();
            $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
              location.hash = target;
            });
          });
        }
      }
    }
  });

 


  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }

});


// initialize scrollable
$(function() {
	
	$(".scrollable").scrollable({
	
		onSeek: function() {
      $("#ecard-choice").val(this.getIndex() + 1);
  	}
	
	
	});
  
});


//Toggle
$(document).ready(function() {
	//$('[class^=toggle-item]').hide();
	$('[class^=link]').click(function() {
	$('[class^=toggle-item]').hide();
	var $this = $(this);
	var x = $this.attr("class");
	$('.toggle-item-' + x).toggle();
	return false;
	});
});



//ddsmoothmenu.init({
	//mainmenuid: "smoothmenu1", //menu DIV id
	//orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
	//classname: 'ddsmoothmenu', //class added to menu's outer DIV
	//customtheme: ["#1c5a80", "#18374a"],
	//contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
//})

//Dropdown menu
$(function(){

    $("ul.dropdown li").hover(function(){
    
        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');
    
    }, function(){
    
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');
    
    });
    
    if (navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/Android/i) ){
      $("ul.dropdown > li > a").click(function(event){
          event.preventDefault();
          $(this).addClass("hover");
          $('ul:first',this).css('visibility', 'visible');
      
      });
    }
    
    
    
    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

});

//ipad/iphone specific formatting 

 $().ready(function() {
 
	if (navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/Android/i) ){
	
	/* NAV POSITION */

var specScroll = false; // If special scrolling is needed

/* Check what kind of position to use.*/
(function navPos() {
    var width = checkWidth();

    if (width <= 480 || navigator.userAgent.match(/iPad/i) != null) {
        specScroll = true;
    }else{
        specScroll = false;
        window.onscroll = NaN;
    }
})();

$(window).resize( function(){ navPos(); } ); // After resizing, check what to use again.


/* When clicking one of the nav anchors */
$(function() {
    $('a').bind('click',function(e){
        var $anchor = $(this);

        if(specScroll){
            $('#header ').css('position', "absolute");
            window.onscroll = anchorScroll;
        }
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 700,'easeOutExpo', function(){
            if(specScroll){setTimeout("window.onscroll = touchScroll;", 100);} 
            // the set timeout is needed for not overriding the clickability of the anchors after anchor-scrolling.
        });

        e.preventDefault();
    });
});

/* While the user clicks and anchors in nav */
function anchorScroll() { $('#header').css('top', window.pageYOffset); }

/* the first time the user scrolls by touch and lift the finger from screen */
function touchScroll() { 
    $('#header').css('position', 'fixed');
    $('#header').css('top', 0);
    window.onscroll = NaN;
}

/* CHECK WIDTH OF WINDOW */
function checkWidth() {
    myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        myWidth = window.innerWidth;    //Non-IE
    } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
        myWidth = document.documentElement.clientWidth; //IE 6+ in 'standards compliant mode'
    } else if( document.body && ( document.body.clientWidth ) ) {
        myWidth = document.body.clientWidth;    //IE 4 compatible
    }
    return myWidth;
}


	}

});



//Ecards

 $().ready(function() {
      
      var submitted = false;

      $("#share-form-form").validate({
        rules: {
          "share-your-first-name": {
            required: true,
            minlength: 2
          },
          "share-your-address": {
            required: true,
            minlength: 2,
            email: true
          },
          "friends-first-name": {
            required: true,
            minlength: 2
          },
          "friends-address": {
            required: true,
            minlength: 2,
            email: true
          },
          "your-comment": {
            required: true,
            minlength: 2
          }
        },
        messages: {
          "share-your-first-name": {
            required: "Your first name is required."
          },
          "share-your-address": {
            required: "Your email address is required.",
            email: "Your email address is invalid."
          },
          "friends-first-name": {
            required: "Your friends first name is required."
          },
          "friends-address": {
            required: "Your friends email address is required.",
            email: "Your friends email address is invalid."
          },
          "your-comment": {
            required: "You must enter a comment to send to your friend."
          }
          
        },
        errorPlacement: function(error, element) {
          //alert(error.html());        
        },
        showErrors: function(errorMap, errorList) {
            if (submitted) {
                var summary = "You have the following errors: \n\n";
                $.each(errorList, function() { summary += " * " + this.message + "\n"; });
                alert(summary);
                submitted = false;
            }
            this.defaultShowErrors();
        },          
        invalidHandler: function(form, validator) {
            submitted = true;
        },

        onfocusout: false,
        onkeyup: false,
        onsubmit: true
      });
      
      $("#btn-submit").click(function(){
        if ($("#share-form-form").valid()) {
          $("#share-form-form").submit();
          
        }
        return false;

      });
        




        

      });

