﻿var fullyLoaded = {

    formSubmitted: false,
    formError: false,
    
    init: function() {
    
	if($.browser.msie && $.browser.version < 7)
		PngFix();

        // popup init
        $('#optinLink').click(function(e) { $('#popWrap').fadeIn(); e.preventDefault(); } );
        $('#closePop').click(function(e) { $('#popWrap').fadeOut(); e.preventDefault(); } );
    
    

        $('#form').submit(fullyLoaded.validate);    
        
        fullyLoaded.initTab('Offers');
        fullyLoaded.initTab('Compare');
        fullyLoaded.initTab('Photos');
        fullyLoaded.initTab('Form');

 	// add click handler to eBrochure button
	if($('#eBrochure')) {
		
		$('#eBrochure').click(function(e) { fullyLoaded.selectTab('Form'); e.preventDefault(); } );
	}
        
        
        $('div.brochureBtn').each(function (i, el) { 
            
            $(el).click(function(e) {
                fullyLoaded.selectTab('Form');
                e.preventDefault();
            }); 
        });
        
        $('div.testDrive').each(function (i, el) { 
            
            $(el).click(function(e) {
                fullyLoaded.selectTab('Form');
                e.preventDefault();
            }); 
        });
        
        
        
        
        $('div.eBrochure').each(function (i, el) { 
            
            $(el).click(function(e) {
                fullyLoaded.selectTab('Form');
                e.preventDefault();
            }); 
        });

        
        fullyLoaded.initGallery(); 

	    // brochure/test drive tab shown initially (for non-JS enabled users)
	    // but we want to show offers tab first
	    if(!fullyLoaded.formSubmitted)
	        fullyLoaded.selectTab('Offers');
    },
    
    initTab: function(tabName) {
    
        if($('#tab' + tabName)) {

            $('#tab' + tabName).click(function (e) { 
            
                fullyLoaded.selectTab(tabName);
                e.preventDefault();
            });
        }
    },
    
    initGallery: function() {
    
        $('.thumbnailVid #pic1').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/caliber_large_vid1.jpg');
        });
        
        $('.thumbnailVid #pic2').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/caliber_large_vid2.jpg');
            
        });
        
        $('.thumbnailVid #pic3').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/caliber_large_vid3.jpg');
        });    
        $('.thumbnailVid #pic4').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/nitro_large_vid1.jpg');
        });
        
        $('.thumbnailVid #pic5').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/nitro_large_vid2.jpg');
            
        });
        
        $('.thumbnailVid #pic6').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/nitro_large_vid3.jpg');
        });    
        $('.thumbnailVid #pic7').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/avenger_large_vid1.jpg');
        });
        
        $('.thumbnailVid #pic8').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/avenger_large_vid2.jpg');
            
        });
        
        $('.thumbnailVid #pic9').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/avenger_large_vid3.jpg');
        });
                $('.thumbnailVid #pic10').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/grandcherokee_large_vid1.jpg');
        });
        
        $('.thumbnailVid #pic11').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/grandcherokee_large_vid2.jpg');
            
        });
        
        $('.thumbnailVid #pic12').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/grandcherokee_large_vid3.jpg');
        });

 
        $('.thumbnailVid #pic13').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/patriot_large_vid1.jpg');
        });


 
        $('.thumbnailVid #pic14').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/patriot_large_vid2.jpg');
        });


 
        $('.thumbnailVid #pic15').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/patriot_large_vid3.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
		
		
		
 
        $('.thumbnailVid #pic16').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/300c_large_vid1.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
		
		
		
 
        $('.thumbnailVid #pic17').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/300c_large_vid2.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
		
        $('.thumbnailVid #pic18').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/300c_large_vid3.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic19').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/grandvoyager_large_vid1.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic20').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/grandvoyager_large_vid2.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic21').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/grandvoyager_large_vid3.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic22').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/sebring_large_vid1.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic23').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/sebring_large_vid2.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic24').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/sebring_large_vid3.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
		
		
		
		     $('.thumbnailVid #pic25').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/voyager_large_vid1.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic26').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/voyager_large_vid2.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic27').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/voyager_large_vid3.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
		
		
				
		
		
		     $('.thumbnailVid #pic28').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/300ctouring_large_vid1.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic29').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/300ctouring_large_vid2.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
        $('.thumbnailVid #pic30').mouseover(function(e) {
        
            $('#largeVid img').attr('src', 'images/300ctouring_large_vid3.jpg');
        });

		$('.thumbnailVid').each(function (i, el) {
		
		    $(el).click(function(e) {
	                e.preventDefault();
	            });
			
		}); 
		
		
		
		
		
		
    },
    
    selectTab: function(tabName) {

        // remove selected tab style
        $('#mainNav a').each(function (i, el) { $(el).removeClass('current'); });
                
        // hide all content panels
        $('#contentWrap > div').each(function(i, el) { $(el).hide(); });
                
        // add selected tab style
        $('#tab' + tabName).addClass('current');
                
        // show selected content panel
        $('#panel' + tabName).show();
		
	if($.browser.msie && $.browser.version < 7)
		PngFix();
    },
    
    validate: function() {
    
        fullyLoaded.formError = false;
        
        fullyLoaded.check('#ucFullyLoadedForm_chkBrochure');
        fullyLoaded.check('#ucFullyLoadedForm_chkTestDrive');
        
        fullyLoaded.check('#ucFullyLoadedForm_ddlTitle');
        fullyLoaded.check('#ucFullyLoadedForm_txtForename');
        fullyLoaded.check('#ucFullyLoadedForm_txtSurname');
        fullyLoaded.check('#ucFullyLoadedForm_txtHouse');
        fullyLoaded.check('#ucFullyLoadedForm_txtPostcode');
        fullyLoaded.check('#ucFullyLoadedForm_txtTelephone');
        fullyLoaded.check('#ucFullyLoadedForm_txtEmail');
        
        
        hasOption = false;
        
        $('td.choose').removeClass('error'); 
        
        $("td.choose").find("input[@type$='checkbox']").each(function(){
                
                if(!hasOption)
                    hasOption = this.checked;
        });
            
        if(!hasOption)
        {
            fullyLoaded.formError = true;    
        }
        
        if(fullyLoaded.formError)
        {
            alert("Please choose an option and ensure you have completed all fields.");
        }

        return !fullyLoaded.formError;
    },
    
    check: function(el) {
        
        if($(el)) {

            if($(el).val().length === 0) {
            
                fullyLoaded.formError = true;         
                
                $(el).addClass('error');
            }         
            else {
                $(el).removeClass('error');
            }   
        }   
    }
};


$(document).ready(function(){
    	fullyLoaded.init();	
});


function PngFix()
{

var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if ((version >= 5.5) && (document.body.filters)) 
{
   for(var i=0; i<document.images.length; i++)
   {
      var img = document.images[i]
      var imgName = img.src.toUpperCase()

      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
      {
			isElementVisible = true;
			
		 	$(document.images[i]).parents().each(function(i, el) {
			
				if($(el).css('display') == "none")
					isElementVisible = false;			
			});
			
			if(isElementVisible)
			{
				 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		         var imgStyle = "display:inline-block;" + img.style.cssText 
		         if (img.align == "left") imgStyle = "float:left;" + imgStyle
		         if (img.align == "right") imgStyle = "float:right;" + imgStyle
		         if (img.parentElement && img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
		         var strNewHTML = "<span " + imgID + imgClass + imgTitle
		         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		         img.outerHTML = strNewHTML
		         i = i-1		
			}         
      }
   }
}

}