/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


jQuery(document).ready(function() {
    initSearchBtnProjectType();
    initSearchBtnClick();
    initSearchSlider();
    initSearchBtnClear();

    jQuery('.content-search-project-btn').colorTip({
        color:'tooltip-show'
    });


    if ($(".toolbox").length > 0) {
    //alert('toolbox');
    $.ajax({
            url: '/service/home/tool-search-result/?row=4&id=toolbox&qrycat=',
            success: function(data){
                    jQuery(".toolbox").html(data);

                    //initProjectMapListClick();

                    $('input#txtSearch_toolbox').jsonSuggest({url: '/service/ap-projects_search/tool-search-suggest/', maxResults: 80, onSelect: function(item){$('input#txtSearch_toolbox').val('"'+item.text+'"');}});
            }
        });
     }
     if ($(".searchbox").length > 0) {
     //alert('searchbox');
     $.ajax({
            url: '/service/home/tool-search-result/?row=4&id=searchbox&qrycat=',
            success: function(data){
                    jQuery(".searchbox").html(data);

                    //initProjectMapListClick();

                    $('input#txtSearch_searchbox').jsonSuggest({url: '/service/ap-projects_search/tool-search-suggest/', maxResults: 80, onSelect: function(item){$('input#txtSearch_searchbox').val('"'+item.text+'"');}});
            }
        });
     }
     if ($(".searchpage").length > 0) {
     //alert('searchpage');
     $.ajax({
            url: '/service/home/tool-search-result/?row=4&id=searchpage&qrycat=',
            success: function(data){
                    jQuery(".searchpage").html(data);

                    initProjectMapListClick();
                    initProjectMapList();

                    $('input#txtSearch_searchpage').jsonSuggest({url: '/service/ap-projects_search/tool-search-suggest/', maxResults: 80, width: 250, onSelect: function(item){$('input#txtSearch_searchpage').val('"'+item.text+'"');}});
            }
        });
     }
     if ($(".brandpage").length > 0) {
        initProjectMapList();
     }
});


function initSearchBtnProjectType() {
    $('#search-home-left .content-search-project-btn').click(function(){
        $(this).toggleClass('selected');

        search_home_right = $(this).parent().parent().parent().parent().find("#search-home-right");
        id = $(search_home_right).attr('class');
        //alert(id);

        paramcat = '';
        $(this).parent().find('.content-search-project-btn').each(function(index) {

            if($(this).hasClass('selected')) {
                $(this).addClass('search-project-btn-'+index);
                //$cat = $cat+eval(index+1)+',';
                 paramcat = paramcat+eval(index+1)+',';
            } else {
                $(this).removeClass('search-project-btn-'+index);
            }

        });

        paramtxt = $('#txtSearch_'+id).val();
        paramprice = $("#Slider_"+id).slider('value').replace(';', ',') ;

        //alert('Project: '+paramcat+'\ntext: '+paramtxt+'\nprice: '+paramprice);

        $.ajax({
            url: '/service/home/tool-search-result/?row=4&id='+id+'&qrycat='+paramcat+'&qrytxt='+paramtxt+'&qryprice='+paramprice+'&rnd='+Math.random(),
            success: function(data){
                $(search_home_right).fadeOut(100, function(){

                    $(search_home_right).html(data);
                    $(search_home_right).fadeIn(500, function(){

                    if ($(".searchpage").length > 0) {
                        initProjectMapListClick();
                        initProjectMapList();
                    }

                    });
                });
            }
        });
    });
}

function initSearchSlider() {
    // select price project

    jQuery(".Slider-Price").slider({
        from: 0,
        to: 999,
        heterogeneity: ['56/5','67/10','89/30','99.99/998.99'],
        scale: [0,1,2,3,4,5,10,20,30,999],
        step: 1,
        dimension: '&nbsp;MB',
        max: 30,
        limits: false
    });

    $('#mnu-quick-search').hide();
    $('#mnu-quick-search').css('visibility','visible');
}

