var gallery_loop;
function slideSwitch() {
    var $active = $("div.color-select ul li a.hover");

    if ( $active.length == 0 ) $active = $('div.color-select ul li:last a');

    // use this to pull the images in the order they appear in the markup
    var $element = $active.closest('li').next().find('a');
    var $next =  $element.length ? $element
        : $("div.color-select ul li:first a");

    $("div.color-select a").removeClass('hover');
    $next.addClass('hover');
    $("div.color-select img.sample").removeClass('active');
    $("div.color-select img." + $next.attr('rel') + "").css({opacity: 1.0})
    .addClass('active');
    gallery_loop = setTimeout('slideSwitch();', 5000);
}

$(document).ready(function(){
    gallery_loop = setTimeout('slideSwitch();', 5000);
    $("div.color-select a").click(function() {
        clearTimeout(gallery_loop);
        //switch the background
        $("div.color-select a").removeClass('hover');
        $(this).addClass('hover');
        $("div.color-select img.sample").removeClass('active');
        $("div.color-select img." + $(this).attr('rel') + "").css({opacity: 1.0})
        .addClass('active');
        gallery_loop = setTimeout('slideSwitch();', 10000);
    });
    
    $("div.colors-select ul li a").hover(function() {
    	//$(this).attr('src', '/images/colorchooser/'+$(this).parent().attr('rel')+'-glow.gif');
    	$('.active').removeClass('active');
    	$('#'+$(this).attr('rel')).addClass('active');
    }/*, function() {
    	$(this).attr('src', '/images/colorchooser/'+$(this).parent().attr('rel')+'.gif');
    }*/);
});

