$(function() {
    
	//add js class to the body
    $('body').addClass('hasJS');
	
	//set up the lightbox
    $(".screen a, a.sponsor").fancybox({
        'transitionIn': 	'elastic',
        'transitionOut': 	'elastic',
        'overlayColor': 	'#000',
		'titleShow': 		true,
		'titlePosition':	'inside'
    });

	$("a.video").fancybox({
		'width'				: 628,
		'height'			: 362,
        'autoScale'     	: false,
        'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});



    //if we have JS then remove the static map build the jquery map
    if ($('body').hasClass('hasJS')) {
        $('div').remove('#staticMap');
        $('body.home-contact .cols3').prepend('<div id="map"></div>');
            $("#map").gMap({ markers: [{ 
                latitude:   53.0609181587198,
                longitude:  -2.224527597427368,
                html:       "<div id='popup'><strong>Jigsaw Finance Limited</strong>Genesis Centre<br />Innovation Way<br />Stoke on Trent<br />ST6 4BF<br /><br /><strong>Sat Nav:</strong> ST6 4PX</div>"
                //popup: true
            }],
                zoom: 12
            });
    }
	
	// unobtrusively add placeholder text
	$("input[type=text][title],textarea[title]")
        .each( showPlaceholder ) // initialize each control on page load
        .blur( showPlaceholder )
        .focus( hidePlaceholder );
 
    /*$("form").submit( function() {
			$( "input[type=text][title],textarea[title]", this ).each(function(){
								//var $qwert = $(this).attr("title");	
								//var $qwertval = $(this).val();	
								//console.log($qwert +" || "+ $qwertval);
								
								if($(this).val == $(this).attr("title") ){
										console.log($(this).attr("title") + " this is ok");
								} else {
										console.log($(this).attr("title") + " this is NOT ok");	
								}												   
			});
		});*/
			
	$("form").submit( function() {
				$( "input[type=text][title],textarea[title]", this ).each(hidePlaceholder);	
			});
	
    function showPlaceholder() {
    	var $control = $(this);
        var placeholderText = $control.attr("title");
        if ( $control.val() === "" || $control.val() === placeholderText ) {
        	$control.addClass("placeholder");
            $control.val(placeholderText);
        }
    };
    function hidePlaceholder() {
    	var $control = $(this);
        if ( $control.val() === $control.attr("title") ) {
        	$control.removeClass("placeholder");
        	$control.val("");
        }
    }

	// form validation call
	$("#feedback form").validate();
	
	// detect OS to fix font sizes for non system fonts on mac
	if($.client.os == 'Mac'){
		$('head').append('<link rel="stylesheet" href="/content/mac-os.css" type="text/css" />');
	}
	
    // check if flsh is installed, then display the login page - if it isnt, then display the config help page
    $("#nav ul li.n6").click(function(e) {
        e.preventDefault();
        function getFlashVersion(){
			  // ie
			  try {
				try {
				  // avoid fp6 minor version lookup issues
				  // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
				  var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
				  try { axo.AllowScriptAccess = 'always'; }
				  catch(e) { return '6,0,0'; }
				} catch(e) {}
				return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
			  // other browsers
			  } catch(e) {
				try {
				  if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
					return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
				  }
				} catch(e) {}
			  }
			  return '0,0,0';
			}
			//find out which version of flash they have - if lower than 7.. for example 6, then tell them and redirect to the support page 
			var version = getFlashVersion().split(',').shift();
			var versionNo = 7;
			if(version < versionNo){
			   alert("Sorry - you must have at least version " + versionNo + " of Flash installed - please get / update your version now, or refer to the following support pages");
			  window.open('https://jigsaw.yourcarloan.co.uk/DPS/core/config.html');
			  //$(location).attr('href','https://jigsaw.yourcarloan.co.uk/DPS/core/config.html');
			}else{
			  window.open('https://jigsaw.yourcarloan.co.uk/DPS/dpslaunch.aspx');
			  //$(location).attr('href','https://jigsaw.yourcarloan.co.uk/DPS/dpslaunch.aspx');
			}
   
    });
	
	
	
	var font = (function () {
    var test_string = 'mmmmmmmmmwwwwwww';
    var test_font = '"Comic Sans MS"';
    var notInstalledWidth = 0;
    var testbed = null;
    var guid = 0;
    
    return {
        // must be called when the dom is ready
        setup : function () {
            if ($('#fontInstalledTest').length) return;

            $('head').append('<' + 'style> #fontInstalledTest, #fontTestBed { position: absolute; left: -9999px; top: 0; visibility: hidden; } #fontInstalledTest { font-size: 50px!important; font-family: ' + test_font + ';}</' + 'style>');
            
            
            $('body').append('<div id="fontTestBed"></div>').append('<span id="fontInstalledTest" class="fonttest">' + test_string + '</span>');
            testbed = $('#fontTestBed');
            notInstalledWidth = $('#fontInstalledTest').width();
        },
        
        isInstalled : function(font) {
            guid++;
        
            var style = '<' + 'style id="fonttestStyle"> #fonttest' + guid + ' { font-size: 50px!important; font-family: ' + font + ', ' + test_font + '; } <' + '/style>';
            
            $('head').find('#fonttestStyle').remove().end().append(style);
            testbed.empty().append('<span id="fonttest' + guid + '" class="fonttest">' + test_string + '</span>');
                        
            return (testbed.find('span').width() != notInstalledWidth);
        }
    };
})();
	
	
	  font.setup();
		if(font.isInstalled('Calibri') == false){
			$('#nav li a').css('fontSize','9px');	
		}
	
	
});// document ready