function initSearchBtnClick() {
    $('.btnSearch-Project').click(function(){

        search_home_right = $(this).parent().parent().parent().find("#search-home-right");
        id = $(search_home_right).attr('class');
        //alert(id);

        paramcat = '';

        $(this).parent().find('.content-search-project-btn').each(function(index) {
            if($(this).hasClass('selected')) {
                paramcat = paramcat+eval(index+1)+',';
            }
        });

        paramtxt = encodeURI($('#txtSearch_'+id).val());
        paramprice = $("#Slider_"+id).slider('value').replace(';', ',') ;

        //alert('Project: '+paramcat+'\ntext: '+paramtxt+'\nprice: '+paramprice);

        $.ajax({
            url: '/service/home/tool-search-result/?row=4&id='+id+'&qrycat='+paramcat+'&qrytxt='+paramtxt+'&qryprice='+paramprice+'&rnd='+Math.random(),
            success: function(data){

                $(search_home_right).fadeOut(100, function(){

                  $(search_home_right).html(data);
                  $(search_home_right).fadeIn(500, function(){

                    if ($(".searchpage").length > 0) {
                        initProjectMapListClick();
                        initProjectMapList();
                    }

                    });
                });
            }
        });
    });
}

function initProjectMapList() {

    $("#project-map-list").carouFredSel({
                        width	: 740,
                        height	: 190,
                        scroll	: 2000,
                        circular: false,
                        infinite: false,
                        auto	: false,
                        prev	: "#foo2_prev",
                        next	: "#foo2_next"
                    });
                    
    $("#brand-project-list").carouFredSel({
                        width	: 961,
                        height	: 190,
                        scroll	: 2000,
                        circular: false,
                        infinite: false,
                        auto	: false,
                        prev	: "#foo2_prev",
                        next	: "#foo2_next"
                    });                
}

function toggleDisabled(el) {
                try {
                    el.disabled = el.disabled ? false : true;
                }
                catch(E){}

                if (el.childNodes && el.childNodes.length > 0) {
                    for (var x = 0; x < el.childNodes.length; x++) {
                        toggleDisabled(el.childNodes[x]);
                    }
                }
            }

function initProjectMapListClick() {

    $('.iconcheck').click(function(){

        //alert($(this).attr('id'));
        if ($(this).parent().find(".selected").length > 0) {
            $(this).parent().find('.project_map').css('cursor','default');
            $(this).parent().find('.project_map').removeClass('enabled');
            $(this).parent().removeClass('enabled');
            $(this).parent().find('.project_map').unbind('click');
            //alert("selected");
            gpos_searchpage[eval($(this).attr('id'))].show = 0;
        } else {
            $(this).parent().find('.project_map').css('cursor','pointer');
            $(this).parent().find('.project_map').addClass('enabled');
            $(this).parent().addClass('enabled');
            $(this).parent().find('.project_map').click(function(){gmaplocate(this,1);});
            //alert("none");
            gpos_searchpage[eval($(this).attr('id'))].show = 1;
        }
        $(this).toggleClass('selected');
        gmaplocate($(this).parent().find('.project_map'),0);
    });
}

function initSearchBtnClear() {

    $('.txtsearch').keyup(function(){
       if ($(this).val().length > 0) {
           $(this).parent().find('.xbtn').css('display','block');
       } else {
           $(this).parent().find('.xbtn').css('display','none');
       }
     })

     $('.xbtn').click(function(){
         $('.xbtn').css('display','none');
         $(this).parent().find('.txtsearch').val('');
         $(this).parent().find('.txtsearch').focus();
     });
}

function gmaplocate(el,locate){

            //alert('gpos_'+$(el).parent().parent().parent().parent().parent().attr('class'));
            get_gpos = eval('gpos_'+$(el).parent().parent().parent().parent().parent().attr('class'));
            gpos = [];

            gpos_center = $(el).attr('rel');
            gpos_center = gpos_center.split(',');
            vindex = 0;

            for (var i in get_gpos) {
                if (get_gpos[i].show == 1) {
                    gpos.push(get_gpos[i]);

                    gpos[vindex].popup = false
                    if (locate == 1) {
                        if ((gpos[vindex].latitude==gpos_center[0]) && (gpos[vindex].longitude==gpos_center[1])) {
                            gpos[vindex].popup = true
                        }
                    }
                    vindex = vindex+1;
                }
            }

            gzoom = $gmap.getZoom();

            search_map = $(el).parent().parent().parent().parent().parent().parent().find("#search-map");


            if (locate == 1) {
                $(search_map).gMap({
                    latitude:     gpos_center[0],
                    longitude:    gpos_center[1],
                    zoom:         gzoom,
                    markers:      gpos/*,
                    urlkml:       "http://www.ap-thai.com/images/BTSMRT.kml"*/
                });
            } else {
                $(search_map).gMap({
                    zoom:         0,
                    markers:      gpos/*,
                    urlkml:       "http://www.ap-thai.com/images/BTSMRT.kml"*/
                });
            }

           //initCalculatorTip();
           //jQuery('.tooltip-show').colorTip();

        }



