function $(id) {
  return document.getElementById(id);
}


function xml2array(xml) {

var result = new Array();
 var key = null;
 var value = null;
 var start = xml.getElementsByTagName("item")[0];
 for (var i = 0; i < start.childNodes.length; i++) {
   key = xml.getElementsByTagName("part")[i].getAttribute("id");
   //£ata dla IE je¿eli puste pole
   if (xml.getElementsByTagName("part")[i].firstChild != null) {
   
	value = xml.getElementsByTagName("part")[i].firstChild.nodeValue;
   
   } else {
   
	value = "Error";
   
   }
   result[key] = value;
 }
 
 
 return result;
}


// create the HelloWorld application (single instance)
var HelloWorld = function(){
    // everything in this space is private and only accessible in the HelloWorld block
    
    // define some private variables
    var dialog, showBtn;
    
    var toggleTheme = function(){
        getEl(document.body, true).toggleClass('ytheme-gray');
    };
    
    
     
	
    // return a public interface
    return {
        init : function(){
        

        
			 dane = getEl('webDane');
			 dane.hide();
             showBtn = getEl('show-dialog-btn');
             getBtn = getEl('getData');

             if (showBtn) showBtn.on('click', this.showDialog, this, true);
             if (getBtn) getBtn.on('click', this.getXML,this, true);
             
        },
        
        showDialog : function(){
        

            if(!dialog){ // lazy initialize the dialog and only create it once
                var dialog = new YAHOO.ext.BasicDialog("hello-dlg", { 
                        //modal:true,
                        autoTabs:true,
                        width:700,
                        height:350,
                        shadow:true,
                        minWidth:300,
                        minHeight:250,
                        proxyDrag: true
                });
                dialog.addKeyListener(27, dialog.hide, dialog);
                dialog.addButton('Zamknij', dialog.hide, dialog);
                dialog.addButton('Dodaj do katalogu', this.add, dialog);

            }
            dialog.show(showBtn.dom);
            this.dialog = dialog;
        },
        
        handleSuccess:function(o) {
        
			try {
			alert(o.responseXML);
			
			tablica = xml2array(o.responseXML);
			
			$('keyw').value = tablica['keyw'];
			$('tytul').value = tablica['tytul'];
			$('adres').value = tablica['url'];
			$('opis').value = tablica['opisik'];
											
			dane = getEl('webDane');
			dane.show();
			$('webpic').innerHTML = '<img src="http://msnsearch.srv.girafa.com/srv/i?s=MSNSEARCH&r='+tablica['url'] + '">';
			
			} catch (err) {
			
				alert(err.description);
			
			}
        },
        
        getXML : function(){
        
		
			try {
			
			
			urlek2 = document.getElementById('urlek').value;
			
			//alert(urlek2);

			sUrl = 'getXML.php?url=' + urlek2;
			
			var callback =
			{
			   success:this.handleSuccess,
			   failure:this.handleFailure
			}; 
        
		
          
			YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);	
				
			} catch (err) {
			
			//alert(err);
			
			}	
				
				
		},
        
          
       add : function(dialog) {
        
			/*
			reg = /^[a-zA-Z0-9±æê³ñó¶¿¼¡ÆÊ£ÑÓ¦¯¬.-]{1,30}@[a-zA-Z0-9±æê³ñó¶¿¼¡ÆÊ£ÑÓ¦¯¬-]+(\.[a-zA-Z0-9±æê³ñó¶¿¼¡ÆÊ£ÑÓ¦¯¬]+)+$/;
			
			wyn = $('email').value.match(reg);
			
			if (wyn == null) {
			
				alert("Podaj poprawny adres e-mail");
				
				return;
			
			}
			*/
			
			if ($('tytul').value.length < 4) { alert('Zbyt krótki tytu³ - minimum 4 znaki'); return;}
			if ($('opis').value.length < 10) { alert('Zbyt krótki opis - minimum 10 znaków'); return;}
			if ($('keyw').value.length < 10) { alert('Zbyt ma³a ilo¶æ s³ów kluczowych - minimum 10 znaków'); return;}
       
			var request = YAHOO.util.Connect.asyncRequest(
					'POST', 'dodajurl.php',
					{ 
						success: function(o) { 
						
						var message = new YAHOO.ext.BasicDialog("message", { 
								modal:true,
								autoTabs:true,
								width:300,
								height:150,
								shadow:true,
								minWidth:300,
								minHeight:300
						});
						
						function ukryjoba() {
						
						message.hide();
						message.disable();
						addPage.dialog.hide();
						addPage.dialog.disable();
						
						di = getEl('hello-dlg');
						

						}
						
						message.addKeyListener(27, message.hide, message);
						message.addButton('Zamknij', ukryjoba, message);
						$('messagebox').innerHTML = 'Strona zosta³a dodana. Dziêkujemy';
						message.show();
						
						
					
						}
					}
					,
					"pod=" + $('pod').value + "&" +
					"kat=" + $('kat').value + "&" +
					"tytul=" + $('tytul').value + "&" +
					"url=" + $('adres').value + "&" +
					"slowa=" + $('keyw').value + "&" +
					"email=" + $('email').value + "&" +
					"opis=" + $('opis').value ); 
					
					
					
       
       
       
        }
    };
}();

