﻿var reviewPageIndex = 0;
var vehicleSearchOptions = {
    year: { control: '#year', placeholder: 'Year' },
    make: { control: '#makeId', placeholder: 'Make' },
    model: { control: '#modelId', placeholder: 'Model' },
    vehicle: { control: '#vehicleId', placeholder: 'Vehicle' },
    button: '#btnVehicle'
};

var currentSection = '';
var hasFlash = true;

$(document).ready(function() {
    var flashVersion = swfobject.getFlashPlayerVersion();
    // handle the case where flash is not installed
    if (flashVersion.major == 0) { hasFlash = false; }

    $('.content').addClass('mediaContentBg');
    $('#maincontent').css('width', '1007px');

    $("a.dwn_arrow").hide();
    $(".tire_nav_links").show();

    $("a.dwn_arrow").click(function() {
        open_navigation();
    });

    $("a.up_arrow").click(function() {
        close_navigation();
    });

    $('#flashContent').show();
    $('#videoPlayer').hide();
    
    onHashChanged(window.location.hash);
    $.History.bind(onHashChanged);
});

function open_navigation() {
	$("a.dwn_arrow").hide();
    $("a.up_arrow").show();
    $(".tire_nav_links").slideDown("slow");
}

function close_navigation() {
	$(".tire_nav_links").slideUp("fast");
    $("a.up_arrow").hide();
    $("a.dwn_arrow").show();
}

function popWallpaper(wallpaper) {
    $.getJSON(wallpaperDataUrl + '/' + wallpaper, function(data) {
        if (data == undefined) { return; }
        var slides = '<div class="wall"><img alt="' + data.Title + '" src="' + data.Display + '"><div class="wallpaper_download_link"><p>' + data.Title + '</p>';
        for (var k in data.Download) {
            slides += '<a href="' + data.Download[k] + '">' + k + '</a>';
        }
        slides += '</div></div>';
        $('#wallpaperContent').html(slides);
        $('#wallpaperPopup').show().focus().dialog({ modal: true, draggable: false, minHeight: 350, minWidth: 638, resizable: false, close: function(event, ui) { } });
    });
}

function onHashChanged(state) {
    $('#flashContent').show();
    $('#videoPlayer').hide();
    if (state == undefined) {
        if (currentSection != 'overview') {
            currentSection = 'overview';
            load_overview(overviewUrl, '#tirecontent');
        }
        return;
    }

    var states = state.split('&');
    if (states[0] == 'size' || states[0] == '#size') {
        if (currentSection != 'size') {
            currentSection = 'size';
            load_size(sizeUrl, '#tirecontent');
        }
    } else if (states[0] == 'tech' || states[0] == '#tech') {
        if (currentSection != 'tech') {
            currentSection = 'tech';
            load_technology(techUrl, '#tirecontent', states[1]);
        } else {
            load_technology_tab(states[1]);
        }
    } else {
        if (currentSection != 'overview') {
            currentSection = 'overview';
            load_overview(overviewUrl, '#tirecontent');
        }
    }
}

function load_overview(url, target) {
	$('#body').hide();
    $('#body').removeClass('overviewBackground');
    $('#body').removeClass('sizesBackground');
    $('#body').removeClass('techBackground');
    $('#tireOverview').addClass('active_nav');
    $('#tireSizes').removeClass('active_nav');
    $('#tireTech').removeClass('active_nav');
    reviewPageIndex = 0;
    $.get(url, function(data) {
        $(target).html(data);
        GetReview(reviewPageIndex);
        $('#body').addClass('overviewBackground').fadeIn(3000);

        if (hasFlash) { swfobject.getObjectById('FlashID').changeSection('overview'); }
        $('#staticImage').html('<img src="' + staticImageUrl.replace('{section}', 'overview') + '" alt="" />');

        changeVideoPage(0);
        changeWallpaperPage(0);
    });
}

function changeWallpaperPage(page) {
    $(".wallpaperPage").hide();
    $("#wallpaperPage" + page).show();
}

function changeVideoPage(page) {
    $(".videoPage").hide();
    $("#videoPage" + page).show();
}

function load_size(url, target) {
	$('#body').hide();
    $('#body').removeClass('overviewBackground');
    $('#body').removeClass('sizesBackground');
    $('#body').removeClass('techBackground');
    $('#tireOverview').removeClass('active_nav');
    $('#tireSizes').addClass('active_nav');
    $('#tireTech').removeClass('active_nav');
    $.get(url, function(data) {
        init_size(target, data);

        $('#body').addClass('sizesBackground').fadeIn(3000);

        if (hasFlash) { swfobject.getObjectById('FlashID').changeSection('size'); }
        $('#staticImage').html('<img src="' + staticImageUrl.replace('{section}', 'sizes') + '" alt="" />');
    });
}

function init_size(target, data) {
    $(target).html(data);
    $(vehicleSearchOptions.year.control).change(function() { LoadMake(tireSearchUrl, vehicleSearchOptions); });
    $(vehicleSearchOptions.make.control).change(function() { LoadModel(tireSearchUrl, vehicleSearchOptions); });
    $(vehicleSearchOptions.model.control).change(function() { LoadVehicle(tireSearchUrl, vehicleSearchOptions); });
    $(vehicleSearchOptions.vehicle.control).change(function() { GetTireByVehicle(tireSearchUrl, vehicleSearchOptions); });

    LoadYear(tireSearchUrl, vehicleSearchOptions);
    $('#frmVehicle').ajaxForm({ iframe: 'true', success: function(responseText) { init_size(target, responseText); } });
}

function load_technology(url, target, tab) {
	$('#body').hide();
    $('#body').removeClass('overviewBackground');
    $('#body').removeClass('sizesBackground');
    $('#body').removeClass('techBackground');
    $('#tireOverview').removeClass('active_nav');
    $('#tireSizes').removeClass('active_nav');
    $('#tireTech').addClass('active_nav');
    $.get(url, function(data) {
        $(target).html(data);
        $('#body').addClass('techBackground').fadeIn(3000);

        if (hasFlash) { swfobject.getObjectById('FlashID').changeSection('tech'); }
        $('#staticImage').html('<img src="' + staticImageUrl.replace('{section}', 'tech') + '" alt="" />');

        load_technology_tab(tab);
    });
}

function load_technology_tab(tab) {
    $('#tab_container > div').hide();
    if (tab == undefined || tab == '') {
        $('#tab_container > div:first').show();
    } else {
        $('#' + tab).show();
    }
}

function GetReview(page) {
    $.get(getReviewUrl + page, function(data) {
        $('#reviews').append(data);
    });
}

function PopupVideo(video)
{
    if (video == undefined) { return; }

    if (hasFlash)
    {        
        var player = '<iframe width="560" height="349" src="http://www.youtube.com/embed/' + video + '" frameborder="0" allowfullscreen></iframe>';
        $('#videoPlayerPopup').show().focus().dialog({ modal: true, draggable: false, minHeight: 400, minWidth: 600, resizable: false, close: function(event, ui) { $('#videoPlayerPopup').html(''); } });
        $('#videoPlayerPopup').append(player);
    } else
    {        
        // send user to youbute instead
        window.open('http://www.youtube.com/watch?v=' + video);
        return;
    }
}
