$(document).ready(function () {


    ////////////////////////////////////////
    //Code for product_details.php

    //$.cookie("PhoneCompareIds") = "1-2-3";


    // Add to compare button
    $('a.compare').click(function () {
        var id = phoneId;
        var name = phoneName;
        var image = "<img src='" + phoneImage + "' alt='" + phoneName + "' />";
        insertphone(id, name, image);
    });

    $('a.tablet-compare').click(function () {
        var id = phoneId;
        var name = phoneName;
        var image = "<img src='" + phoneImage + "' alt='" + phoneName + "' />";
        inserttablet(id, name, image);
    });

    // Clear list
    $('.clear-list-button a').click(function () {
        clear_phones();
    });

    // Clear list of tablets
    $('.tablet-clear-list-button a').click(function () {
        clear_tablets();
    });

    // Get phones from cookies and insert compare phones on page load
    if (location.href.toLowerCase().indexOf('/products/compare') == -1)
        insertphones_onload(phoneId);

    // Get phones from cookies and insert compare tablets on page load
    if (location.href.toLowerCase().indexOf('/products/compare') == -1 && location.href.toLowerCase().indexOf('tablets') != -1)
        inserttablets_onload(phoneId);
});

// Function to create the compare link
function linkActive() {
    $('.compare-button a').css({ "color": "#A59D95", "cursor": "pointer" });
    $('.compare-button a').attr("href", "compare/" + $.cookie('PhoneCompareIds'));
}

function linkDisabled() {
    $('.compare-button a').css({ "color": "#ccc", "cursor": "default" });
    $('.compare-button a').attr("href", "Javascript:void(0);");
}

function linkAddDisabled() {
    $('a.compare').css({ "color": "#ccc", "cursor": "default" });
}
function linkAddActive() {
    $('a.compare').css({ "color": "#69B40F", "cursor": "pointer" });
}

// Function to create the compare link - FOR TABLETS -->
function tabletLinkActive() {
    $('.tablet-compare-button a').css({ "color": "#A59D95", "cursor": "pointer" });
    $('.tablet-compare-button a').attr("href", currentRegion + "products/compare/" + $.cookie('TabletCompareIds'));
}

function tabletLinkDisabled() {
    $('.tablet-compare-button a').css({ "color": "#ccc", "cursor": "default" });
    $('.tablet-compare-button a').attr("href", "Javascript:void(0);");
}

function tabletLinkAddDisabled() {
    $('a.tablet-compare').css({ "color": "#ccc", "cursor": "default" });
}
function tabletLinkAddActive() {
    $('a.tablet-compare').css({ "color": "#69B40F", "cursor": "pointer" });
}

//Insert Phone and Image to compare
function insertphone(id, name, image) {

    //var currentValue = $.cookie('PhoneCompareIds') == null ? '' : $.cookie('PhoneCompareIds');

    var ids = currentPhoneIds.split('-');

    if ((ids.indexOf(id) == -1 || ids.indexOf(id) == undefined) && currentPhoneCount < 3) {
        //set our internal tracking value
        currentPhoneIds = currentPhoneIds + (currentPhoneIds.length > 0 ? '-' : '') + id;
        //set the new cookie value
        $.cookie('PhoneCompareIds', currentPhoneIds, { path: currentRegion });

        currentPhoneCount++;
        $('.compare-' + currentPhoneCount).html(image);
        $('.compare-' + currentPhoneCount + ' img').css("display", "none");
        $('.compare-' + currentPhoneCount).append('<span></span>');
        $('.compare-' + currentPhoneCount + ' span').html(name);
        $('.compare-' + currentPhoneCount + ' img').fadeIn();

        switch (currentPhoneCount) {
            case 1:
                linkDisabled(); //cant compare
                linkAddDisabled(); //can add
                break;
            case 2:
                linkActive(); //can compare
                linkAddDisabled(); //can add
                break;
            case 3:
                linkActive(); //can compare
                linkAddDisabled(); //cant add
                break;
        }
    }
}

//Insert Phone and Image to compare
function inserttablet(id, name, image) {

    //var currentValue = $.cookie('TabletCompareIds') == null ? '' : $.cookie('TabletCompareIds');
  
    var ids = currentPhoneIds.split('-');

    if ((ids.indexOf(id) == -1 || ids.indexOf(id) == undefined) && currentPhoneCount < 3) {
        //set our internal tracking value
        currentPhoneIds = currentPhoneIds + (currentPhoneIds.length > 0 ? '-' : '') + id;
        //set the new cookie value
        $.cookie('TabletCompareIds', currentPhoneIds, { path: currentRegion });

        currentPhoneCount++;
        $('.compare-' + currentPhoneCount).html(image);
        $('.compare-' + currentPhoneCount + ' img').css("display", "none");
        $('.compare-' + currentPhoneCount).append('<span></span>');
        $('.compare-' + currentPhoneCount + ' span').html(name);
        $('.compare-' + currentPhoneCount + ' img').fadeIn();

        switch (currentPhoneCount) {
            case 1:
                tabletLinkDisabled(); //cant compare
                tabletLinkAddDisabled(); //can add
                break;
            case 2:
                tabletLinkActive(); //can compare
                tabletLinkAddDisabled(); //can add
                break;
            case 3:
                tabletLinkActive(); //can compare
                tabletLinkAddDisabled(); //cant add
                break;
        }
    }
}

