var player1;
var __pageForms;

$(document).ready(function() {
    
    // Set proper image and width for Find A Dealer Near You button...
    
    // Safari on Mac
    if(navigator.appVersion.indexOf('Mac') != -1 && navigator.userAgent.indexOf('Safari') != -1) {
        $("#topNavFindADealerNearYou").addClass('topNavFindADealerNearYouMacSafari');
    }
    // Non-Safari on Mac, or IE 9.0 on PC
    else if((navigator.appVersion.indexOf('Mac') != -1 && navigator.userAgent.indexOf('Safari') === -1) || (navigator.appVersion.indexOf('Mac') === -1 && navigator.userAgent.indexOf('MSIE 9.0') != -1)) {
        $("#topNavFindADealerNearYou").addClass('topNavFindADealerNearYouMacNonSafariOrPcIe');
    }
    // Non Mac
    else if(navigator.appVersion.indexOf('Mac') === -1) {
        $("#topNavFindADealerNearYou").addClass('topNavFindADealerNearYouNonMac');
        
    }
    
    $('.slideshow').cycle({
        fx:    'curtainX',
        sync:  false,
        delay: -2000
    });
    
    $('#gallery a').lightBox({
        fixedNavigation:true
    });
    
    $('#gallery_marketing a.gallery_image').lightBox({
        fixedNavigation:true
    });
    
    $("#gallery_list").simplyScroll({
        className: 'vert',
        horizontal: false,
        frameRate: 20,
        speed: 5            
    });
    
    // Testimonials video
    (function() {
        var flashvars = {
            file: '/assets/JanuarySavings_30sec.flv', 
            autostart: 'false', 
            image: "/media/JanSavings_MED.png"
        },
        params = {
            allowfullscreen: 'true', 
            allowscriptaccess: 'always'
        },
        attributes = {
            id: 'player1', 
            name: 'player1'
        },
        ie_params = "";

        $('#lbFB').jqm();
        $('#lbFB').jqmAddClose('.hideDialog'); 

        // IE created a "Fix" for rendering flash >.>
        // http: //siderite.blogspot.com/2007/10/flashvars-empty-after-internet-explorer.html
        if (typeof ($.browser.msie) !== "undefined" && $.browser.msie === true) {
            ie_params = "?" + $.param(flashvars);
        }
        
        swfobject.embedSWF('/player/player.swf' + ie_params, 'videoDiv', 425, 356, '9.0.115', 'false', flashvars, params, attributes);
    })();
    
    // Left hand nav video
    (function(){
    
        var flashvars = {
            file: '/media/60_Second_Install.flv',
            autostart: 'false',
            image: "/media/60SecVideoScreenShot.png"
        },
        params = {
            allowfullscreen: 'true', 
            allowscriptaccess: 'always'
        },
        attributes = {
            id: 'player2', 
            name: 'player2'
        },
        ie_params = "";        

        $('#lbFB2').jqm();
        $('#lbFB2').jqmAddTrigger('#clickTest');
        $('#lbFB2').jqmAddClose('.hideDialog');
        

        // IE created a "Fix" for rendering flash >.>
        // http: //siderite.blogspot.com/2007/10/flashvars-empty-after-internet-explorer.html
        if (typeof ($.browser.msie) !== "undefined" && $.browser.msie === true) {
            ie_params = "?" + $.param(flashvars);
        }
        swfobject.embedSWF('/player/player.swf' + ie_params, 'videoDiv2', 425, 356, '9.0.115', 'false', flashvars, params, attributes);
    })();
    
    checkFormValues(__pageForms);    
});

function checkFormValues(pageForms) {
    if(!$.isArray(pageForms)) {
        pageForms = [pageForms];
    }
    
    $.each(pageForms, function(index, formId){
        if($('#'+formId).length > 0) {
            var cookie = $.cookie('form_'+formId);
            if( cookie != null) {
                var formFieldValues = $.JSON.decode(cookie);
               
                $.each(formFieldValues, function(filedID, fieldValue){
                    var field =  $('#'+filedID);
                    if(field.length > 0) {
						if(field.attr('type') != 'checkbox' && field.attr('type') != 'radio' && field.attr('type') != 'select') {
	                        field.val(fieldValue);
						} else if(field.attr('type') == 'checkbox' || field.attr('type') == 'radio') {
							if(fieldValue == 'true') {
								field.attr('checked', 'checked');
							} else {
								field.removeAttr('checked');
							}
						} else if(field.attr('type') == 'select') {
                            field[0].selectedIndex = fieldValue;
                        }
                    }
                });
            }
        } 
    });
}

function saveFormPost(form) {
    var cookie = {};

	var returnVal = true;

    $.each(form.elements, function(index, elm) {
        elm = $(elm);

        var elmVal = elm.val();
        if(elmVal == "false") {
            alert("Please fill in all required fields");
            returnVal = false;
			return false;
        }

		if(elm.attr('type') != 'checkbox' && elm.attr('type') != 'radio' && elm.attr('type') != 'select') {
	        cookie[elm.attr('id')] = elm.val();
		} else if(elm.attr('type') == 'checkbox' || elm.attr('type') == 'radio') {
			if(elm.is(':checked')) {
				cookie[elm.attr('id')] = 'true';
			} else {
				cookie[elm.attr('id')] = 'false';
			}
		} else if(elm.attr('type') == 'select') {
            cookie[elm.attr('id')] = elm[0].selectedIndex;
        }
    });
    
	if(returnVal) {
	    $.cookie('form_'+String(form.id), String($.JSON.encode(cookie)));
	}
    
    return returnVal;
}

