/*********************************************************
 *  Using jQuery 1.4.3 - safe mode enabled - use via $j  *
 *********************************************************/

var $j = jQuery.noConflict();

//DOM ready triggers
$j(document).ready(function(){
    
    //globals
    var obj = this;
    
    // add selector to last ul.li of subnav - IE bug
    $j("last-child").not("last-child a").addClass("lastLi")

    //remove border from last item
    $j('.course-wrapper, .search_item').last().append().css({'border-bottom':'none', 'margin-bottom':'0px'})
    
    //faqs accordion
    faqAccordions()
    addingThis()
    
    //treatments slider
    if ($j('.slider').size() > 0) {
        //init slider
        lectricSlider()      
    }    
   
    //dropkick custom dropdowns
    $j('#location_search_distance_field, #location_search_sm_category_select, #location_search_sm_tag_select').dropkick({
        theme : 'black'
    }); 
    
    
    if ($j('.study-map').size() > 0) {
        
        //http://www.hodgsonco.com/the-great-jquery-interactive-map-saga-a-howto-part-two
        //http://craigsworks.com/projects/qtip2/docs/plugins/ajax/
        
        //add the colored dots
        $j('.study-map').maphilight({
            fill: true,
            fillColor: '961212',
            fillOpacity: 0.9,
            strokeColor: '961212',
            fade: false,
            alwaysOn: true
        }); 

        //location triggers
        $j('.locations li a').each(function(e) {
            $j(this).mousedown(function(e) {
                $linkId = $j(this).attr('href');
                $postid = $j(this).attr('class');
                loadQtip($linkId, $postid);
                $j($linkId).mouseover();
    
            })
            .click(function(e) {e.preventDefault();});        
        });   

        //image map triggers
        $j('area').each(function() {
            $postid = $j(this).attr('class');
            $linkId = $j(this).attr('id');
            $linkId = '#' + $linkId;
            loadQtip($linkId,$postid); 
        });  
    }
   
   
});

//tooltip location
function loadQtip($linkId, $postid) {
    
    $url = 'http://' + document.domain + '/wp-content/themes/twentyten/get-studyclub.php'; 
    
    $j($linkId).each(function(){
        $j(this).qtip(
        {
            show: { 
                solo: true 
            },
            content: {
                text: 'Loading study club', 
                ajax: {
                    url: $url, 
                    type: 'GET', 
                    loading: false,
                    data: {
                        postid: $postid
                    }, 
                    dataType: 'json', 
                    success: function(data, status) {
                        console.log(data);
                        var content = data.studyclub[0].title + data.studyclub[0].content + data.studyclub[0].link;

                        // Now we set the content manually (required!)
                        this.set('content.text', content);
                    }
                }
            },
            style: {
                name: 'cream', // Give it the preset cream style
                border: {
                    width: 0, 
                    radius: 4 
                }, 
                tip: true // Apply a tip at the default tooltip corner
            },
            position: {
                my: 'bottom left',  // Position my top left...
                at: 'right top', // at the bottom right of...
                target: $j(this) // my target
            },
            hide: {
                fixed: true, 
                delay: 500
            }
        });
    });  
}

//accordion with HTML content
function faqAccordions() {

    if ($j('#accordion').size() <= 0) return;{

        $j('.acc_container').hide(); //Hide/close all containers
            $j('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container

            //On Click
            $j('.acc_trigger').click(function(){
                    if( $j(this).next().is(':hidden') ) { //If immediate next container is closed...
                            $j('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
                            $j(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
                    }
                    return false; //Prevent the browser jump to the link anchor
            });
    }
}

//addThis button
function addingThis () {
    $j('.col2').append('<div class="addThis">');
    $j('.col2').append('<a href=\'http://www.addthis.com/bookmark.php\' onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\'); " onmouseout=\'addthis_close();\' onclick=\'return addthis_sendto();\'><img src=\'http://s7.addthis.com/static/btn/v2/lg-share-en.gif\' border=\'0\' alt=\'AddThis Social Bookmark Button\' /></a>');
    $j('.col2').append('<script type=\'text/javascript\' src=\'http://s7.addthis.com/js/250/addthis_widget.js\'></script>');
    $j('.col2').append('</div>');
}

//treatments slider
function lectricSlider() {
    // Initialize the slider
    var slider = new Lectric();
    slider.init('.slider', {next: '.next', previous: '.previous'});

    // Keyboard shortcuts for left and right arrows
    $j(document).keydown(function(e) {
        if (e.keyCode === 39) {
            e.preventDefault();
            var previous = slider.page();
        slider.to(previous + 1);
        } else if (e.keyCode === 37) {
            e.preventDefault();
            var previous = slider.page();
            slider.to(previous - 1);
        }
    });
}