var addPage = function(){

    var dialog, showBtn;

    return {
        init : function(){
        

        
			 dane = getEl('webDane');
			 dane.hide();
             showBtn = getEl('show-dialog-btn');
             getBtn = getEl('getData');

             if (showBtn) showBtn.on('click', this.showDialog, this, true);
             if (getBtn) getBtn.on('click', this.getXML,this, true);
             
             if(!dialog){ // lazy initialize the dialog and only create it once
                var dialog = new YAHOO.ext.BasicDialog("hello-dlg", { 
                        //modal:true,
                        autoTabs:true,
                        width:700,
                        height:350,
                        shadow:true,
                        minWidth:300,
                        minHeight:250,
                        proxyDrag: true
                });
                dialog.addKeyListener(27, dialog.hide, dialog);
                dialog.addButton('Zamknij', dialog.hide, dialog);
                dialog.addButton('Dodaj do katalogu', this.add, dialog);

            }
              
           
            this.dialog = dialog;
        },
        
        showDialog : function(){
        
			showBtn = getEl('show-dialog-btn');
        
			this.dialog.show(showBtn.dom);
        
        },
        
        handleSuccess:function(o) {
        
			try {
			
			tablica = xml2array(o.responseXML);
			
			$('keyw').value = tablica['keyw'];
			$('tytul').value = tablica['tytul'];
			$('adres').value = tablica['url'];
			$('opis').value = tablica['opisik'];
											
			dane = getEl('webDane');
			dane.show();
			$('webpic').innerHTML = '<img src="http://msnsearch.srv.girafa.com/srv/i?s=MSNSEARCH&r='+tablica['url'] + '">';
			
			} catch (err) {
			
				alert(err.description);
			
			}
        },
        
        getXML : function(){
        
		
			try {
			
			
			urlek2 = document.getElementById('urlek').value;
			
			sUrl = 'getXML.php?url=' + urlek2;
			
			var callback =
			{
			   success:this.handleSuccess,
			   failure:this.handleFailure
			}; 
        
		
          
			YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);	
				
			} catch (err) {
			
			}	
				
				
		},
        
          
       add : function(dialog) {
        
			reg = /^[a-zA-Z0-9±æê³ñó¶¿¼¡ÆÊ£ÑÓ¦¯¬.]{1,30}@[a-zA-Z0-9±æê³ñó¶¿¼¡ÆÊ£ÑÓ¦¯¬]+(\.[a-zA-Z0-9±æê³ñó¶¿¼¡ÆÊ£ÑÓ¦¯¬]+)+$/;
			
			wyn = $('email').value.match(reg);
			
			if (wyn == null) {
			
				alert("Podaj poprawny adres e-mail");
				
				return;
			
			}
			
			if ($('tytul').value.length < 4) { alert('Zbyt krótki tytu³ - minimum 4 znaki'); return;}
			if ($('opis').value.length < 10) { alert('Zbyt krótki opis - minimum 10 znaków'); return;}
			if ($('keyw').value.length < 10) { alert('Zbyt ma³a ilo¶æ s³ów kluczowych - minimum 10 znaków'); return;}
       
			var request = YAHOO.util.Connect.asyncRequest(
					'POST', 'dodajurl.php',
					{ 
						success: function(o) { 
						
						//alert(o.responseText);
						
						var message = new YAHOO.ext.BasicDialog("message", { 
								modal:true,
								autoTabs:true,
								width:300,
								height:150,
								shadow:true,
								minWidth:300,
								minHeight:300
						});
						
						function ukryjoba() {
						
						message.hide();
						addPage.dialog.hide();
						
						di = getEl('hello-dlg');
						

						}
						
						message.addKeyListener(27, message.hide, message);
						message.addButton('Zamknij', ukryjoba, message);
						$('messagebox').innerHTML = 'Strona zosta³a dodana. Dziêkujemy';
						message.show();
						
						
					
						}
					}
					,
					"pod=" + $('pod').value + "&" +
					"kat=" + $('kat').value + "&" +
					"tytul=" + $('tytul').value + "&" +
					"url=" + $('adres').value + "&" +
					"slowa=" + $('keyw').value + "&" +
					"email=" + $('email').value + "&" +
					"opis=" + $('opis').value ); 
					
					
					
       
       
       
        }
    };
}();

