﻿var deviceArray = [];
var currentSelectedIndex = 0;
var firstTimeLoading = true;
var nextSelectedIndex = -1;
var featuresArray = null;
var currentFeatureTextIndex = -1;
var deviceFeatureSlider = null;
var showFeatureSlideContainerTimer = null;
var currentSelectedDeviceImg = null;
var showTopFeatures = true;

var chidPosMap = [0, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];

function afterInFocus() {
//    if(nextSelectedIndex > -1)
//        currentSelectedIndex = nextSelectedIndex;
}

function beforeInFocus(delta) {

    SetCurrentSliderImageToDevice();

    var objPosAdj = delta / 27.6;    
    var newObjPos = parseInt(objPosAdj.toFixed(0));
    currentSelectedIndex = parseInt(currentSelectedIndex) + newObjPos;
    
    if (currentSelectedIndex >= deviceArray.length)
        currentSelectedIndex = currentSelectedIndex - deviceArray.length;
    if (currentSelectedIndex < 0)
        currentSelectedIndex = deviceArray.length + currentSelectedIndex;

    ChangeManufactureAndModel(deviceArray[currentSelectedIndex]);
    if (showFeatureSlideContainerTimer)
        clearInterval(showFeatureSlideContainerTimer);

    showFeatureSlideContainerTimer = setInterval("showFeatureSlideContainer()", 2000);
}

function moveNext() {

    SetCurrentSliderImageToDevice();
   
    currentSelectedIndex = currentSelectedIndex + 1;
    if (currentSelectedIndex >= deviceArray.length)
        currentSelectedIndex = 0;
    ChangeManufactureAndModel(deviceArray[currentSelectedIndex]);
    
    if (showFeatureSlideContainerTimer)
        clearInterval(showFeatureSlideContainerTimer);

    showFeatureSlideContainerTimer = setInterval("showFeatureSlideContainer()", 2000);   
    
}

function showFeatureSlideContainer() {
    var currentDevice = deviceArray[currentSelectedIndex];
    
    if (currentDevice.isFeatureLoaded == true) {        
        deviceFeatureSlider = new inter_slide(currentDevice.featuresSlide, document.getElementById('show3'));
        document.getElementById('show3').style.display = '';
    }
    if (showFeatureSlideContainerTimer)
        clearInterval(showFeatureSlideContainerTimer);
}

function SetCurrentSliderImageToDevice() {
    if (deviceFeatureSlider) {
        if (deviceFeatureSlider.playing)
            deviceFeatureSlider.gostop(' Stop ');
        if (currentSelectedDeviceImg != null && currentSelectedIndex > -1 && deviceArray[currentSelectedIndex].featuresSlide != null
            && currentFeatureTextIndex > -1) {
            currentSelectedDeviceImg.src = deviceArray[currentSelectedIndex].featuresSlide[currentFeatureTextIndex][0];
            deviceArray[currentSelectedIndex].image = deviceArray[currentSelectedIndex].featuresSlide[currentFeatureTextIndex][0];
        }
    }
    currentFeatureTextIndex = -1;
    document.getElementById('show3').style.display = 'none';
}


function GetFeatureSlideStartIndex() {
    var ret = 0;
    if (currentSelectedIndex > -1) {
        for (var i = 0; i < deviceArray[currentSelectedIndex].featuresSlide.length; i++) {
            if (deviceArray[currentSelectedIndex].image == deviceArray[currentSelectedIndex].featuresSlide[i][0]) {
                ret = i;
                break;
            }
        }
    }
    return ret;
}

function movePrev() {

    SetCurrentSliderImageToDevice();

    currentSelectedIndex = currentSelectedIndex - 1;    
    if (currentSelectedIndex < 0)
        currentSelectedIndex = deviceArray.length - 1;
    ChangeManufactureAndModel(deviceArray[currentSelectedIndex]);
    
    if (showFeatureSlideContainerTimer)
        clearInterval(showFeatureSlideContainerTimer);
    showFeatureSlideContainerTimer = setInterval("showFeatureSlideContainer()", 2000);    

}

