/***********************
*  Site Specific JS   *
************************/

//Javascripts
$(document).ready(function(){ 
  $.favicon('http://cdn.myld.com.au/2/1565/greg-luke-optometrist-medioptics_bbb1ac781b.ico','http://cdn.myld.com.au/2/1565/greg-luke-optometrist-medioptics_f164132fa6.png');
	
	menu.contactDetails({
		phone: ['0266589114', '0266589117'],
					//optional, can have multiple values['0752212345', ['Head Office', '0752212345']]
		email: 'info@medioptics.com.au',
					//optional, can have multiple values['email@gmail.com', ['Support', 'support@yahoo.com']]
		address: 'SP017 Toormina Gardens Shopping Centre Toormina Rd Toormina 2452',
					//optional, can have multiple values['Robina, QLD', ['Southport', 'Southport, QLD']]
		 hours: [ 
					['Mon-Wed', '9:00am - 5:30pm'],
					['Thursday', '9:00am - 6:00pm'],
					['Saturday', '8:00am - 12:00pm'],
					['Sunday', 'Closed'],
					['Public Holidays', 'Closed']
			] 
			//optional 
	});
	
	//home services link
	$('#htLink1').replaceWith('<a href="contact">click here</a>');
	
	//slider
	$("#slider").backstretch([
		"http://cdn.myld.com.au/2/1565/greg-luke-optometrist-medioptics_25d23fe2a3.jpg",
		"http://cdn.myld.com.au/2/1565/greg-luke-optometrist-medioptics_ab737ef42d.jpg",
		"http://cdn.myld.com.au/2/1565/greg-luke-optometrist-medioptics_6364c76e43.jpg",
		"http://cdn.myld.com.au/2/1565/greg-luke-optometrist-medioptics_394973cd07.jpg"
	], {duration: 3000, fade: 750, random: true});
	// Set active for the first bullet when document ready;;
	$('.pagination').first().addClass('active');
	// Since you called it as $.backstretch, it's attached to the body
	var instance = $("#slider").data("backstretch");
	$('.pagination').click(function () {
		var index = $('.pagination').index( $(this) );
		$('.pagination').removeClass('active');
		$(this).addClass('active');
		// Show the slide based on the clicked index
		instance.show(index);
		// Return false, so that the click doesn't change the page hash
		return false;
	});
	// Set the current pagination active while running as slideshow
	$("#slider").on("backstretch.before", function (e, instance, index) {
		$('.pagination').removeClass('active').eq(index).addClass('active');
	});
	
	$("#tabImage").backstretch([
		"http://cdn.myld.com.au/2/1613/web_greg-luke-optometrist-medioptics_12fc4cc5e1.jpeg",
		"http://cdn.myld.com.au/2/1613/web_greg-luke-optometrist-medioptics_12fc4cc5e1.jpeg",
		"http://cdn.myld.com.au/2/1613/web_greg-luke-optometrist-medioptics_12fc4cc5e1.jpeg",
		"http://cdn.myld.com.au/2/1613/web_greg-luke-optometrist-medioptics_12fc4cc5e1.jpeg",
		"http://cdn.myld.com.au/2/1613/web_greg-luke-optometrist-medioptics_12fc4cc5e1.jpeg"
	], {duration: 200, fade: 200, random: false});
	$('#tabImage').backstretch("pause");
     
});

$(window).load(function(){
	mapcanvas();
	
	//scroller
	$('#scrollable').mCustomScrollbar('update');
	
	

//check if hash tag exists in the URL
    function scrolltoTop(hash) {
        if ($('#scrollable').length) {
            $("#scrollable").mCustomScrollbar('scrollTo',hash);
						$('html, body').animate({ 
						'scrollTop': $('#content').offset().top 
						}, 400);
        }
    }

    if(window.location.hash) {
        var hash_tag = window.location.hash;

        scrolltoTop(hash_tag);
    }
});

$(window).bind("load resize",function(){
	$('.htText').each(function() {
    $(this).css('bottom', -$(this).outerHeight()+88);
  });
});

//form validation
$('#custom_form').smartCaptcha({ 
  validateText: ["name", "message", "number"],
  validateEmail: ["email"],
	validateStyle: "default"
});

//map
function mapcanvas() {
  var myLatlng = new google.maps.LatLng(-30.35395, 153.09188);
  var image = 'http://cdn.myld.com.au/2/1565/greg-luke-optometrist-medioptics_459ba61952.png';
  var mapOptions = {
    zoom: 17,
		scrollwheel: false,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  
  if ($('#map-canvas').length > 0) {
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    
      var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          icon: image
      });
      
      google.maps.event.addDomListener(window, 'resize', function() {
			map.setCenter(myLatlng);
	  });         
      google.maps.event.addDomListener(window, 'orientationchange', function() {
			map.setCenter(myLatlng);
	  });   
	}
}

//view section 1
$('#view_section_1').insertAfter('#slider');

//error div
$('#error').insertAfter('#slider');

//nav justify
$('.navbar .nav').addClass('nav-justified');

//Detect mac
	var Browser = menu.browserDetect('version');
		if (Browser.OS == 'Mac') {
		$('body').addClass('mac');
}

//scroller
$('#scrollable').mCustomScrollbar({
        scrollButtons:{
            enable:false
        },
        mouseWheel:{
            enable:true,
            preventDefault:true
        },
        callbacks:{
            onScroll: function(){
                var lastId,
                topMenu = $('#scroll-nav'),
                menuItems = topMenu.find('a'),
                containerHeight = $('#scrollable').offset().top,
                scrollItems = menuItems.map(function(){
                      var item = $($(this).attr("href"));
                      if (item.length) { return item; }
                });

                var cur = scrollItems.map(function(){
                    if ($(this).offset().top <= containerHeight + 50) {
                        return this;
                    }
                });
                cur = cur[cur.length-1];
                var id = cur && cur.length ? cur[0].id : "";
                if (lastId !== id) {
                    lastId = id;
                    menuItems
                        .parent().removeClass("active")
                        .end().filter("[href=#"+id+"]").parent().addClass("active");
                }

                hashTag(id);


            },
        },
        advanced:{
            // updateOnBrowserResize: true
        },
        theme:"dark-thick"
    });
		
    //Scroll Animation
    $("#scroll-nav li a").on('click', function(e) {

        e.preventDefault();
        var hash = $(this).attr('href');
        var imgTag = hash.replace('#', '');

        $("#scrollable").mCustomScrollbar('scrollTo',hash);
        hashTag(imgTag);
    });

    $(window).bind('resize',function(){
        $('#scrollable').mCustomScrollbar("update");
    });
		
    //Custom Function
    function hashTag(hash_tag) {
        switch(hash_tag) {
            case 'scroll1':
            	$('#tabImage').backstretch("show",0);
            break;
            case 'scroll2':
            	$('#tabImage').backstretch("show",1);
            break;
            case 'scroll3':
            	$('#tabImage').backstretch("show",2);
            break;
            case 'scroll4':
            	$('#tabImage').backstretch("show",3);
            break;
            case 'scroll5':
            	$('#tabImage').backstretch("show",4);
            break;
            default:
            	$('#tabImage').backstretch("show",0);
        }
    }