$(function() {

	$('#fitment_menu li').hover(
			function() {
				$(this).children('div').show();
				var popup = $(this).children('div:first');
				var diff = parseInt((popup.offset().top + popup.height())
						- ($(window).height() + $(window).scrollTop()));
				if (diff >= 0) {
					popup.css('top', -diff - 50);
				} else {
					popup.css('top', 0);
				}
			}, function() {
				var popup = $(this).children('div:first');
				popup.css('top', 0);
				$(this).children('div').hide();
			});

	$('#navigation_menu > li').hover(
			function() {
				$(this)
						.css('background',
								'url("/images/menu_hover.gif") right');
				// $(this).next('li').css('background',
				// 'url("/images/main-nav-bg.gif") right');
				$(this).children('ul').css('left', '0');

			},
			function() {
				$(this).children('ul').css('left', '-999em');
				// $(this).next('li').css('background',
				// 'url("/images/main-nav-bg.gif") right');
				$(this).css('background',
						'url("/images/main-nav-bg.gif") right');
				// $(this).children('ul').children('li').children('a').css('color',
				// '#3a3a3a');
				if ($(this).hasClass('end_menu')) {
					$(this).css('background',
							'url("/images/main-nav-bg-end.gif") right');
				}
			});

	$(
			'#navigation_menu > li > ul > li > a, #navigation_menu > li > ul > li >div ul > li > a')
			.hover(
					function() {
						$(this).css('font-weight', 'normal').css('color',
								'#fff').css('background-color', '#1a89d0');// );
					},
					function() {
						$(this).css('font-weight', 'normal').css('color',
								'#3a3a3a').css('background', 'none');
						;
					});

	$('#fitment_menu li div').hover(function() {
		// toggle parent 'hover'
		$(this).prev('a').addClass('fake_hover');
	}, function() {
		// remove parent hover
		$(this).prev('a').removeClass('fake_hover');
	});
	
	$('.stage_number_fitment').each( function (i) {
		if ($(this).html() == 1)
		{
			$(this).removeClass('stage_number_incomplete').addClass('stage_number_complete');
		}
	});

	$('.application').live(
			'click',
			function() {
				if ($(this).is(':checked')) {
					$(this).nextAll('select').removeAttr('disabled').css(
							'border', '1px solid #ff0000');
				} else {
					$(this).nextAll('select').attr('disabled', 'disabled').css(
							'border', '1px solid #ccc');
				}
				var flag = false;
				$('.application').each(
						function(i, index) {
							if ($(this).is('[type="radio"]')) {
								if (!$(this).is(':checked')) {
									$(this).nextAll('select').attr('disabled',
											'disabled');
								}
							}

							if ($(this).is(':checked')) {
								flag = true;
							}
						});
				if (flag == false) {
					$('.ballast').attr('disabled', 'disabled');
					$('#circ5').addClass('stage_number_incomplete')
							.removeClass('stage_number_complete');
					$('.warranty').attr('disabled', 'disabled');
					$('#circ6').addClass('stage_number_incomplete')
							.removeClass('stage_number_complete');
				}

			});

	$('.fitment_select').live(
			'change',
			function() {
				var flag = true;
				$(this).next().next().removeClass('stage_number_incomplete').addClass('stage_number_complete');
				$('.fitment_select').each(function(i, index) {
					if ($(this).val() >= 0) {
						flag = false;
					}
				});

				if (flag == true) {
					// enable next lot of options

				} else {
					// disable em all
					$('.hid_options input, .hid_options select').attr(
							'disabled', 'disabled');
				}
			});

	$('.stock').live(
			'click',
			function() {
				var flag = false;
				$('.stock').each(function(i, index) {
					if ($(this).is(':checked')) {
						flag = true;
					}
				});
				if (flag == true) {
					$('#gogo_btn').removeAttr('disabled');
					$('#step6 p:first').css('color', '#000');
					$('#circ6').removeClass('stage_number_incomplete')
							.addClass('stage_number_complete');
				}
			});

	$('.color').live(
			'change',
			function() {
				var flag = false;
				$('.color').each(
						function(i, index) {
							if (!$(this).attr('disabled')
									&& $(this).val() != '') {
								if ($('.ballast').attr('disabled')) {
									$('#step4 p:first').css('color', '#000');
									$('.ballast').removeAttr('disabled');
									$('#circ5').removeClass(
											'stage_number_incomplete')
											.addClass('stage_number_complete');
								}
								flag = true;
							}
						});
				if (flag == false) {
					$('.ballast').attr('disabled', 'disabled');
					$('#circ5').addClass('stage_number_incomplete')
							.removeClass('stage_number_complete');
				}
			});

	$('.ballast').live(
			'change',
			function() {
				if ($(this).val().length > 0) {
					$('.warranty').removeAttr('disabled');
					$('#step5 p:first').css('color', '#000');
					$('#circ6').removeClass('stage_number_incomplete')
							.addClass('stage_number_complete');
				} else {
					$('.warranty').attr('disabled', 'disabled');
					$('#circ6').addClass('stage_number_incomplete')
							.removeClass('stage_number_complete');
				}
			});

	$('.warranty').live(
			'change',
			function() {
				if ($(this).val().length > 0) {
					$('.stock').removeAttr('disabled');
					$('#step6 p:first').css('color', '#000');
					$('#circ7').removeClass('stage_number_incomplete')
							.addClass('stage_number_complete');
				} else {
					$('.stock').attr('disabled', 'disabled');
					$('#circ7').addClass('stage_number_incomplete')
							.removeClass('stage_number_complete');
				}
			});
});

function chooseNodeCallBack(year, make, model) {
	if ($('#hid_selector').length > 0) {
		// on option select page
		var data = {};
		$.post('/cart/options/loadOptions/', data, function(data) {
			$('#hid_options').html(data.content);
		}, 'json');

		$('.stage_number_fitment, #circ4').removeClass(
				'stage_number_incomplete').addClass('stage_number_complete');
	}
	// console.log(year);
	// console.log(make);
	// console.log(model);
	// window.location = ''
}

function toggle4() {
	if ($('#step4').hasClass('disabled')) {
		// enable everything
		$('#step4').find('input').removeAttr('disabled');
	} else {
		// disable
		$('#step4').find('input').attr('disabled', 'disabled');
	}
}

function switchImg(url) {
	$('#gallery-big').children('img').attr('src', url);
}

