//------------------------------------------------------------------------------
//   AJAX functions for manufacturers select
//------------------------------------------------------------------------------

function getManufacturerSelect()
{
    var parent_id=$F('device');
    var url ='index.php';
    var pars ='page=ajaxresponse/get_child_categories_json&categoryId='+parent_id+'&rid='+Math.random();

    var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		parameters: pars,
		onLoading: showWait4Manufacturer,
		onComplete: showManufacturerResponse
	});
}
//------------------------------------------------------------------------------
function showWait4Manufacturer()
{
    $('manufacturer').options.length=0;
    $('manufacturer').options[0] = new Option('Please wait...',0);
}
//------------------------------------------------------------------------------
function showManufacturerResponse(originalRequest)
{
/*
    data = originalRequest.responseText.evalJSON();
    
    window.PATH[0] = data.sef_name;
    window.PATH_NAME[0] = data.name;
    var ilgis = window.PATH.length;
    for(var i = 1; i < ilgis; i++){
        window.PATH.pop();
        window.PATH_NAME.pop();
    }
    $('manufacturer').options[0] = new Option(' - Models - ',0);
    //$('models').options[0] = new Option(' - Models - ',0);
    var count=1;
    $('manufacturer').options.length=count;
    //$('models').options.length=count;
    for (var i = 0; i < data.childs.length; i++) {
	    $('manufacturer').options[count] = new Option(data.childs[i].name,data.childs[i].id);
	    count+=1;
	    $('manufacturer').options.length=count;
    }

    var box = $('column-center').getElementsByClassName('box');
    box[0].remove();
    createLetterArray(data.childs, data);
*/
    data = originalRequest.responseText.evalJSON();
    window.PATH[0] = data.sef_name;
    
    window.PATH_NAME[0] = data.name;
    var ilgis = window.PATH.length;
    for(var i = 1; i < ilgis; i++){
        window.PATH.pop();
        window.PATH_NAME.pop();
    }
    // generate link path;
    var linkPath = window.WEB_ROOT+'/';
    for (var i = 0; i < window.PATH.length; i++) {
        linkPath += window.PATH[i]+'/';
    }// end generate link path;
    //alert(linkPath);
    $('manufacturer').options[0] = new Option(' - Models - ',0);
    $($('manufacturer').options[0]).addClassName('bold');
    var count=1;
    $('manufacturer').options.length=count;
    var hasParts = false;
    for (var i = 0; i < data.childs.length; i++) {
        if(data.childs[i].name.substring(0,1)!='.'){
	       $('manufacturer').options[count] = new Option(data.childs[i].name,linkPath+data.childs[i].sef_name+'/');
	       count+=1;
	       $('manufacturer').options.length=count;
	    } else {
            hasParts = true;
        }
    }
    if(hasParts==true){
        $('manufacturer').options[count] = new Option(' - Parts - ',0);
        $($('manufacturer').options[count]).addClassName('bold');
        count+=1;
        $('manufacturer').options.length=count;
    }
    
    for (var i = 0; i < data.childs.length; i++) {
        if(data.childs[i].name.substring(0,1)=='.'){
	    $('manufacturer').options[count] = new Option(data.childs[i].name.substring(2),linkPath+data.childs[i].sef_name+'/');
	    count+=1;
	    $('manufacturer').options.length=count;
	    }
    }
    var box = $('column-center').getElementsByClassName('box');
    box[0].remove();
    createLetterArray(data.childs, data);
}

