var tree;
var myLayout;
$(document).ready(function () {
    myLayout = $('body').layout({
    west__onresize:                 initPaneScrollbar
    ,	triggerEventsOnLoad:        false
    ,	resizeWhileDragging:        true
    ,	triggerEventsWhileDragging: false
    ,	useStateCookie:             false
    ,	north__spacing_closed:      4
    //,	togglerLength_open: 0
    //,	togglerLength_closed: 0
    });
	
    myLayout.sizePane("north", 104);
    myLayout.sizePane("west", 250);
    myLayout.addToggleBtn("#west-closer", "west");


    // NOW init all scrollbars
    //initPaneScrollbar( 'west', myLayout.panes.west );
    //initPaneScrollbar( 'center', myLayout.panes.center );
    
    //1 - MANEJO DEL BOTON DE EXPANDIR-CONTRAER VENTANA DE RESULTADOS
    $("#west-closer").click(function(){
            var tituloObj=$("#west-closer").attr("title")
            if(tituloObj == "Abrir"){
                    $('#west-closer').attr({
                       'title': 'Cerrar',
                       'class': 'ui-layout-toggler-vertical-show'
                    });
            }else{
                    $('#west-closer').attr({
                       'title': 'Abrir',
                       'class': 'ui-layout-toggler-vertical-hide'
                    });
            }
    });
    $("#west-closer").mouseover(function(){
            var tituloObj=$("#west-closer").attr("title")
            if(tituloObj == "Abrir"){
                    $("#west-closer").addClass("ui-layout-toggler-vertical-select-hide");
            }else{
                    $("#west-closer").addClass("ui-layout-toggler-vertical-select-show");
            }
    });
    $("#west-closer").mouseout(function(event){
            var tituloObj=$("#west-closer").attr("title")
            if(tituloObj == "Abrir"){
                    $("#west-closer").removeClass("ui-layout-toggler-vertical-select-hide");
            }else{
                    $("#west-closer").removeClass("ui-layout-toggler-vertical-select-show");
            }
    }); 
    
    //FIN 1
    $('#searchPrincipal').attr({
       'value': 'Ej.: Pizza, Calzado, Ferreteria',
       'title': 'Ej.: Pizza, Calzado, Ferreteria'
    });
    $("#searchPrincipal").focus(function(event){
            if($("#searchPrincipal").attr("value") == 'Ej.: Pizza, Calzado, Ferreteria'){
                    $('#searchPrincipal').attr({
                       'value': ''
                    });
            }
    });
    $("#searchPrincipal").blur(function(event){
            if($("#searchPrincipal").attr("value") == ''){
                    $('#searchPrincipal').attr({
                       'value': 'Ej.: Pizza, Calzado, Ferreteria'
                    });
            }
    });
	$("#searchPrincipal").keypress(function(event) {
		if ( event.which == 13 ) {
			search_establecimiento();
		}
	});

	//Initialize PrettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
    $("#registerImg").mousedown(function(){
            $("a[rel^='prettyPhoto']").prettyPhoto();
    });
    
    //Categorias Principales
	/*
	tree = new dhtmlXTreeObject("treeboxbox_tree", "100%", "100%", 0);
    tree.setSkin('dhx_skyblue');
    tree.setImagePath("./images/csh_vista/");
    tree.enableSmartXMLParsing(true); 
    tree.loadXML("./components/treeCategories.php");
    tree.setOnClickHandler(tonclick);
    tree.openOnItemAdding(true);*/
	
    $("#searchPrincipal").autocomplete("./components/get_categories_list.php", {
        width: 260,
        matchContains: true,
        selectFirst: false
    });
	
    $("#btnSearch").click(function(event){
        search_establecimiento();
    });
});

