// JavaScript Document

$(function() {


	$('.accordion div.closed_accordion').toggle(function() {
		$(this).css('background','url("images/arrow_down.gif") no-repeat scroll 0 4px');
		$(this).siblings().slideDown('500');
		$(this).children('input.accordion_check').attr('checked','checked');
		return false;
		},
		function() {
		$(this).siblings().slideUp('500');
		$(this).css('background','url("images/arrow.gif") no-repeat scroll 0 4px');
		});

	
	   $('input[type=checkbox][checked]').each(function(){
	   $(this).parents('.result_content').css('display', 'block');
	   $(this).parents('.accordion').children('.closed_accordion').css('background','url("images/arrow_down.gif") no-repeat scroll 0 4px');
	   $(this).parents('.accordion').children('.closed_accordion').attr('class', 'result_heading open_accordion');
	   });
	
	
	
		
	//PRODUCT CELL BORDERS
	
	$('.pro_cell_list:last, .pro_cell:last').css('border-bottom', '1px solid #8B8B8B');
	
	$('.photo_small_cell:nth-child(6n)').css('margin-right','0');
	
	$('.dealer_cell:last').css('border-bottom', '1px solid #8B8B8B');
	
	$('.featured_dealer_cell:last').css('border-bottom', '1px solid #8B8B8B');
	
	$('.pro_cell, .pro_cell_thumb, .pro_cell_list').hover(function() {
		$(this).css('background-color', '#f0f0f0');
		}, function () {
		$(this).css('background-color', '');
		});
	
	
	//CHANGE PRODUCT CELL VIEWING STYLE
	//COOKIES! FOR SAVING LIST STATE ON RESULTS PAGE					

	
	$('.view_option a').click(function() {
		$('.view_option a').removeClass('active');
		$(this).addClass('active');
	});
	
	$('.view1 a').click(function() {
		$('.product_cells span').children('div').attr('class', 'pro_cell');
		$.cookie('default_view', 'active');
		$.cookie('list_view','inactive');
		$.cookie('thumb_view','inactive');
	});
	
	$('.view2 a, .dealerView1 a').click(function() {
		$('.product_cells span, .dealer_cells span').children('div').attr('class', 'pro_cell_thumb');
		$.cookie('thumb_view', 'active');
		$.cookie('list_view','inactive');
		
		$.cookie('default_view','inactive');
	});
	
	$('.view3 a').click(function() {
		$('.product_cells span, .dealer_cells span').children('div').attr('class', 'pro_cell_list');
		$.cookie('list_view', 'active');
		$.cookie('thumb_view','inactive');
		$.cookie('defaul_view','inactive');
	});
	
	//Cookies
	
	var default_view = $.cookie('default_view');
	var thumb_view = $.cookie('thumb_view');
	var list_view = $.cookie('list_view');
	
	//view1 state 
	if (default_view == 'active') {
		$('.product_cells span').children('div').attr('class', 'pro_cell');
		$('.view_option a').removeClass('active');
		$('.view1 a').addClass('active');
		}
	
	if (thumb_view == 'active') {
		$('.product_cells span').children('div').attr('class', 'pro_cell_thumb');
		$('.view2 a').addClass('active');
		
		}
		
	if (list_view == 'active') {
		$('.product_cells span').children('div').attr('class', 'pro_cell_list');
		$('.view3 a').addClass('active');
		}

	
	//HIDE THUMBNAIL IF ONLY ONE
	
	if ($('.photo_small_cell').length < 2) {
		$('#photo_small').css('display', 'none');
		}
	
	
	//TRUNCATE REVIEW TEXT

  	$(".truncate").truncatable({limit: 300, more: '[more]', less: true, hideText: '[hide]'});
	$('.truncate a').click(function(event) {
		event.preventDefault();								
	});
	
	//HIDE OVERFLOW TEXT FOR REGULAR PRODUCT VIEW
	
	$('.detail_truncate').truncatable({limit:200});
	$('.detail_truncate a').css('display', 'none');
	$('.detail_truncate p').append('...');
	
	//TESTIMONIAL BORDER-BOTTOM:NONE FOR LAST PARAGRAPH
	
	$('#tab_content .tab-testimonials p.testimonial:last').css('border-bottom', 'none');
	
	//TESTIMONIAL 'WRITE REVIEW' ACCORDION FORM
	
	$('.review_link').toggle(function() {
		$(this).css('background','url("images/arrow_down.gif") no-repeat scroll 0 2px');
		$('.review_form').slideDown('500');											 
		return false;
		},
		function() {
		$('.review_form').slideUp('500');	
		$(this).css('background','url("images/arrow.gif") no-repeat scroll 0 2px');
		});

	
	//FORM VALIDATOR
	
	$('.textInputLong').siblings('span').addClass('validator');
	
	//INITIALIZE PRICE SLIDER
	
	$( "#slider-range" ).slider({
			range: true,
			min: 0,
			max: 2750,
			values: [ 75, 500 ],
			slide: function( event, ui ) {
				$( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
			}
		});
		$( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
			" - $" + $( "#slider-range" ).slider( "values", 1 ) );
		
		$('.slider_price').css('color', '#C56116');
		
	//RANDOM BACKGROUND IMAGES FOR RESULTS PAGE
	var totalCount = 4;
	var num = Math.ceil( Math.random() * totalCount );

    $('#bodyarea_inner ').css('background','url("images/bg/'+num+'.jpg") left bottom no-repeat');
	
	
	//TABLE FORMATTING 
	
	$('tr td:nth-child(1)').addClass('Col1');
	$('tr td:nth-child(2)').addClass('Col2');
	$('tr td:nth-child(3)').addClass('Col3');
	$('tr td:nth-child(4)').addClass('Col4');
	$('tr td:nth-child(5)').addClass('Col5');
	
	//PIE
	 
	 if (window.PIE) {
		$('table, .cssButtonGreen, .borderRadius').each(function() {
			PIE.attach(this);
		});
	}
	
});
