$(document).ready(function() {
    // initialize links (change hrefs from external to internal)
    $('.index #section-listing a').each(function(index){
        if (index > 0) {
            oldhref = $(this).attr('href');
            oldhref = oldhref.split('/');
            i = oldhref.length;
            newhref = "#" + oldhref[i - 1];
            $(this).attr('href', newhref);            
        }
    });
    
    // see if there's an anchor in the url, if so, open that page
    // SLIGHTLY BUGGY W/ CACHING
    current_url = window.location.href;
    current_url = current_url.split('#');
    if (current_url[1]) {
        anchor = "#" + current_url[1];
        clicked = $('#section-listing a[href="'+ anchor +'"]');
        openPage(clicked);
    }
    
    // refer a friend popup
    
    $('.quick-referral-link').click(function(e){
        e.preventDefault();
        url = $(this).attr('href');
        openOverlay(url);
    });
    
    // long referral
    
    $('.referral form input[type="submit"]').click(function(e) {
       selected_slugs = []
       $('form input:checked').each(function() {
           selected_slugs.push($(this).val());
       });
       slug_list = selected_slugs.join(', ');
       $('#id_url_pks').val(slug_list);
    });
    
    
    // main section ajax
    $('#section-listing a').click(function() {
       clicked = $(this);
       openPage(clicked);
    });
    
    
    // forms
    $('#form-health-dental-history .contingent').next().hide();
    $('#form-health-dental-history .contingent input[type="radio"]').click(function () {
        value = $(this).val();
        if (value == "yes") {
            $(this).closest('.contingent').next().show('fast');
        } else if (value = "no") {
            $(this).closest('.contingent').next().hide('fast');            
        }
    });
    
    // fancybox plugin
    $('.lightbox').fancybox({
        'titlePosition' : 'inside',
    });
    
    
    // rsvp form submit
    $('#rsvp form').submit(function(e) {
        e.preventDefault();
        url = $('#rsvp form').attr('action');
        data = $('#rsvp form').serialize();
        $.post(url, data, function() {
           $('#rsvp').html('<h4>See you at the event!</h4>') 
           return false;
        });
    });
    
}); 

function openOverlay(url) {
    currenturl = 'http://meyerdentalgroup.net' + window.location.pathname;
    $.get(url, function(data) {
        overlay = '<div id="overlay-container">' + data + '<div id="overlay"></div></div>'
        $('body').append(overlay);
        $('.module').prepend('<a href="#" class="close-overlay">[x]</a>')
        $('.close-overlay').click(function(e) {
            e.preventDefault();
            $('#overlay-container').fadeOut('fast', function(){
                $('#overlay-container').remove();
            });
            $('#quick-referral #id_url').attr('value', currenturl);
        });
        
        $('#quick-referral input[type="submit"]').click(function(e) {
            e.preventDefault();
            formdata = $('#quick-referral form').serialize() 
            $.post('/quick_referral/', formdata, function() {
                $('#overlay-container').fadeOut('fast', function(){
                    $('#overlay-container').remove();
                })
            });
        });   


    });
}


function openPage(clicked) {
    $('#section-listing li').removeClass();
    parentelem = $(clicked).parent('li');
    parentelem.addClass('selected');
    left = parentelem.position().left - 26;
    width = parentelem.innerWidth();
    $('#caret').css('left', (left + (width/2)));
    url = $(clicked).attr('href').split("#");
    url = url[1] + '?xhr';
    $.getJSON(url, function(data) {
        photolength = data.photos.length;
        if (photolength > 0) {
            photos = ['<div id="photos" class="', data.photo_position, '">'];
            for (i=0; i<photolength; i++) {
                photos.push('<div class="photo"><img src="' + data.photos[i].url + '" title="' + data.photos[i].title + '" height="' + data.photos[i].height + 'px" width="' + data.photos[i].width + 'px" />' + "<span>" + data.photos[i].title + "</span></div>");
            }
            photos.push('</div>');
            photos = photos.join("");
        } else {
            photos = "";
        }
        bottom = '<div id="bd-bottom"><a class="alternate-link" title="Contact Dr. Thomas Meyer, DDS" href="/contact-us">Contact Dr. Meyer to request more information now!</a><h1 class="callout">' + data['title'] + ' by Des Plaines Dentist Dr. Thomas Meyer</h1><p>Serving the Areas of Des Plaines, Mount Prospect, and the Surrounding Chicago Suburbs.</p></div>'
        html = ["<h3>", data['title'], "</h3>", photos, data['copy'], bottom];
        
        $('#page-content').fadeOut('fast', function() {
            $('#page-content').html(html.join('')).fadeIn('fast');
        });
    });
}