function initPaneScrollbar ( pane, $Pane ) {
	$Pane.find("div.scrolling-content:first")
		// re/init the pseudo-scrollbar
		.jScrollPane({
			scrollbarMargin:	10	// spacing between text and scrollbar
		,	scrollbarWidth:		10
		,	arrowSize:			16
		,	showArrows:			true
		})
		// REMOVE the *fixed width & height* just set on jScrollPaneContainer
		.parent(".jScrollPaneContainer").css({
			width:	'100%'
		,	height:	'100%'
		})
	;
};
function search_establecimiento(){
	var idCateg;
	var categoria = $("#searchPrincipal").attr("value");
        if(categoria != "Ej.: Pizza, Calzado, Ferreteria"){
            $.ajax({
               type: "POST",
               url: "./components/searchCategoria.php",
               data: "categoria="+categoria,
               beforeSend: function(objeto){
                   $('#searchPrincipal').attr({
                       'value': 'Buscando...'
                    });
               },
               complete: function(objeto, exito){
                   finalSearch(idCateg,categoria);
               },
               success: function(msg){
                   idCateg = msg;
               }
             });
	}
};
function finalSearch(id,categoria){
    $.ajax({
       type: "POST",
       url: "./components/listEstablecimientos.php",
       data: "categoria="+id+"&descripcion="+categoria,
       beforeSend: function(objeto){
           $('#searchPrincipal').attr({
               'value': 'Buscando...'
            });
       },
       complete: function(objeto, exito){
            $('#searchPrincipal').attr({
               'value': 'Ej.: Pizza, Calzado, Ferreteria',
               'title': 'Ej.: Pizza, Calzado, Ferreteria'
            });
			initPaneScrollbar( 'center', myLayout.panes.center );
       },
       success: function(msg){
           if(msg != "0"){
               $("#listado").html(msg);
           }else{
               alert("Estimado usuario no se encontraron establecimientos que concuerden con su busqueda!");
           }
       }
     });
}
function enviarMail(mail){
    if(mail != ''){
        var mailt = 'mailto:'+mail;
        parent.location = mailt;
    }
}
function enviarWebsite(website){
    if(website != ''){
        window.open("http://"+website);
    }
}
function viewAdvertising(establecimiento){
	if(establecimiento != ''){
        $.ajax({
		   type: "POST",
		   url: "./components/viewEstablecimientos.php",
		   data: "establecimiento="+establecimiento,
		   beforeSend: function(objeto){
			   $('#searchPrincipal').attr({
				   'value': 'Buscando...'
				});
		   },
		   complete: function(objeto, exito){
				$('#searchPrincipal').attr({
				   'value': 'Ej.: Pizza, Calzado, Ferreteria',
				   'title': 'Ej.: Pizza, Calzado, Ferreteria'
				});
				$("a[rel^='prettyPhoto']").prettyPhoto();
		   },
		   success: function(msg){
				$("#listado").html(msg);
		   }
		 });
    }
}
function coordenadas(cx,cy){
	$('#txt_cx').attr({
	   'value': cx
	});
	$('#txt_cy').attr({
	   'value': cy
	});
}

function tonclick(id) {
	var idTmp = id.substr(0,1);
    if(id != '' && idTmp != 0){
        var descCategoria = tree.getItemText(id);//TEXTO QUE SE ENCUENTRA EN EL ARBOL
        $.ajax({
           type: "POST",
           url: "./components/listEstablecimientos.php",
           data: "categoria="+id+"&descripcion="+descCategoria,
           beforeSend: function(objeto){
               $('#searchPrincipal').attr({
                   'value': 'Buscando...'
                });
           },
           complete: function(objeto, exito){
                $('#searchPrincipal').attr({
                   'value': 'Ej.: Pizza, Calzado, Ferreteria',
                   'title': 'Ej.: Pizza, Calzado, Ferreteria'
                });
				initPaneScrollbar( 'center', myLayout.panes.center );
           },
           success: function(msg){
               if(msg != "0"){
                   $("#listado").html(msg);
               }else{
                   alert("¡Estimado usuario no se encontraron establecimientos que concuerden con su busqueda!");
               }
                
           }
         });
    }
};
function link_social(social){
	if(social == 'facebook'){
		window.open('http://www.facebook.com/sharer.php?u=http://www.mymegaportal.com/&t=Directorio%20Virtual%20de%20tu%20Localidad','ventanacompartir', 'toolbar=0, status=0, width=650, height=450');
	}
	if(social == 'twitter'){
		window.open('http://twitter.com/home?status=Buscando%20todo%20lo%20que%20necesito%20en%20mi%20localidad%20en%20http://www.mymegaportal.com','ventanacompartir', 'toolbar=0, status=0, width=650, height=450');
	}
	
}
