jQuery(document).ready(function(){
    init_flipNav();
	init_zoom();
	init_slidingShelves();
	init_gatewaySort();
	init_print();
	init_request();
	init_validate();
	init_home();
});

function init_flipNav(){
    for(var i=0;i<jQuery('.flipNav').length;i++){
        for(var j=0;j<jQuery('a',jQuery('.flipNav').eq(i)).length;j++){
            jQuery('a',jQuery('.flipNav').eq(i)).eq(j).data('pos',j);
        }
    }
    jQuery('.flipNav a').click(function(){
        var c=jQuery(this).parents('ul');
        jQuery('.curr',c).removeClass('curr');
        jQuery(this).parents('li').addClass('curr');
        
        c=jQuery(this).parents('.flipNav').parent();
        jQuery('.flipNav ~ .curr',c).removeClass('curr');
        jQuery('.flipBod',c).eq(jQuery(this).data('pos')).addClass('curr');
        
        return false;
    });
}

function init_zoom(){
    var LB_W=620;
    
    jQuery('.action_zoom a').click(function(){
        var _left=(jQuery(document).width()-LB_W)/2;
	    
	    jQuery.blockUI({message:'<div class="imgGallery active">'+jQuery('#imgGalleryContent').html()+'</div>',css:{position:'absolute',left:_left+'px',cursor:'auto',width:'auto',top:'100px',borderWidth:'0px'}});
	    
	    for(var i=0;i<jQuery('.imgGallery.active .input_imgSrc').length;i++){
	        jQuery('.imgGallery.active .view').append(jQuery('<div class="galleryImg"><img src="'+jQuery('.imgGallery .input_imgSrc').eq(i).attr('value')+'" /></div>'));
	    }
	    jQuery('.imgGallery.active .galleryImg:first').addClass('curr');
	    jQuery('.imgGallery .btn_close a').click(function(){
	        jQuery.unblockUI();
	    });
	    
	    init_galleries();
	    
	    return false;
	});
}

function init_galleries(){
	for(var i=0;i<jQuery('.imgGallery').length;i++){
		jQuery('.size',jQuery('.imgGallery').eq(i)).empty();
		jQuery('.size',jQuery('.imgGallery').eq(i)).append(jQuery('.galleryImg',jQuery('.imgGallery').eq(i)).length);
		jQuery('.imgGallery').eq(i).data('pos',0);
		jQuery('.nav_gallery li a',jQuery('.imgGallery').eq(i)).eq(0).click(function(){
			return gallery_recede(jQuery(this).parents('.imgGallery'));
		});
		jQuery('.nav_gallery li a',jQuery('.imgGallery').eq(i)).eq(1).click(function(){
			return gallery_advance(jQuery(this).parents('.imgGallery'));
		});
	}
}

function init_slidingShelves(){
	var i,curShelf,numProducts,productSetInnerW;
	var PRODS_PER_SLIDE=3;
	var PRODS_PER_VIEW=4;
	var PROD_WIDTH=149;
	var PROD_MARGIN=22;
	var T_TRANSIT=500;

	for(i=0;i<jQuery('.slidingShelf').length;i++){
		// Determine number of products, set sliding width
		curShelf=jQuery('.slidingShelf').eq(i);
		numProducts=jQuery('.product',curShelf).length;
		curShelf.data('numProducts',numProducts);
		productSetInnerW=numProducts*PROD_WIDTH + (numProducts-1)*PROD_MARGIN;
		jQuery('.productSetInner',curShelf).css('width',productSetInnerW);

		// Disable slide-next if not enough products, forget about animations
		if(numProducts<=PRODS_PER_VIEW){
			jQuery('.btn_next a',curShelf).addClass('disabled');
			jQuery('.btn_prev a',curShelf).click(function(){
				return false;
			});
			jQuery('.btn_next a',curShelf).click(function(){
				return false;
			});
		}
		else{
			// Attach animation events
			jQuery('.btn_prev a',curShelf).click(function(){
				var curShelf=jQuery(this).parents('.slidingShelf');
				var curProductSetInner=jQuery('.productSetInner',curShelf);
				var newPos=parseInt(curProductSetInner.css('marginLeft')) + (PROD_WIDTH+PROD_MARGIN)*PRODS_PER_SLIDE;
				var numProducts=curShelf.data('numProducts');
	
				// Reached beginning?
				if(newPos>0)
					newPos=0;
				
				// Do animation
				newPos+="px";
				curProductSetInner.animate({marginLeft:newPos},T_TRANSIT,function(){				
					if(parseInt(newPos)>=0)
						jQuery('.btn_prev a',curShelf).addClass('disabled');
					if(Math.abs(parseInt(newPos))<(PROD_WIDTH+PROD_MARGIN)*(numProducts-PRODS_PER_VIEW))
						jQuery('.btn_next a',curShelf).removeClass('disabled');
				});
				
				return false;
			});
			jQuery('.btn_next a',curShelf).click(function(){
				var curShelf=jQuery(this).parents('.slidingShelf');
				var curProductSetInner=jQuery('.productSetInner',curShelf);
				var newPos=parseInt(curProductSetInner.css('marginLeft')) - (PROD_WIDTH+PROD_MARGIN)*PRODS_PER_SLIDE;
				var numProducts=curShelf.data('numProducts');
	
				// Reached end?
				if(Math.abs(newPos)>(PROD_WIDTH+PROD_MARGIN)*(numProducts-PRODS_PER_VIEW)){
					newPos=(PROD_WIDTH+PROD_MARGIN)*(numProducts-PRODS_PER_VIEW)*(-1);}
					
				// Do animation
				newPos+="px";				
				curProductSetInner.animate({marginLeft:newPos},T_TRANSIT,function(){						  
					if(parseInt(newPos)<0)
						jQuery('.btn_prev a',curShelf).removeClass('disabled');
					if(Math.abs(parseInt(newPos))>=(PROD_WIDTH+PROD_MARGIN)*(numProducts-PRODS_PER_VIEW))
						jQuery('.btn_next a',curShelf).addClass('disabled');
				});
				
				return false;
			});
			jQuery('.slidingShelf .btn a').hover(function(){
			    jQuery(this).addClass('hover');
			},
			function(){
			    jQuery(this).removeClass('hover');
			});
		}
	}
	
	jQuery('.slidingShelf .product a').hover(function(){
		jQuery(this).parents('.product').addClass('hover');
	},
	function(){
		jQuery(this).parents('.product').removeClass('hover');
	});
}