function getImageForObjPos(objPos) {    
    var ret = "";
    var index = -1;
    
    switch (parseInt(objPos)) {
        case 0:
            index = currentSelectedIndex;
            break;
        case 1:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 2:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 3:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 4:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 5:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 6:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;

        case 12:            
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 11:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 10:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
             break;
        case 9:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 8:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 7:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            
            break;
    }
    if (index > -1) {        
        ret = deviceArray[index].image;
    }     
    return ret;
}

function getDevicForObjPos(objPos) {
    var index = -1;

    switch (parseInt(objPos)) {
        case 0:
            index = currentSelectedIndex;
            break;
        case 1:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 2:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 3:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 4:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 5:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;
        case 6:
            index = currentSelectedIndex - objPos;
            if (index < 0)
                index = deviceArray.length + index;
            break;

        case 12:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 11:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 10:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 9:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 8:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;
            break;
        case 7:
            index = currentSelectedIndex + (13 - objPos);
            if (index >= deviceArray.length)
                index = index - deviceArray.length;

            break;
    }
    if (index > -1)
        return deviceArray[index];
    else
        return null;
}


function Device(deviceId, image, manufactureId, deviceName) {
    this.deviceId = deviceId;
    this.image = image;
    this.manufactureId = manufactureId;
    this.deviceName = deviceName;
    this.isFeatureLoaded = false;
    this.featuresSlide = [];
    this.featuresSlide.no_descriptions = 1; //use for no descriptions displayed
    this.featuresSlide.pause = 0; //use for pause onmouseover
    this.featuresSlide.no_controls = 1; //use images for controls
    this.featuresSlide.specs = 'width=310, height=385' //global specifications for this show's new window(s)
    this.featuresSlide.random = 0; //set a random slide sequence on each page load
    this.featuresSlide.manual_start = 0; //start show in manual mode (stopped)
    this.featuresSlide.delay = 5000;
    this.featuresSlide.fadecolor = 'Transparent';

//    this.featuresSlide[0] = ["gps_images/gallery/iPhone3G.png", "test"];
//    this.featuresSlide[1] = ["gps_images/gallery/iPhone3Gs.png", "test1"];
//    this.featuresSlide[2] = ["gps_images/gallery/iPhone4.png", "test2"];

}

function DeviceFeatures() {
    this.featureId;
    this.featureImage;
    this.feature;
}

/* CreateSlides : Method to create an array of slides*/

function CreateSlides() {
    
    deviceArray = new Array();
    GolfLogix.Portal.SmartphoneService.GetAllSmartphones(OnSuccessSmartphone, OnFailedSmartphone);            
}

function OnSuccessSmartphone(result, userContext, methodName) {    
    var devicelist = eval(result);
    for (var i = 0; i < devicelist.length; i++) {        
        var sliderImage = "slider/gallery/" + devicelist[i].SliderImage;
        deviceArray.push(new Device(devicelist[i].PhoneId, sliderImage, devicelist[i].ManufacturerId, devicelist[i].Title));        
    }    
}

function OnFailedSmartphone() {
    //alert('failed');
}

/* CreateSlides ends */

function OnChangeDevice() {    
    getDevices("OnChangeDevice");
}

function getIndexForDeviceID(deviceID) {
    var ret = -1;
    for (i = 0; i < deviceArray.length; i++) {
        var deviceObj = deviceArray[i];
        if (deviceObj.deviceId == deviceID) {
            ret = i;
            break;
        }
    }
    return ret;
}

