var slideshowTimer = null;

$(document).ready(function(){

	$("ul#panels").carousel({
		perPage: 1,
		autoScroll: true,
		nextWrap: true
	});
	
	$(".pagenav").find("li:last").addClass("last");
	
	if(location.pathname == "/" && location.search == ""){
		$(".pagenav li.first").addClass("current_page_item");
	}
	
	$('div.testimonials div.test-content').filter(':lt(2)').show();
	$('#more').click(function(){
		if($(this).html() == 'Read More'){
			$(this).html('Read Less').removeClass('more').addClass('less');
			$('div.testimonials div.test-content').show();
		} else if($(this).html() == 'Read Less') {
			$(this).html('Read More').removeClass('less').addClass('more');
			$('div.testimonials div.test-content').filter(':gt(1)').hide();
			$('html,body').animate({ scrollTop: 0}, 500, 'swing');
		}
	});


	$('.sidebar-nav UL LI').each(function(){
		if ($(this).children('A').html() == 'store') $(this).append($('.category-list').html());
	});
	$('.category-list').html('');
	
	$('#sidebar ul li ul li.active').parent().parent().addClass('active-parent');
	//$('#sidebar ul li.current_item_parent').addClass('active-parent');
	
	Cufon.set('fontFamily', 'copperplate').replace('#header p');
	Cufon.set('fontFamily', 'kozuka-light').replace('h2', {hover:true});
	Cufon.set('fontFamily', 'kozuka-extralight').replace('h1');
	Cufon.set('fontFamily', 'kozuka-light').replace('h4');
	Cufon.set('fontFamily', 'leelawad').replace('h3')('#sidebar>.sidebar-nav>ul>li>a', {hover:true});
	Cufon.set('fontFamily', 'leelawad').replace('p.h1')
	Cufon.set('fontFamily', 'kozuka-light').replace('p.intro');
	Cufon.set('fontFamily', 'kozuka-light').replace('div.test-text p');
	$('div.wpcf7-response-output').replaceWith('');
	$('.wpcf7').prepend('<div class="wpcf7-response-output wpcf7-display-none"></div>');
	
	$('#sidebar>.sidebar-nav>ul>li').each(function(){
		$(this).removeClass('active_parent');
	});
	
	$('#sidebar>.sidebar-nav>ul>li.item_parent>a').click(function(){
		/*$('#sidebar ul li ul').hide();
		$(this).next('ul').slideDown();
		
		return false;*/
	});
	
	if($('#rotator')){
		//doSlideshow('rotator', true, 5000);
	};
	
	$('#content .product-details .wpcart_gallery a, #content .product-details .picture>a').removeClass('thickbox');
	
	$('#content .product-details .wpcart_gallery a').click(function(){
		
		var obj = new Image();
		var src = $(this).attr('href');
		obj.onload = function(){
			$('#content .product-details .picture IMG.product_image').attr('src', src);
			$('#content .product-details .picture a.preview_link').attr('href', src);
		};
		obj.src = $(this).attr('href');
		return false;
	});	
	
	$('#content .product-details a.preview_link').lightBox({
		imageLoading:			'../../../wp-content/themes/munitio/public/images/lightbox/lightbox-ico-loading.gif',
		imageBtnPrev:			'../../../wp-content/themes/munitio/public/images/lightbox/lightbox-btn-prev.gif',	
		imageBtnNext:			'../../../wp-content/themes/munitio/public/images/lightbox/lightbox-btn-next.gif',
		imageBtnClose:			'../../../wp-content/themes/munitio/public/images/lightbox/lightbox-btn-close.gif'
	});
	
	addNotifyForm();
	
	
	$('.dealers li').each(function(i){
		if ( i == 2 || (i % 3) == 2 )
		$(this).css('margin', '0');
	});
	
	
});

function doSlideshow(objid, start, time){
	if(!$('#' + objid) || !isVisible($('#' + objid))){
		if(slideshowTimer != null){
			clearTimeout(slideshowTimer);
			slideshowTimer = null;
		}
		return;
	}
	
	if(start){
		clearTimeout(slideshowTimer);
		slideshowTimer = null;
	}
	
	var currObj = $('#' + objid + ' li.current');
	var nextObj = currObj.next();
	if($('#' + objid + ' li:last').hasClass('current')){ 
		nextObj = $('#' + objid + ' li:first');
	}
	
	nextObj.addClass('current');
	nextObj.css('opacity', 0);
	currObj.removeClass('current');
	currObj.animate({opacity:0}, 2000);
	nextObj.animate({opacity:1}, 2000);
	

	slideshowTimer = setTimeout('doSlideshow("' + objid + '", false, ' + time + ');', time);
}

function isVisible(obj) { return (obj.css('display') == 'block'); }

function addNotifyForm(){
    $('#notify_form, #add_dealers').submit(function(){
		var form = $(this);
		var formData = $(this).serialize();
		var error = '';
		var formId = $(this).attr('id');
        var err = form.find('.error');
        err.slideUp('normal');
            $.ajax({
                type: 'POST', url: form.attr('action'), data: formData,
                success: function ( responseData ){
                    if((error = Process(responseData)) != ''){
                        err.html(error);
                        err.css('color','#aa0000');
                        err.slideDown('normal');                        
                    } else{
						if ( formId == 'add_testimonials' ){
							err.html('Thank you for submitting your testimonial!  We will review and confirm your comments.');
						} else if ( formId == 'add_dealers' ) {
							err.html('Thank you! We will review your information and contact you with any questions prior to posting your dealer information.');
						} else {
							err.html('Thank you! We will notify you via email when this product will be available.');
						}
                        err.css('color','green');
                        err.slideDown('normal');                      
                        document.getElementById(formId).reset();
                    }
                }
        });		
        
		return false;
	});
}
function Process(str){
	list = str.split(':');
	if(list[0].toLowerCase() != 'done'){
		return list[1];
	} else {
		return '';
	}
}