function checkPreCart() {
	// check beams and colours
	var valid = false;
	$('.application').each(function(i, index) {
		if ($(this).is(':checked')) {
			valid = true;
		}
	});
	if (valid == false) {
		$('#step4 p:first').css('color', '#ff0000');
		return false;
	}

	var valid = false;
	$('.ballast').each(function(i, index) {
		if ($(this).is(':checked')) {
			valid = true;
		}
	});
	if (valid == false) {
		$('#step5 p:first').css('color', '#ff0000');
		return false;
	}

	var valid = false;
	$('.warranty').each(function(i, index) {
		if ($(this).is(':checked')) {
			valid = true;
		}
	});
	if (valid == false) {
		$('#step6 p:first').css('color', '#ff0000');
		return false;
	}

	var valid = false;
	$('.stock').each(function(i, index) {
		if ($(this).is(':checked')) {
			valid = true;
		}
	});
	if (valid == false) {
		$('#step7 p:first').css('color', '#ff0000');
		return false;
	}

	return true;
}
function openBallastPopup() {
	$('#ballast-popup').css({
		'display':'block'
	}).animate({
		"right": "0px"
		},
		"slow");
}
function closeBallastPopup() {
	$('#ballast-popup').animate({
		"right": "-587px"
		},
		"slow", function(){
			$('#ballast-popup').css({
				'display':'none'
			});
		});
}

function setPage(){
	$(location).attr('href',"/cart/options");
}

//	Javascript for bulb selection page

$('.bulb-color').live('change',function() {
				var flag = false;
				if (!$(this).attr('disabled') && $(this).val() != '') {
					if ($('.bulb-ballast').attr('disabled')) {
						$('#step3 p:first').css('color', '#000');
						$('.bulb-ballast').removeAttr('disabled');
						$('#circ3').removeClass('stage_number_incomplete').addClass('stage_number_complete');
					}
					flag = true;
				}
				
			  if (flag == false || $(this).val() == '') {
					$('.bulb-ballast').attr('disabled', 'disabled');
					$('#step3 p:first').css('color', '#555');
					$('#circ3').addClass('stage_number_incomplete').removeClass('stage_number_complete');
					
					$('#step4 p:first').css('color', '#555');
					$('#circ4').addClass('stage_number_incomplete').removeClass('stage_number_complete');
				}
			});

$('.bulb-ballast').live(
			'change',
			function() {
				if ($(this).val().length > 0) {
					$('.bulb-warranty').removeAttr('disabled');
					$('#step4 p:first').css('color', '#000');
					$('#circ4').removeClass('stage_number_incomplete').addClass('stage_number_complete');
				} else {
					$('.bulb-warranty').attr('disabled', 'disabled');
					$('#circ4').addClass('stage_number_incomplete').removeClass('stage_number_complete');
				}
			});

function checkBulbSelect() {
	var valid = false;
	if($('.bulb-color').val() != ""){ //check color
		valid = true;
	}else {
		$('#step2 p:first').css('color', '#ff0000');
		$('.stage_number_fitment').removeClass('stage_number_error');
		$('#circ2').addClass('stage_number_error');
		return false;
	}
	
	//checks ballast
	var valid = false;
	$('.bulb-ballast').each(function(i, index) {
		if ($(this).is(':checked')) {
			valid = true;
		}
	});
	if (valid == false) {
		$('#step3 p:first').css('color', '#ff0000');
		$('.stage_number_fitment').removeClass('stage_number_error');
		$('#circ3').addClass('stage_number_error');
		return false;
	}
	
	//check warranty
	var valid = false;
	$('.bulb-warranty').each(function(i, index) {
		if ($(this).is(':checked')) {
			valid = true;
		}
	});
	if (valid == false) {
		$('#step4 p:first').css('color', '#ff0000');
		$('.stage_number_fitment').removeClass('stage_number_error');
		$('#circ4').addClass('stage_number_error');
		return false;
	}
	return true;
}

//SkyJax
$(document).ready(function(){
	if(typeof skyJax != 'undefined'){ //checks to see if skyJax has been instanciated
		$('#description').skyJax({
			triggers: '#hidsize|change',
			source: '@bulb_ajax',
			params: { size: $('#hidsize option:selected').liveVal() }
		});
		
		$('#side_content').load('/bulbSelect/sidecontent/');
	}
});