function OnChangeModel() {        
    var selectedDeviceValue = _phoneDropDown.value;
    var selectedDeviceIndex = getIndexForDeviceID(selectedDeviceValue);

    if (selectedDeviceIndex > -1 && currentSelectedIndex != selectedDeviceIndex) {
        var sliderRotateDegree = 27.69;
        
        SetCurrentSliderImageToDevice()
        
        var indexDeviation = selectedDeviceIndex - parseInt(currentSelectedIndex);
        currentSelectedIndex = selectedDeviceIndex;
       
        if (indexDeviation >= -3 && indexDeviation <= 3) {
            deviceSlider.roundabout_animateAngleToFocusWithCallBack(sliderRotateDegree * indexDeviation);
        }
        else {
            firstTimeLoading = true;
            deviceSlider.roundabout_animateAngleToFocusWithCallBack(sliderRotateDegree * 4);
            firstTimeLoading = false;
        }
        if (showFeatureSlideContainerTimer)
            clearInterval(showFeatureSlideContainerTimer);
        showFeatureSlideContainerTimer = setInterval("showFeatureSlideContainer()", 2000); 
    }
    currentFeatureTextIndex = -1;
    GetFeaturesById();
}


function ChangeManufactureAndModel(currentDevice) {
    //alert("mid:" + currentDevice.manufactureId + " dId: " + currentDevice.deviceId);
    if (_manufaturerDropDown.value != currentDevice.manufactureId) {
        _manufaturerDropDown.value = currentDevice.manufactureId;
        getDevices(currentDevice.deviceId);
    }
    else {
        if (_phoneDropDown.value != currentDevice.deviceId)
            getDevices(currentDevice.deviceId);
    }
}


/*GetDevices : Get all devices by manufacturer Id*/

function getDevices(userContext) {
    if (deviceArray != null && deviceArray.length > 0) {
        _phoneDropDown.options.length = 0;        
        for (var j = 0; j < deviceArray.length; j++) {
            opt = document.createElement("option");
            if (deviceArray[j].manufactureId == _manufaturerDropDown.value) {                
                opt.text = deviceArray[j].deviceName;
                opt.value = deviceArray[j].deviceId;
                _phoneDropDown.options.add(opt);
            }
        }
    }
    
    if (userContext == null) {
        _phoneDropDown.value = deviceArray[0].deviceId;
        GetFeaturesById();
        if (showFeatureSlideContainerTimer)
            clearInterval(showFeatureSlideContainerTimer);
        showFeatureSlideContainerTimer = setInterval("showFeatureSlideContainer()", 2000); 
    }
    else {
        if (userContext == "OnChangeDevice") {           
            OnChangeModel();
        }
        else {
            _phoneDropDown.value = userContext;
            GetFeaturesById();
        }
    }    
    //GolfLogix.Portal.SmartphoneService.GetDevicesByManufacturers(_manufaturerDropDown.value, 0, OnSuccessDevice, OnFailedDevice, userContext);    
}

function OnSuccessDevice(result, userContext, methodName) {    
    _phoneDropDown.options.length = 0;
    var devicelist = eval(result);    
    for (var j = 0; j < devicelist.length; j++) {
        opt = document.createElement("option");
        if (_phoneDropDown != null) {
            opt.text = devicelist[j].PhoneName;
            opt.value = devicelist[j].PhoneId;
            _phoneDropDown.options.add(opt);
        }
    }    
        
    if (userContext == "OnChangeDevice") {
        OnChangeModel();
        _phoneDropDown.value = devicelist[0].PhoneId;
    }
    else
        _phoneDropDown.value = userContext;

    if (userContext == null)
        _phoneDropDown.value = devicelist[0].PhoneId;
              
    GetFeaturesById(_phoneDropDown.value);
}

function OnFailedDevice() {
    //alert('failed');
}

/* getDevices ends */

/*GetFeaturesById : Get all features by model Id*/

