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

//Javascripts
$(document).ready(function(){
    $.favicon('http://cdn.myld.com.au/2/943/onyx-hairdressing-medi-spa_ad8cb55242.png');

    menu.contactDetails({

        phone: '0268854555', //optional, can have multiple values[array]
        mobile: null, //optional, can have multiple values[array]
        email: 'admin@onyxdubbo.com.au', //optional, can have multiple values[array]
        address: 'Shop 32 Orana Mall, Dubbo NSW 2830', //optional, single value
        hours: [['Mon - Wed','9:00am-5:30am'],['Thursday','9:00am-8:00pm'],['Friday','9:00am-5:30pm'],['Saturday','9:00am-5:00pm'],['Sunday','10:00am-4:00pm']] //optional

    });


    // Back Stretch
    $("#slider").backstretch([
        "http://cdn.myld.com.au/2/942/onyx-hairdressing-medi-spa_8665e01a42.jpg",
        "http://cdn.myld.com.au/2/942/onyx-hairdressing-medi-spa_bd925c644f.jpg",
        "http://cdn.myld.com.au/2/942/onyx-hairdressing-medi-spa_72a9b6491d.jpg",
        "http://cdn.myld.com.au/2/942/onyx-hairdressing-medi-spa_04f19facdf.jpg",
        "http://cdn.myld.com.au/2/942/onyx-hairdressing-medi-spa_2a48ee90fc.jpg"
    ], {duration: 3000, fade: 750, random: true});

    $('#next').click(function(x) {
        x.preventDefault();
        $('#slider').data('backstretch').next();
    });
    $('#prev').click(function(x) {
        x.preventDefault();
        $('#slider').data('backstretch').prev();
    });




    // Scroll
    $(function() {
        $('#banner a[href*=#]:not([href=#])').click(function() {
            if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
                var target = $(this.hash);
                target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
                if (target.length) {
                    $('html,body').animate({
                        scrollTop: target.offset().top
                    }, 1000);
                    return false;
                }
            }
        });
    });


    // Tabber BG
    $("#tab1").click(function() {
        $(".tabber-tab").css("display","none");
        $("#hair-cut").css("display","block");
    });
    $("#tab2").click(function(){
        $(".tabber-tab").css("display","none");
        $("#hair-colouring").css("display","block");
    });
    $("#tab3").click(function(){
        $(".tabber-tab").css("display","none");
        $("#hair-treatments").css("display","block");
    });
    $("#tab4").click(function(){
        $(".tabber-tab").css("display","none");
        $("#hair-extensions").css("display","block");
    });


    // Match Height
    $(".match").matchHeight();


    // Collapsible
    $('.collapse').on('shown.bs.collapse', function() {
        $(this).parent().find(".glyphicon-plus").removeClass("glyphicon-plus").addClass("glyphicon-minus");
    }).on('hidden.bs.collapse', function() {
        $(this).parent().find(".glyphicon-minus").removeClass("glyphicon-minus").addClass("glyphicon-plus");
    });




    // Captcha
    if ( $("body#careers").is("*") ) {
        $('#career_form').smartCaptcha({
            redirectLink: null,
            validateStyle: "default"
        });
    }
    if ( $("body#contact-us").is("*") ) {
        $('#contact_form').smartCaptcha({
            validateText: ["name", "message"],
            validateEmail: ["email"],
            redirectLink: null,
            validateStyle: "default"
        });
    }



    // Map
    if ( $("body#contact-us").is("*") ) {
        function contactmap() {
          var myLatlng = new google.maps.LatLng(-32.25551, 148.63132);
          var image = 'http://cdn.myld.com.au/2/943/onyx-hairdressing-medi-spa_26243b553c.png';
          var mapOptions = {
            zoom: 13,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          }

          if ($('#contact-map').length > 0) {
              var map = new google.maps.Map(document.getElementById('contact-map'), 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);
              });
            }
        }

        $(window).load(function(){
            contactmap();
        });
    }




    // Gallery
    if ( $("body#gallery").is("*") ) {
        if(Modernizr.touch && $(".fancybox").length > 0 ) {
           var myPhotoSwipe = $(".fancybox").photoSwipe({ enableMouseWheel: false , enableKeyboard: false });
        } else {
            /* Apply to single image */
            $("a.fancybox").fancybox();

            /* Apply fancybox to multiple items */
            $("a.fancybox[rel='gallery_group']").fancybox({
                'transitionIn'    :    'elastic',
                'transitionOut'    :    'elastic',
                'speedIn'        :    600,
                'speedOut'        :    200
            });

            /* Apply with thumbnails visible */
            $("a.fancybox").fancybox({
                helpers : {
                    thumbs : {
                        width: 200,
                        height: 200
                    }
                }
            });
        }
    }




    //view section 1
    $('#view_section_1').insertAfter('#banner');

    //error div
    $('#error').insertAfter('#banner');

    // Replace Slider
    if ( $("body#contact-us").is("*") ) {
        $('#banner').remove();
    }


});