// using onDocumentReady instead of window.onload initializes the application
// when the DOM is ready, without waiting for images and other resources to load
YAHOO.ext.EventManager.onDocumentReady(addPage.init, addPage, true);





var reportPage = function(){

    var raport, raportBtn;
    var url;

    return {
        init : function(){
			/*
			 dane = getEl('webDane');
			 dane.hide();
             showBtn = getEl('show-dialog-btn');
             getBtn = getEl('getData');

             if (showBtn) showBtn.on('click', this.showDialog, this, true);
             if (getBtn) getBtn.on('click', this.getXML,this, true);
             */
             
             if(!raport){ // lazy initialize the dialog and only create it once
                var raport = new YAHOO.ext.BasicDialog("reportPage", { 
                        //modal:true,
                        autoTabs:true,
                        width:340,
                        height:200,
                        shadow:true,
                        minWidth:300,
                        minHeight:250,
                        proxyDrag: true
                });
                //dialog.addKeyListener(27, dialog.hide, dialog);
                raport.addButton('Anuluj', raport.hide, raport).disable;
                raport.addButton('Zg³o¶', this.report, raport);
                this.raport = raport;
                

            }
            
             
             
        },
        
        showDialog : function(dane,id){
			
				//alert(dane);

				$('rmessagebox').innerHTML = 'Czy napewno chcesz zg³osiæ <br /><br /><b>' + dane + '</b>';
				$('rmessageboxurl').value = dane;
				reportPage.raport.show(id.dom);
        
        },
        
        report : function() {
        
			

			var request = YAHOO.util.Connect.asyncRequest(
					'POST', 'broken.php',
					{ 
						success: function(o) { 
							
							reportPage.raport.hide();
							reportPage.raport.disable();
						
						}
					}
					,
					"wpis=" + $('rmessageboxurl').value);
        
        
        
        }
       
    };
}();



// using onDocumentReady instead of window.onload initializes the application
// when the DOM is ready, without waiting for images and other resources to load
YAHOO.ext.EventManager.onDocumentReady(reportPage.init, reportPage, true);


var addCat = function(){

    var kat, katBtn;
    var name;

    return {
        init : function(){
			/*
			 dane = getEl('webDane');
			 dane.hide();
             showBtn = getEl('show-dialog-btn');
             getBtn = getEl('getData');

             if (showBtn) showBtn.on('click', this.showDialog, this, true);
             if (getBtn) getBtn.on('click', this.getXML,this, true);
             */
             
             if(!kat){ // lazy initialize the dialog and only create it once
                var kat = new YAHOO.ext.BasicDialog("kmessage", { 
                        //modal:true,
                        autoTabs:true,
                        width:340,
                        height:240,
                        shadow:true,
                        minWidth:300,
                        minHeight:250,
                        proxyDrag: true
                });

                kat.addButton('Anuluj', kat.hide, kat).disable;
                kat.addButton('Dodaj', this.addkat, kat);
                this.kat = kat;
                

            }
            
             
             
        },
        
        showDialog : function(dane,id){

				$('kmessageboxpod').value = dane;
				addCat.kat.show(id.dom);
        
        },
        
        addkat : function() {
        
			var request = YAHOO.util.Connect.asyncRequest(
					'POST', 'dodaj_podkategorie.php',
					{ 
						success: function(o) { 
							
							addCat.kat.hide();
							
							window.location.reload(); 
						
						}
					}
					,
				"kat=" + $('kmessageboxpod').value + "&x=y&pod="+ $('podk').value);			
			
			
        
        }
       
    };
}();



// using onDocumentReady instead of window.onload initializes the application
// when the DOM is ready, without waiting for images and other resources to load
YAHOO.ext.EventManager.onDocumentReady(addCat.init, addCat, true);
