function toggleMakes() {
    $("#makes-short").toggle();
    $("#makes-long").toggle();
    $("#expand-makes").toggle();
    $("#collapse-makes").toggle();
}

function toggleCategories() {
    $("#categories-short").toggle();
    $("#categories-long").toggle();
    $("#expand-categories").toggle();
    $("#collapse-categories").toggle();
}

function photos_makeHtml(data , photoType)
{
    $('#carousel').fadeOut('slow');
    $('#carousel').html('');
    $('photos_links_header').html('');
    $('photos_links').html('');
    //If AutoShows we need to send also the show_tag_id to get the image link
    if (photoType === "AutoShows")
    {
        $("#carousel").load("index.php?module=homepage_photos", {
            imgId: data.image_id,
            action : 'getImgById',
            type: photoType,
            show_tag_id: data.show_tag_id
            }, function () { });
    }
    else
    {
        $("#carousel").load("index.php?module=homepage_photos", {
            imgId: data,
            action : 'getImgById',
            type: photoType
        }, function () { });
    }
    $('#carousel').fadeIn('slow');
}

function photos_next()
{
    if (photos_currentTab === 'NewCars')
    {
        if (photos_NewCarsCount > photos_Current + 1)
        {
            photos_Current++;
        }
        else
        {
            photos_Current = 0;
        }
        photos_makeHtml(photos_NewCarsIds[photos_Current] , "NewCars");
    }
    else if (photos_currentTab === 'AutoShows')
    {
        if (photos_AutoShowCount > photos_Current + 1)
        {
            photos_Current++;
        }
        else
        {
            photos_Current = 0;
        }
        photos_makeHtml(photos_AutoShowIds[photos_Current] , "AutoShows");
    }
    else if (photos_currentTab === 'SpyShots')
    {
        if (photos_SpyShotsCount > photos_Current + 1)
        {
            photos_Current++;
        }
        else
        {
            photos_Current = 0;
        }
        photos_makeHtml(photos_SpyShotIds[photos_Current] , "SpyShots");
    }
}

function photos_prev()
{
    if (photos_currentTab === 'NewCars')
    {
        if (photos_Current - 1 > -1)
        {
            photos_Current--;
        }
        else
        {
            photos_Current = photos_NewCarsCount - 1;
        }
        photos_makeHtml(photos_NewCarsIds[photos_Current] , "NewCars");
    }
    else if (photos_currentTab === 'AutoShows')
    {
        if (photos_Current - 1 > -1)
        {
            photos_Current--;
        }
        else
        {
            photos_Current = photos_AutoShowCount - 1;
        }
        photos_makeHtml(photos_AutoShowIds[photos_Current] , "AutoShows");
    }
    else if (photos_currentTab === 'SpyShots')
    {
        if (photos_Current - 1 > -1)
        {
            photos_Current--;
        }
        else
        {
            photos_Current = photos_SpyShotsCount - 1;
        }
        photos_makeHtml(photos_SpyShotIds[photos_Current] , "SpyShots");
    }
}

function photos_setTab(tabname)
{
    photos_currentTab = tabname;
    photos_Current = 0;
    data = '';
    $("ul#photos-nav li").removeClass("ui-tabs-selected");
    if (tabname === "NewCars")
    {
        data = photos_NewCarsIds;
        $("#photos_new_cars").addClass("ui-tabs-selected");
    }
    if (tabname === "AutoShows")
    {
        data = photos_AutoShowIds;
        $("#photos_auto_shows").addClass("ui-tabs-selected");
    }
    if (tabname === "SpyShots")
    {
        data = photos_SpyShotIds;
        $("#photos_spy_shots").addClass("ui-tabs-selected");
    }
    photos_makeHtml(data[photos_Current] , tabname);
}

var tcc_idx =0;
var tcc_rotate = true;
var tcc_timeout;

function loadTab(tcc_idx){
    //navigation links selector
    var nav = '#car-reviews-nav li a';
    var tab = '#car-reviews-nav li';
    var cat = $(nav+':eq('+tcc_idx+')').attr('title');
    var url = ('index.php?module=car-reviews&vcat=' + cat);
    $('#reviews_content').fadeOut(500).load(url,'',function(){
        // remove active link class for all links then change the class of the link
        $(tab).removeClass("ui-tabs-selected");
        $(tab+':eq('+tcc_idx+')').addClass("ui-tabs-selected");
        $('#reviews_content').fadeIn(500);
        tcc_timeout = setTimeout("rotateTabs()",10000);
        return false;
    });
}
function rotateTabs(){
    var tabsCount = $('#car-reviews-nav li').length;
    if(tcc_rotate === true && tcc_idx < tabsCount){
        loadTab(tcc_idx);
        tcc_idx++;
        if(tcc_idx == tabsCount){
            tcc_idx = 0;
        }
    }
}

