$(function() {

	$("#aHrefNeedAssistance").fancybox({
		'width'				: 540,
		'height'			: 450,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
	
	$(".stdpopup").fancybox({
		'width'				: 540,
		'height'			: 450,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});


	$(".search_inventory").fancybox({
		'width'				: 540,
		'height'			: 450,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});


    $(".fb_popup").fancybox({
        'width': 400,
        'height': 480,
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe'
    });

    $(".request_info").fancybox({
        'width': 400,
        'height': 540,
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe'
    });

	$("a.autocheck").fancybox({
        'width': 1000,
        'height': 480,
        'type': 'iframe',
		'onStart': function () {
			$('object').css('visibility', 'hidden').addClass('hidden');
		},
		'onClosed': function () {
			$('object.hidden').css('visibility', 'visible').removeClass('hidden');
		}
    });

    $('.action').live('click', function() {
        var that = this;
        $.map(this.className.split(' '), function(className) {
            var classNames = className.split('-');
            var action = classNames.shift();
            var targets = classNames.join('-');

            if (!targets) { return; }

            var elems = targets == 'this' ? $(that) : $('.' + targets);

            switch (action) {
                case 'show': elems.show(); break;
                case 'hide': elems.hide(); break;
                case 'toggle': elems.slideToggle(); break;
                case 'submit': elems.submit(); break;
                case 'focus': elems.filter(':first').focus(); break;
                case 'checkall': elems.attr('checked', true); break;
                case 'uncheckall': elems.attr('checked', false); break;
                case 'togglecheck': elems.attr('checked', $(that).attr('checked')); break;
                case 'activate': elems.addClass('active'); break;
                case 'deactivate': elems.removeClass('active'); break;
                case 'toggleActive': elems.toggleClass('active'); break;
                case 'fadein': elems.fadeIn(); break;
                case 'fadeout': elems.fadeOut(); break;
				case 'slidedown': elems.slideDown(); break;
                case 'slideup': elems.slideUp(); break;
            }
        });
    });

    var trans_speed = 300;

    $('input.default').each(function() {
        var default_val = $(this).attr('default');
        $(this).val(default_val);

        $(this).focus(
            function() {
                if (default_val == $(this).val()) {
                    $(this).val('');
                }
                $(this).addClass('focus');
            }
        ).blur(
            function() {
                if ('' == $(this).val()) {
                    $(this).val(default_val);
                }
                $(this).removeClass('focus');
            }
        );
    });

    $('#slideshow').cycle({
        fx: 'fade',
        pager: '#hero_count',
        // callback fn that creates a thumbnail to use as pager anchor
        pagerAnchorBuilder: function(idx, slide) {
            return '<a href="#" class="dot">' + (idx + 1) + '</a>';
        },
		'requeueOnImageNotLoaded': false
    });

    $('#hero').hover(function() {
        $('#slideshow').cycle('pause');
    }, function() {
        $('#slideshow').cycle('resume');
    });

	var rooftop_timeout;
    $('.rooftop_list li').hover(
		function() {
			clearTimeout(rooftop_timeout);
			$('.rooftop_list li').removeClass('hover');
			
			$(this).addClass('hover') 
		}, 
		function() {
			$self = $(this);
			rooftop_timeout = setTimeout(function () {
				$self.removeClass('hover') 
			}, 500);
		}
	);

    var vehicle_slideshow = {
        fx: 'scrollLeft',
        //pager: '#detail_count',
        next: '#detail_next',
        prev: '#detail_prev',
        after: function(options) {
            $('#detail_index').html($(this).index() + 1);
        }
    }

    $('#detail_images').cycle(vehicle_slideshow);


    $('#color_slides').click(function() {
        $('#detail_images').cycle('stop').hide();

        $('#detail_count').text($('#color_images img').size());

        $('#color_images').show().cycle(vehicle_slideshow);

        return false;
    });

    $('#gallery_slides').click(function() {
        $('#color_images').cycle('stop').hide();
        $('#detail_images').show().cycle(vehicle_slideshow);
        $('#detail_count').text($('#detail_images img').size());

		var objBox = $("a[rel=gallery]");
        objBox.trigger('click');

        return false;
    });
    
    $('#detail_photos').click(function () {
    	$("a[rel=gallery]").trigger('click');

        return false;
    })

    $("a[rel=gallery]").fancybox({
		'onStart': function () {
			$('object').css('visibility', 'hidden').addClass('hidden');
		},
		'onClosed': function () {
			$('object.hidden').css('visibility', 'visible').removeClass('hidden');
		}
	});


    $('#hero .hero_collapse').click(function() {
        hero_hide($(this).closest('.hero_form'));
    });

    $('#hero .hero_expand').click(function() {
        hero_show($(this).closest('.hero_form'));
    });

    if ($('#hero .hero_form').size() > 1) {
        setTimeout(function() {
            //contract the open forms
            $('#hero .hero_form.open').each(function() {
                hero_hide($(this));
            });
        }, 2000)
    }

    var hero_hide = function($elem) {
        $elem.find('table').fadeOut(trans_speed);
        $elem.find('.hero_collapse').fadeOut(trans_speed);

        $elem.delay(trans_speed).animate({
            backgroundPosition: '(0 -140px)'
        }, trans_speed * 2, function() {
            $elem.removeClass('open').addClass('closed');
            $elem.find('.hero_expand').show();
        });

    }

    var hero_show = function($elem) {
        $elem.find('.hero_expand').fadeOut(trans_speed);

        $elem.animate({
            backgroundPosition: '(0px 0px)'
        }, trans_speed * 2, function() {
            $elem.find('table').fadeIn(trans_speed);
            $elem.find('.hero_collapse').fadeIn(trans_speed);

            setTimeout(function() {
                $elem.removeClass('closed').addClass('open');
                $elem.find('.hero_collapse').show()
            }, trans_speed);
        });
    }

    $('.feature_content_med .feature_arrow_left').click(function() {

        var $row = $(this).closest('.feature_content_med'),
        $boxes = $row.find('div.feature');
        //check to see if we can slide
        if (parseInt($boxes.first().css("left")) > 0) {
            return false;
        }

        $boxes.animate({ 'left': '+=640' }, 400);

    });

    $('.feature_content_med .feature_arrow_right').click(function() {

        var $row = $(this).closest('.feature_content_med'),
        $boxes = $row.find('div.feature');

        //check to see if we can slide
        if (parseInt($boxes.last().css('left')) < 600) {
            return false;
        }

        $boxes.animate({ 'left': '-=640' }, 400);

    });

    $('.car_col1_slidecontrols .right').click(function() {

        var $row = $(this).closest('.image_holder'),
        $boxes = $row.find('img.img_cardetail');

        //check to see if we can slide
        if (parseInt($boxes.last().css('left')) < 160) {
            return false;
        }

        $boxes.animate({ 'left': '-=160' }, 400);

    });

    $('.selected_vehicle').click(function() {
        if ($(this).attr('checked')) {
            $(this).addClass('checked');
        }
        else {
            $(this).removeClass('checked');
        }
    });
	
	var closeChat, openChat, toggleChat;
	
	closeChat = function () {
		$('#livechat_contain').animate({
				'width': '65',
				'backgroundPosition': '-461px 0'
			}, {
				'duration': 550
			}
		);

		$('#livechat_outside').animate({
			'left': '-461'
		},{
			'duration': 550,
			'complete': function () {
				$('#livechat_contain').removeClass('open')
			}
		});
		
		//$('#livechat-animated').attr('rel', $('#livechat-animated').attr('src')).attr('src', '');
	}
	openChat = function () {
		$('#livechat_contain').animate({
				'width': '461',
				'backgroundPosition': '0 0'
			}, {
				'duration': 550
			}
		);

		$('#livechat_outside').animate({
			'left': '70'
		},{
			'duration': 550,
			'complete': function () {
				$('#livechat_contain').addClass('open')
			}
		});
		
		//$('#livechat-animated').attr('src', $('#livechat-animated').attr('rel')).attr('rel', '');
	}
	
	toggleChat = function () {
		if($('#livechat_contain').hasClass('open')) {
			closeChat();
		}
		else {
			openChat();
		}
	}
	
	$('#livechat_banner').click(function () {
		toggleChat();
	});
	
	$('#livechathot').click(function () {
		toggleChat();
		
		return false;
	});
	
	$('#livechat_close').click(function () {
		closeChat();
		
		return false;
	});
	
	$('#livechat_submit').click(function () {
		if(typeof ps6r2Vow == 'function') {
			ps6r2Vow();
		}
		
		closeChat();
	});
	
	$('#nav li').hover(function() {
        $(this).addClass('hover');
    }, function() {
    	$(this).removeClass('hover');
    });
	
	$('#detail_video').fancybox();
});

function vehicle_compare() {
	var cars = '';
	
	$('.selected_vehicle.checked').each(function () {
		cars += 'compare[]=' + $(this).val() + '&';
	});
	
	window.location = root + 'vehicle_compare.php?' + cars;
	
	return false;
}

function deselectedAllVehicles() 
{
    $('.selected_vehicle').attr('checked', false);
    return false; //prevent propagation
}

function processPopupSearch(url) {
    var year = $('#pop_years').val();
    var model = $('#pop_model').val();
    var make = $('#pop_make').val();
    var price = $('#pop_price').val();

    parent.window.location = url + "?type=all&years=" + year + "&model=" + model + "&make=" + make + "&price=" + price;
}
