﻿if (typeof console == "undefined") console = { log: function() {} };

var jQT = new $.jQTouch({ useFastTouch:false });


function DropDown(divElement)
{
    this.divElement = divElement;
    this.selectedDiv = divElement.find('div.veneerSelectedItem');
    this.selectElement = divElement.find('select');
    var divClass = this.divElement.attr('id');
    this.isProvince = (/province/i).test(divClass);
    this.isModel = (/model/i).test(divClass);
    this.isTrim = (/trim/i).test(divClass);
    this.isPayment = (/payment/i).test(divClass);

    var that = this;

    /*
    this.divElement.find('div').click(function() {
        that.selectElement.click();
    });
    */
    this.selectElement.change(function() {
        that.selectedDiv.removeClass('veneerNextItem');
        that.updateVeneer();
    });
    this.resize = function() {
        var veneerWidth = that.selectElement.width();
        that.selectedDiv.width(veneerWidth);
    };    
  
    if ((/CPOPaging/i).test(this.divElement.attr('class')))
    {
        //this.divElement.find('div.optionVeneer').hide();
        //this.selectElement.fadeTo(0,0);
    }
    else
    this.selectElement.fadeTo(0,0);
    if (this.selectElement.find('option:selected').length > 0)
    {
        this.selectedDiv.text(this.selectElement.find('option:selected').text());
    }
    if ((/disabled/i).test(this.divElement.attr('class')))
        this.Disable();
}
DropDown.prototype.Ready = function() {
    if (this.selectElement.find('option').length > 1)
        this.Enable();
};
DropDown.prototype.Enable = function() {
    this.selectElement.removeAttr('disabled');
    this.divElement.removeClass('pricingOptionDisabled');
};
DropDown.prototype.Disable = function() {
    this.selectElement.attr('disabled','disabled');
    this.divElement.addClass('pricingOptionDisabled');
};
DropDown.prototype.updateVeneer = function() {
    var selectedOption = this.selectElement.find('option:selected');
    this.selectedDiv.text(selectedOption.text());
}
DropDown.prototype.val = function() {
    if (this.selectElement != undefined && this.selectElement.length > 0)
        return this.selectElement.val();
};

function SCIMobile() {
    var geocodedLocation = null;
    var that = this;
    
    this.GeolocateUser = function(successCallback, failureCallback) {
        if ($.cookie('scilat').length == undefined || $.cookie('scilng').length == undefined) {
            var located = jQT.updateLocation(function(position) {
                if (position) {
                    $('input.lat').val(position.latitude);
                    $('input.lng').val(position.longitude);
                    $.cookie('scilat', position.latitude, { expires: 30/1440 });
                    $.cookie('scilng', position.longitude, { expires: 30/1440 });
                    geocodedLocation = position;
                    if (successCallback)
                        successCallback(position);
                }
            },
            function(error) {
                if (error)
                {
                    console.log(error.code + ' ' + error.message);
                    $.cookie('scilat', null);
                    $.cookie('scilng', null);
                    if (failureCallback)
                        failureCallback(error);
                }
            });
        }
        else {
            if (successCallback) {
                if (geocodedLocation == null)
                    successCallback({latitude:parseFloat($.cookie('scilat')), longitude:parseFloat($.cookie('scilng'))});
                else
                    successCallback(geocodedLocation);
            }
        }
    }
};

var mobile = null;

function Reorient(forcePortrait) {
    var orientation = window.innerWidth < window.innerHeight ? 'profile' : 'landscape';
    if (window == undefined || window.innerWidth == undefined || window.innerHeight == undefined)
    {
        orientation = document.documentElement.clientWidth < document.documentElement.clientHeight ? 'profile' : 'landscape';
    }
    if (window.orientation != undefined)
        orientation = (Math.abs(window.orientation) != 90) ? 'profile' : 'landscape'; 
    if (forcePortrait == true)
        orientation = 'profile';
    $('body').removeClass('profile landscape').addClass(orientation).trigger('turn', {orientation: orientation});

    if (orientation == 'landscape') {
        var width = $('div.pad').width();
        $('div.CMSContainer').css('width', width);
    }
    else {
        var width = $('div.pad').width();
        $('div.CMSContainer').css('width', width);
    }
    
    var serviceText = $('textarea.serviceText');
    if (serviceText.length > 0)
        serviceText.width(serviceText.parent().width() - 2);
    
    if (navigator.userAgent.match(/IEMobile\/7/i))
    {
        $('div.vehicleTitle div').each(function() {
            if ($(this).parent().attr('class').match(/locatorTitle/i))
                return false;
            
            $(this).css('margin-top', -1 * $(this).height()/2);
        });
    }
}

$(function() {
        mobile = new SCIMobile();
        mobile.GeolocateUser();
        
        if (window.innerWidth >= window.innerHeight) {
            $('body').removeClass('profile landscape').addClass('landscape');
        }
        
        var supportsOrientationChange = "onorientationchange" in window;
        var orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

        $(window).resize(Reorient);
        $(window).resize();
        
        //$(window).bind(orientationEvent, Reorient);
});