// Google Blogger
function numToShortMonth(num) { // Returns a short month string based on an Int input of range 0-11 (received from a Date.getMonth() return)
        var month = '';

        switch (num) {
            case 0:
                month = "Jan";
                break;
            case 1:
                month = "Feb";
                break;
            case 2:
                month = "Mar";
                break;
            case 3:
                month = "Apr";
                break;
            case 4:
                month = "May";
                break;
            case 5:
                month = "Jun";
                break;
            case 6:
                month = "Jul";
                break;
            case 7:
                month = "Aug";
                break;
            case 8:
                month = "Sep";
                break;
            case 9:
                month = "Oct";
                break;
            case 10:
                month = "Nov";
                break;
            case 11:
                month = "Dec";
                break;
        }

        return month;
    } // End function numToShortMonth(num)

// Begin basic Blogger API v3 News Feed Generator
    var blog_id = "6465164639222379800";
    var api_key = "AIzaSyBlKFuz1Nz9zo60NY0nk5orRaJw7x66aOg";
    var news_blog_api_string = 'https://www.googleapis.com/blogger/v3/blogs/'+blog_id+'/posts?key='+api_key; //GET string using the format: https://www.googleapis.com/blogger/v3/blogs/<blog id number>/posts?key=<API key>
    var news_blog_jqHXR = $.getJSON(news_blog_api_string)

    .done(function(news_blog_data_JSON) { // Once the getJSON is succesfull
      if ($('#blog').length) { //If we're on the blog page
        $.each(news_blog_data_JSON.items, function(i, news_blog_item) { // Loop through each post
          var published_date = new Date(Date.parse(news_blog_item.published)); // Parse the JSON string (v3 uses ECMAScript 5 ISO-8601 format) to Date prototype
          var published_month = numToShortMonth(published_date.getMonth()); // Get a short month String based off the Date prototype - getMonth returns an Int of range 0-11
          if (i === 0){ // First post
            console.log('i = 0 ' + news_blog_item.title);
            $('<div role="tabpanal" class="tab-pane active" id="blogFeedItem' + i + '"><h3 itemprop="name">' + news_blog_item.title + '</h3>' + news_blog_item.content + '<div class="blogDate">Posted: ' + published_date.getDate() + ' ' + published_month + ', ' + published_date.getFullYear() + '</div></div>').appendTo('#blogFeedNews'); // Append post to #blogFeedNews, class is active
            $('<li role="presentation" class="active"><a href="#blogFeedItem' + i + '" role="tab" data-toggle="tab">' + news_blog_item.title + '</a></li>').appendTo('#blogNavTab ul'); // Append post to #blogNavTab, class is active
            $('#blogFeedItem' + i + ' a > img').parent().attr('rel', 'blogFeedItem' + i);
            $('#blogFeedItem' + i + ' a > img').parent().addClass('fancybox');
          } else { // Every other post
            console.log('i /= 0 ' + news_blog_item.title);
            $('<div role="tabpenal" class="tab-pane" id="blogFeedItem' + i + '"><h3 itemprop="name">' + news_blog_item.title + '</h3>' + news_blog_item.content + '<div class="blogDate"> Posted: ' + published_date.getDate() + ' ' + published_month + ', ' + published_date.getFullYear() + '</div></div>').appendTo('#blogFeedNews'); // Append post to #blogFeedNews (is not "active")
            $('<li role="presentation"><a href="#blogFeedItem' + i + '" role="tab" data-toggle="tab">' + news_blog_item.title + '</a></li>').appendTo('#blogNavTab ul'); // Append post to #blogNavTab (is not "active").
            $('#blogFeedItem' + i + ' a > img').parent().attr('rel', 'blogFeedItem' + i);
            $('#blogFeedItem' + i + ' a > img').parent().addClass('fancybox');
          }
        }); // End of Post loop

        $('.blogDate').css({
            "color": "#fff",
            "font-size": "0.7em",
            "float": "right",
            "margin": "20px 0px 10px 0px"
        }); // In-line CSS styling required as there is a timing issue (or something else) preventing standard CSS files from modifying the content

        $('a.fancybox[rel*="blogFeedItem"]').fancybox();

        var hash = window.location.hash;
        if (hash !== '') {
          $('#blogNavTab a[href="' + hash + '"]').tab('show');
          $('html, body').delay(500).animate({
            scrollTop: $(hash).offset().top - 191
          }, 2000);
        }
      }
    }) //End .done

    //Fallback content to be displayed in case the blog is offline; not responding; or over the limit of daily requests
    .fail(function() {
      if ($('#blog').length){ // IF we're on the blog page
        $('<h1 itemprop="name">News Currently Offline</h1><p item="description">Apologies, the news feed is currently offline. We will endeavour to resolve the issue as soon as possible!</p>').appendTo("#blogFeedNews");
      }
    }); //End .fail and $.getJSON(news_blog_api_string)
// End basic Blogger API v3 News Feed Generator