function GetFeaturesById() {   
    if (currentSelectedIndex < 0)
        return;
    var currentDevice = deviceArray[currentSelectedIndex];
    if (currentDevice.isFeatureLoaded == false) {
        var phoneId = currentDevice.deviceId;
        //GolfLogix.Portal.SmartphoneService.GetTopFeaturesById(phoneId, OnSuccessFeature, OnFailedFeature, currentDevice);
        showTopFeatures = true;
        GolfLogix.Portal.SmartphoneService.GetAllSmartphoneFeaturesById(phoneId, OnSuccessFeature, OnFailedFeature, currentDevice);        
        
        //_phoneDropDown.value = deviceArray[currentSelectedIndex].deviceId;
    }
    else {
        _featureText.value = currentDevice.featuresSlide[0][1];
       
        //deviceFeatureSlider = new inter_slide(currentDevice.featuresSlide, document.getElementById('show3'));
    } 
}

function OnSuccessFeature(result, userContext, methodName) {
    var currentDevice = userContext;
    var lst = eval('(' + result + ')');
    var i = 0;
    if (lst.length > 0) {
        for (index = 0; index < lst.length; index++) {
            if (lst[index].img != "") {
                var name = lst[index].desc;
                var img = "gps_images/Smartphone_Features/" + lst[index].img;
                currentDevice.featuresSlide[i] = [img, name, "javascript:ShowDownload();"];
                i++;
            }
                       
//            if (keyValues[0].indexOf('\"') == -1) {
//                img = "gps_images/gallery/" + keyValues[1].split('\"')[0];
//                name = keyValues[0].split('\"')[0];
//                //featuresArray.push(name);                
//                currentDevice.featuresSlide[index] = [img, name];
//            }
//            else {
//                img = "gps_images/gallery/" + keyValues[1].split('\"')[0];
//                name = keyValues[0].split('\"')[0];
//                alert(name);  
//                //featuresArray.push(name);
//                currentDevice.featuresSlide[index] = [img, name];
//            }
        }

        currentDevice.isFeatureLoaded = true;

        //deviceFeatureSlider = new inter_slide(currentDevice.featuresSlide, document.getElementById('show3'));
        _featureText.value = currentDevice.featuresSlide[0][1];
        
    }   
      
}

function OnFailedFeature() {
    //alert('failed');
}

/*GetFeaturesById ends*/

/* change Feature Text - Previous and Next Buttons click */
function showPrevFeature() {
    var currentDevice = deviceArray[currentSelectedIndex];    
    showTopFeatures = false;
    if (currentFeatureTextIndex == 0) {
    
//        _featureText.value = currentDevice.featuresSlide[currentDevice.featuresSlide.length - 1][1];
//        currentFeatureTextIndex = currentDevice.featuresSlide.length - 1;

        if (deviceSlider.data('roundabout').animating === 0) {
            moveNext();
            deviceSlider.roundabout_animateToNextChild();
        }
        return;
    }
    else {
        _featureText.value = currentDevice.featuresSlide[currentFeatureTextIndex - 1][1];
        currentFeatureTextIndex--;
    }
    if (deviceFeatureSlider != null)    
        deviceFeatureSlider.changeimg(false, 'buttonClick');
}

function showNextFeature(obj) {
    var currentDevice = deviceArray[currentSelectedIndex];

    if (currentFeatureTextIndex == currentDevice.featuresSlide.length - 1 || (showTopFeatures && currentFeatureTextIndex == 3)) {

//            _featureText.value = currentDevice.featuresSlide[0][1];
//            currentFeatureTextIndex = 0;

        if (deviceSlider.data('roundabout').animating === 0) {
            movePrev();
            deviceSlider.roundabout_animateToPreviousChild();
     
        }
        return;

    }
    else {
        _featureText.value = currentDevice.featuresSlide[currentFeatureTextIndex + 1][1];
        currentFeatureTextIndex++;
    }

    if (deviceFeatureSlider != null && obj != 'rotate') {       
        showTopFeatures = false;
        deviceFeatureSlider.changeimg(true, 'buttonClick');
    }
}