function initEventsTracker() {
    $("#car-reviews-nav li a span").click(function() {
        pageTracker._trackEvent("Homepage Car Reviews Module ", "Homepage Reviews Category Clicked", $(this).html());
    });

    $("#newmake").change(function () {
       pageTracker._trackEvent("Homepage New Cars Selector", "Homepage New Cars Make Selected", $("#newmake option:selected").text());
    });

     $("#newmodel").change(function () {
       pageTracker._trackEvent("Homepage New Cars Selector", "Homepage New Cars Model Selected", $("#newmodel option:selected").text());
    });

    $("#usedmake").change(function () {
       pageTracker._trackEvent("Homepage Used Cars Selector", "Homepage Used Cars Make Selected", $("#usedmake option:selected").text());
    });

    $("#usedmodel").change(function () {
       pageTracker._trackEvent("Homepage Used Cars Selector", "Homepage Used Cars Model Selected", $("#usedmodel option:selected").text());
    });
    
    $("#expand-makes").click(function () {
       pageTracker._trackEvent("Homepage New Cars Selector", "Homepage New Cars Expand Makes", "Expand Makes");
    });

    $("#expand-categories").click(function () {
       pageTracker._trackEvent("Homepage New Cars Selector", "Homepage New Cars Expand Categories", "Expand Categories");
    });

    $("#photos-nav li a span").click(function() {
        pageTracker._trackEvent("Homepage Mini Photos Gallery ", "Homepage Gallery Tab Clicked", $(this).html());
    });

    $("#photos-new-cars #prev").click(function() {
        pageTracker._trackEvent("Homepage Mini Photos Gallery", "Homepage Gallery Prev Clicked", "Prev Clicked");
    });

    $("#photos-new-cars #next").click(function() {
        pageTracker._trackEvent("Homepage Mini Photos Gallery", "Homepage Gallery Next Clicked", "Next Clicked");
    });

    $("#blog-inner").mouseover(function() {
        pageTracker._trackEvent("Homepage Blogs Module", "Homepage Blogs Hover", "Homepage Blogs Hover");
        $("#blog-inner").unbind("mouseover");
    });

    $("#partners").mouseover(function() {
        pageTracker._trackEvent("Homepage Partners Module", "Homepage Partners Hover", "Homepage Partners Hover");
        $("#partners").unbind("mouseover");
    });
    
    $("#footer-wrapper").mouseover(function() {
        pageTracker._trackEvent("Homepage Footer", "Homepage Footer Hover", "Homepage Footer Hover");
        $("#footer-wrapper").unbind("mouseover");
    });
}
		
$(document).ready(function () {
    photos_setTab("NewCars");
    $("#next").click(function () {
        photos_next() ;
    });
    $("#prev").click(function () {   
        photos_prev() ;
    });
    $("#photos_new_cars").click(function () { 
        photos_setTab("NewCars") ;
    });
    $("#photos_auto_shows").click(function () { 
        photos_setTab("AutoShows") ;
    });
    $("#photos_spy_shots").click(function () { 
        photos_setTab("SpyShots") ;
    });
	
    // initialize the fist tab to display as selected
    $('#car-reviews-nav li:eq('+tcc_idx+')').addClass("ui-tabs-selected");
		
    $('#car-reviews-nav li a').click(function(event) {
        var tcc_idx = $('#car-reviews-nav li a').index(this);
        loadTab(tcc_idx);
        tcc_rotate = false;
        return false;
    });
	
    // move pointer to the next tab then rotate
    tcc_idx = 1;
    rotateTabs();

    //stop rotating when mouse is over reviews_content section
    $("#reviews_content").mouseover(function(){
        tcc_rotate = false;
    });
    $("div#car-reviews").mouseout(function(){
        tcc_rotate = true;
        clearTimeout(tcc_timeout);
        tcc_timeout = setTimeout("rotateTabs()",10000);
    });
	
    // initialize top makes/browse categories expand/collapse
    $("#expand-makes").click(toggleMakes);
    $("#collapse-makes").click(toggleMakes);
    $("#expand-categories").click(toggleCategories);
    $("#collapse-categories").click(toggleCategories);

    // initialze events tracker for google analytics
    initEventsTracker();
});