//------------------------------------------------------------------------------
//   AJAX functions for models select
//------------------------------------------------------------------------------
function getModelSelect()
{
    var parent_id=$F('manufacturer');
    var url ='index.php';
    var pars ='page=ajaxresponse/get_child_categories_json&categoryId='+parent_id+'&rid='+Math.random();

    var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		parameters: pars,
		onLoading: showWait4Model,
		onComplete: showModelResponse
	});
}
//------------------------------------------------------------------------------
function showWait4Model()
{
    $('models').options.length=0;
    $('models').options[0] = new Option('Please wait...',0);
}
//------------------------------------------------------------------------------
function showModelResponse(originalRequest)
{
    data = originalRequest.responseText.evalJSON();
    window.PATH[1] = data.sef_name;
    
    window.PATH_NAME[1] = data.name;
    var ilgis = window.PATH.length;
    for(var i = 2; i < ilgis; i++){
        window.PATH.pop();
        window.PATH_NAME.pop();
    }
    // generate link path;
    var linkPath = window.WEB_ROOT+'/';
    for (var i = 0; i < window.PATH.length; i++) {
        linkPath += window.PATH[i]+'/';
    }// end generate link path;

    $('models').options[0] = new Option(' - Models - ',0);
    $($('models').options[0]).addClassName('bold');
    var count=1;
    $('models').options.length=count;
    var hasParts = false;
    for (var i = 0; i < data.childs.length; i++) {
        if(data.childs[i].name.substring(0,1)!='.'){
	       $('models').options[count] = new Option(data.childs[i].name,linkPath+data.childs[i].sef_name+'/');
	       count+=1;
	       $('models').options.length=count;
	    } else {
            hasParts = true;
        }
    }
    if(hasParts==true){
        $('models').options[count] = new Option(' - Parts - ',0);
        $($('models').options[count]).addClassName('bold');
        count+=1;
        $('models').options.length=count;
    }
    
    for (var i = 0; i < data.childs.length; i++) {
        if(data.childs[i].name.substring(0,1)=='.'){
	    $('models').options[count] = new Option(data.childs[i].name.substring(2),linkPath+data.childs[i].sef_name+'/');
	    count+=1;
	    $('models').options.length=count;
	    }
    }
    var box = $('column-center').getElementsByClassName('box');
    box[0].remove();
    createLetterArray(data.childs, data);
}
//------------------------------------------------------------------------------
// Create array by letter
//------------------------------------------------------------------------------
function createLetterArray(arr, cData)
{
    var count = 0;
    var letterArray = new Array;
    var letter2Object = new Array(arr.length);
    for (var i = 0; i < arr.length; i++) {
        if(letterArray.indexOf(arr[i].name.capitalize().substring(0,1))<0)
        {
            var index = letterArray.push(arr[i].name.capitalize().substring(0,1));
        }
    }
    letterArray.sort();
    for (var i = 0; i < arr.length; i++) {
        letter2Object[i] = letterArray.indexOf(arr[i].name.capitalize().substring(0,1));
    }
    //alert(letter2Object.inspect());
    //alert(letterArray.inspect());
    // part numbers...
    if(letterArray[0]=='.') {
        //alert(letter2Object.length);
        var partNumbers = new Array;
        for (var j = 0; j < letter2Object.length; j++) {
            if(letter2Object[j]==0){
                var index = partNumbers.push(arr[j]);
            }
        }
        var pletterArray = new Array;
        var pletter2Object = new Array(partNumbers.length);
        //alert(partNumbers[0].name.capitalize().substring(2,3));
        for (var i = 0; i < partNumbers.length; i++) {
            if(pletterArray.indexOf(partNumbers[i].name.capitalize().substring(2,3))<0)
            {
                var index = pletterArray.push(partNumbers[i].name.capitalize().substring(2,3));
                //alert(partNumbers[i].name.capitalize().substring(2,4));
            }
        }
        pletterArray.sort();
        for (var i = 0; i < partNumbers.length; i++) {
            pletter2Object[i] = pletterArray.indexOf(partNumbers[i].name.capitalize().substring(2,3));
        }
        //alert(pletter2Object.inspect());
        //alert(pletterArray.inspect());
    }
    
    
    // generate link path;
    var linkPath = window.WEB_ROOT+'/';
    for (var i = 0; i < window.PATH.length; i++) {
        linkPath += window.PATH[i]+'/';
    }// end generate link path;
    // new box div
    var boxDiv = new Element('div', { 'class': 'box', 'id': 'products' });
    boxDiv.appendChild(generatePath());
    boxDiv.appendChild(generateTitle(cData));
    if(letterArray[0]=='.') {
        var pModels = new Element('span', {'class':'bl'});
        pModels.appendChild(Element('img', {src: window.WEB_ROOT+'/templates/layouts/layout4/styles/default/images/search_ico.gif', 'alt':''}));
        pModels.appendChild(Element('a', {href: window.WEB_ROOT+'/'+window.CURRENT_PATH+'#mo'}).update('Search by model'));
        boxDiv.appendChild(pModels);
    
        var pParts = new Element('span',{'class':'bl'});
        pParts.appendChild(Element('img', {src: window.WEB_ROOT+'/templates/layouts/layout4/styles/default/images/search_ico.gif', 'alt':''}));
        pParts.appendChild(Element('a', {href: window.WEB_ROOT+'/'+window.CURRENT_PATH+'#pn'}).update('Search by part number'));
        boxDiv.appendChild(pParts);
    
        var h1 = new Element('h1');
        var span = new Element('span');
        span.appendChild(Element('a', {'name':'mo'}).update('Models'));
        h1.appendChild(span);
        boxDiv.appendChild(h1);
    }
    // new letter div
    var letterDiv = new Element('div', { 'class': 'letters' });
    for (var i = 0; i < letterArray.length; i++) {
        if(letterArray[i]!='.'){
            letterDiv.appendChild(Element('a', { href: window.WEB_ROOT+'/'+window.CURRENT_PATH+'#'+letterArray[i] }).update(letterArray[i]));
        }
    }
    
    boxDiv.appendChild(letterDiv);
    // new alfabet div
    var alfabetDiv = new Element('div', { 'id': 'alfabet' });
    // new ul element
    var ul = new Element('ul');
    var three=0;
    for (var i = 0; i < letterArray.length; i++) {
        if(three==3){
            //add fix li to ul after 3 letters
            ul.appendChild(Element('li', {'class':'clear'}).update('&nbsp;'));
            three=0;
        }
        if(letterArray[i]!='.') {
            // create li element
            var li = new Element('li');
            // add span to li element
            var span = new Element('span');
            span.appendChild(Element('a',{'name':letterArray[i]}).update(letterArray[i]));
            li.appendChild(span);
            three++;
            // will add all categories begining with current letter
            // create category ul
            var cul = new Element('ul');
            for (var j = 0; j < letter2Object.length; j++) {
                if(letter2Object[j]==i){
                    //add li element to category ul
                    var cli = new Element('li');
                    cli.appendChild(Element('a',{href: linkPath+arr[j].sef_name+'/'}).update(arr[j].name));
                    cul.appendChild(cli);
                }
            }
            // add category ul to letter li element;
            li.appendChild(cul);
            // add li element to ul
            ul.appendChild(li);
        }
    }
    // add ul to alfabet
    alfabetDiv.appendChild(ul);
    // add alfabet to box
    boxDiv.appendChild(alfabetDiv);
    // create bottom letter div
    var letterDiv = new Element('div', { 'class': 'letters ptop' });
    var letterSpan = new Element('span', {'class': 'left'});
    for (var i = 0; i < letterArray.length; i++) {
        if(letterArray[i]!='.'){
            letterSpan.appendChild(Element('a', { href: window.WEB_ROOT+'/'+window.CURRENT_PATH+'#'+letterArray[i] }).update(letterArray[i]));
        }
    }
    letterDiv.appendChild(letterSpan);
    boxDiv.appendChild(letterDiv);
    
    //parts
    if(letterArray[0]=='.') {
        var h1 = new Element('h1');
        var span = new Element('span');
        span.appendChild(Element('a', {'name':'pn'}).update('Part Numbers'));
        h1.appendChild(span);
        boxDiv.appendChild(h1);
        // new letter div
        var pletterDiv = new Element('div', { 'class': 'letters' });
        for (var i = 0; i < pletterArray.length; i++) {
            if(pletterArray[i]!='.'){
                pletterDiv.appendChild(Element('a', { href: window.WEB_ROOT+'/'+window.CURRENT_PATH+'#p'+pletterArray[i] }).update(pletterArray[i]));
            }
        }
        
        boxDiv.appendChild(pletterDiv);
            // new alfabet div
        var alfabetDiv = new Element('div', { 'id': 'alfabet' });
        // new ul element
        var ul = new Element('ul');
        var three=0;
        for (var i = 0; i < pletterArray.length; i++) {
            if(three==3){
                //add fix li to ul after 3 letters
                ul.appendChild(Element('li', {'class':'clear'}).update('&nbsp;'));
                three=0;
            }
            // create li element
            var li = new Element('li');
            // add span to li element
            var span = new Element('span');
            span.appendChild(Element('a',{'name':'p'+pletterArray[i]}).update(pletterArray[i]));
            li.appendChild(span);
            three++;
            // will add all categories begining with current letter
            // create category ul
            var cul = new Element('ul');
            for (var j = 0; j < pletter2Object.length; j++) {
                if(pletter2Object[j]==i){
                    //add li element to category ul
                    var cli = new Element('li');
                    cli.appendChild(Element('a',{href: linkPath+partNumbers[j].sef_name+'/'}).update(partNumbers[j].name.substring(2)));
                    cul.appendChild(cli);
                }
            }
            // add category ul to letter li element;
            li.appendChild(cul);
            // add li element to ul
            ul.appendChild(li);
        }
        // add ul to alfabet
        alfabetDiv.appendChild(ul);
        // add alfabet to box
        boxDiv.appendChild(alfabetDiv);
    // create bottom letter div
        var letterDiv = new Element('div', { 'class': 'letters ptop' });
        var letterSpan = new Element('span', {'class': 'left'});
        for (var i = 0; i < pletterArray.length; i++) {
            if(pletterArray[i]!='.'){
                letterSpan.appendChild(Element('a', { href: window.WEB_ROOT+'/'+window.CURRENT_PATH+'#p'+pletterArray[i] }).update(pletterArray[i]));
            }
        }
        letterDiv.appendChild(letterSpan);
        boxDiv.appendChild(letterDiv);
    }
    $('column-center').appendChild(boxDiv);
}
//------------------------------------------------------------------------------
function generatePath()
{
    var p = new Element('p', {'id':'path'});
    p.appendChild(Element('a',{href:window.WEB_ROOT+'/'}).update('<strong>www.simplemicro.com</strong>'));
    var linkPath = window.WEB_ROOT+'/';
    for (var i = 0; i < window.PATH.length; i++) {
        p.innerHTML += " / ";
        linkPath += window.PATH[i]+'/';
        var a = new Element('a', {href:linkPath}).update(window.PATH_NAME[i]);
        p.appendChild(a);
    }
    return p;
}
//------------------------------------------------------------------------------
function generateTitle(cData)
{
    var titleDiv = new Element('div', {'class':'title'});
    
    var h1 = new Element('h1');
    h1.appendChild(Element('span').update(cData.name));
    titleDiv.appendChild(h1);
    
    var descriptionDiv = new Element('div', {'class':'description'}).update(cData.description);
    titleDiv.appendChild(descriptionDiv);
    
    return titleDiv;
}