function init_print(){
    jQuery('.util_print a').click(function(){
        window.print();
        
        return false;
    });
}

function init_request(){
    var LB_W=490;
    
    jQuery('#detailUtilities .util_request a').click(function(){
        var _left=(jQuery(document).width()-LB_W)/2;
        
        jQuery.blockUI({message:'<div id="sampleRequestForm" class="lightbox"></div>',css:{position:'absolute',left:_left+'px',cursor:'auto',width:'auto',top:'100px',borderWidth:'0px'}});
        jQuery('#sampleRequestForm').load('/GetStarted/RequestSample',function(){
            jQuery('.lnk_close a').click(function(){
	            jQuery.unblockUI();
	        });
	        init_validate();
	    });
	    
	    return false;
    });
}

function init_validate(){
    var THINK_TIME=500;
    
    if(jQuery('.offensive').length>0){
        window.scrollTo(0,0);
        jQuery('#errMsg').show(THINK_TIME);
    }
    
    jQuery('form .required').blur(function(){
        var t;
        var el=jQuery(this);
        var c=el.parent('.field');
        
        c.removeClass('offensive');
        c.removeClass('pass');
        c.addClass('processing');
        
        t=setTimeout(function(){
            clearTimeout(t);
            c.removeClass('processing');
            if(!validateInput(el))
                c.addClass('offensive');
            else
                c.addClass('pass');
        },THINK_TIME);
    });
    jQuery('form').submit(function(){
        var el;
        var c=jQuery(this);
        window.scrollTo(0,0);
        
        for(var i=0;i<jQuery('.required',c).length;i++){
            el=jQuery('.required',c).eq(i);
            
            if(!validateInput(el))
                el.parent('.field').addClass('offensive');
            else
                el.parent('.field').addClass('pass');
        }
            
        // If reCAPTCHA is the only offender, submit anyway
        if(jQuery('.offensive',c).length==1 && jQuery('#recaptchaWrap').hasClass('offensive'))
            return true;
        else if(jQuery('.offensive',c).length>0){
            jQuery('#errMsg').show(THINK_TIME);
            return false;
        }
        
        return true;
    });
}

function validateInput(el){
    var v=el.attr('value');
    
    if(v=='')
        return false;
    else if(el.hasClass('v_email') && !/^([\w\.!#\$%\-+.'_]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/.test(v))
        return false;
    
    return true;
}

function init_home(){
    if(jQuery('#featuredCaseStudies').length==1){
        var flashvars = {};
	    var params = {};
	    var attributes = {};
	    
	    flashvars.xmlPath = "/Content/xml/";
	    flashvars.imgPath = "/Content/img/featuredCaseStudies/";
	    attributes.id = "featuredCaseStudies";
	    
	    swfobject.embedSWF("/Content/swf/corp_top_v1.swf", "featuredCaseStudies", "924", "464", "8.0.0", false, flashvars, params, attributes);
    }
}

function init_gatewaySort(){
    jQuery('#form_sort select').change(function(){
    jQuery('#input_sortBy').attr('value', jQuery(this).attr('value'));
    window.location = '/case-studies/' + jQuery(this).attr('value') + '/1' 
        //jQuery(this).parents('form').submit();
    });
}

function gallery_recede(g){
	if(g.data('pos')>0){
		gallery_goto(g,g.data('pos')-1);
		gallery_setLims(g);
	}
	return false;
}

function gallery_advance(g){
	if(g.data('pos')<jQuery('.galleryImg',g).length-1){
		gallery_goto(g,g.data('pos')+1);
		gallery_setLims(g);
	}
	return false;
}

function gallery_goto(g,dest){
	g.data('pos',dest);
	jQuery('.curr',g).removeClass('curr');	
	jQuery('.galleryImg',g).eq(dest).addClass('curr');
	jQuery('.pos',g).text(dest+1);
}

function gallery_setLims(g){
	if(g.data('pos')==0)
		jQuery('.nav_gallery li',g).eq(0).addClass('disabled');
	else
		jQuery('.nav_gallery li',g).eq(0).removeClass('disabled');
		
	if(g.data('pos')>=jQuery('.galleryImg',g).length-1)
		jQuery('.nav_gallery li',g).eq(2).addClass('disabled');
	else
		jQuery('.nav_gallery li',g).eq(2).removeClass('disabled');
}