//Clear all phones from "compare"
function clear_phones() {
    $.cookie('PhoneCompareIds', null, { path: currentRegion });
    currentPhoneIds = '';
    $('.compare-1').html('');
    $('.compare-2').html('');
    $('.compare-3').html('');
    linkDisabled(); //cant compare
    linkAddActive(); //cant add
    currentPhoneCount = 0;
    //update the one in the menu too
}

//Clear all tablets from "compare"
function clear_tablets() {
    $.cookie('TabletCompareIds', null, { path: currentRegion });
    currentPhoneIds = '';
    $('.compare-1').html('');
    $('.compare-2').html('');
    $('.compare-3').html('');
    tabletLinkDisabled(); //cant compare
    tabletLinkAddActive(); //cant add
    currentPhoneCount = 0;
    //update the one in the menu too
}


// Fill spots with JSON info on page load
var currentPhoneCount = 0;
var currentPhoneIds = '';
function insertphones_onload(id) {
    currentPhoneCount = currentComparePhones.items.length;
    currentPhoneIds = $.cookie('PhoneCompareIds') == null ? '' : $.cookie('PhoneCompareIds');
    //if we are on the compare page, then we load the cookies with the id's from the url in the code of the page
    //if (location.href.toLowerCase().indexOf('/products/compare') == -1) {
    for (var i = 0; i < currentComparePhones.items.length; i++) {
        //alert(currentComparePhones.items[i].imageurl);
        $('.compare-' + (i + 1)).html('<img src=\'' + currentComparePhones.items[i].imageurl + '\'/>');
        $('.compare-' + (i + 1)).append('<span></span>');
        $('.compare-' + (i + 1) + ' span').html(currentComparePhones.items[i].name);
    }

    // Check if phoneId is already being compared
    if ($.cookie('PhoneCompareIds') != null) {
        var ids = $.cookie('PhoneCompareIds').split('-');
        if (id == ids[0] || id == ids[1] || id == ids[2] || $('.compare-3').html() != '') {
            linkAddDisabled();
        }
    }
    
    if (currentComparePhones.items.length == 3)
        linkAddDisabled();

    if (currentComparePhones.items.length > 1) {
        linkActive();
    } else {
        linkDisabled();
    }

}


function inserttablets_onload(id) {
    currentPhoneCount = currentComparePhones.items.length;
    currentPhoneIds = $.cookie('TabletCompareIds') == null ? '' : $.cookie('TabletCompareIds');
    //if we are on the compare page, then we load the cookies with the id's from the url in the code of the page
    //if (location.href.toLowerCase().indexOf('/products/compare') == -1) {
    for (var i = 0; i < currentComparePhones.items.length; i++) {
        //alert(currentComparePhones.items[i].imageurl);
        $('.compare-' + (i + 1)).html('<img src=\'' + currentComparePhones.items[i].imageurl + '\'/>');
        $('.compare-' + (i + 1)).append('<span></span>');
        $('.compare-' + (i + 1) + ' span').html(currentComparePhones.items[i].name);
    }

    // Check if phoneId is already being compared
    if ($.cookie('TabletCompareIds') != null) {
        var ids = $.cookie('TabletCompareIds').split('-');
        if (id == ids[0] || id == ids[1] || id == ids[2] || $('.compare-3').html() != '') {
            linkAddDisabled();
        }
    }
    
    if (currentComparePhones.items.length == 3)
        tabletLinkAddDisabled();

    if (currentComparePhones.items.length > 1) {
        tabletLinkActive();
    } else {
        tabletLinkDisabled();
    }

}








////////////////////////////////////////
//Code for compare_phones.php

function AddToCompare(ddlId) {
    //get the current id's
    var currentIds = '';
    for (var i = 0; i < currentComparePhones.items.length; i++)
        currentIds += (currentIds.length > 0 ? '-' : '') + currentComparePhones.items[i].id;
    //new phone id to add
    var phoneId = $('#compare-phone-' + ddlId + ' option:selected').val();
    if (phoneId.length > 0)
        document.location = currentRegion + 'products/compare/' + currentIds + (currentIds.length > 0 ? '-' : '') + phoneId;
}

////////////////////////////////////////




