$(document).ready(function(){

//var baseURL = 'http://localhost/dynamic/';
//var baseURL = 'http://174.121.153.219/~dynamic/';
var baseURL = 'http://dynamicspecialty.com/';
   
$('ul#topnav li').hover(function(){
	$('ul.subnav', $(this)).slideDown(100);
},function(){
	$('ul.subnav', $(this)).slideUp(100);
});

// onHide : fade the window out, remove overlay after fade.
var close = function(hash) { 
   hash.w.hide();
   hash.o.hide(); 
};

// set up vehicle popup
$('div.dialog').jqm({ overlay: 60, onHide: close });
$('div.dialog').jqmAddClose('.close');
$('div.popup').jqm({ overlay: 60, onHide: close });
$('div.popup').jqmAddClose('.close');

$('.close').click(function(){
    hideVideo();
});

function hideVideo()
{
   $('iframe').hide();    
}

function showVideo()
{
    $('iframe').show();
}

$('a.modal').click(function(){
    var id = $(this).attr('href');
    var productID = '#product' + id + '.popup';
	$('iframe').show();
    $(productID).jqmShow(); 
    return false;
})

var navItems = $('#specs_nav, #photos_nav, #pdf_nav');
var pageItems = $('#specs, #photos, #pdf');

$('#specs_nav').click(function(){
   registerClick($(this));
   return false; 
});

$('#photos_nav').click(function(){
   registerClick($(this)); 
   return false;
});

$('#pdf_nav').click(function(){
   registerClick($(this)); 
   return false;
});

pageItems.hide();

/*

$('a.modal').click(function(){
   var productID = $(this).attr('rel');
   $('ul.dialog_nav').attr('rel', productID);
   showContent('photos', productID);
   navItems.removeClass('active');
   $('#photos_nav').addClass('active');
   $('div#product').jqmShow();   
   return false;
});

*/

$('a.video1').click(function(){
    showVideo();
   $('div#video1').jqmShow();   
   return false;
});

$('a.video2').click(function(){
    showVideo();
   $('div#video2').jqmShow();   
   return false;
});

/*
$('a.featured').click(function(){
   var productID = $(this).attr('rel');
   $('ul.dialog_nav').attr('rel', productID);
   showContent('photos', productID);
   navItems.removeClass('active');
   $('#photos').addClass('active');
   $('div#product').jqmShow();
   return false;   
});
*/

// function to be called on nav item clicks
function registerClick(navItem){

   pageItems.hide();

   var idName = navItem.attr('id');
   var productID = navItem.parent('li').parent('ul').attr('rel');
   
   navItems.removeClass('active');
   navItem.addClass('active');
   showContent(idName.substring(0, idName.length-4), productID);
   
}

/* 

function showContent(idName, productID)
{   
    var url = baseURL + 'store/dialog/' + idName + '/' + productID;
   var divName = 'div#' + idName;
   var div = $('#' + idName);
   div.load(url, function(response, status, xhr) {     if(status == "error") {
       var msg = "Sorry but there was an error: ";
       div.html(msg + xhr.status + " " + xhr.statusText);
     }
   }).show();
}

*/
   
var bc = $('#feature_nums'); 

$('#meetus_images').cycle({ 
   fx:      'fade', 
   speed:   600,
   timeout: 10000,
   delay:   -3000,
   prev: '#prev',
   next: '#next',
   after: function(){
               $('.meetus_text').fadeIn().html(this.alt);
            }      
});

// set up new and used bus objects
var nselector = 'div.product.new';
var uselector = 'div.product.used';
var n = $(nselector);
var u = $(uselector);

//console.log(n);
//console.log(u);

$('a.new').click(function(){
    if(!isVisible(nselector))
    {
        n.fadeIn();
    }

    if(isVisible(uselector))
    {
        u.fadeOut();
    }
    return false;
});

$('a.used').click(function(){
    if(!isVisible(uselector))
    {
        u.fadeIn();
    }
    
    if(isVisible(nselector))
    {
        n.fadeOut();
    }    
    return false;
});

// checks to see if the elements found by the selector passed are visible or not
function isVisible(selector) {
    var elements = $(selector+':visible');
    if(elements.length > 0)
    {
        return true;
    }
    else
        return false;
}

// if no feature images, put a border on the bottom of the features area
var imgLength = $('#feature_images img').length;
if(!imgLength)
{
   $('#feature_images').css({ borderBottom: "1px solid #999999" });
   $('#feature_text').hide();
}

// pop up external links in new, smaller windows
$('a[rel="external"]').click(function(){
   window.open($(this).attr('href'),this,'width=800,height=600,status=0,toolbar=0');
   return false;
});   

$('div#featured_vehicle').hover(function(){
   $('div#featured_vehicle h4').css({ "backgroundColor" : "#1B4C71" });
},function(){
   $('div#featured_vehicle h4').css({ "backgroundColor" : "#d2661d" });      
});

$('#newschooser').change(function(){
   var path = $(this).val();
   if(path != 0)
   {
      window.location.href = path;
   }
});

});

