﻿Type.registerNamespace('Shopping.Controls');

Shopping.Controls.CatalogControl = function(element) {
	Shopping.Controls.CatalogControl.initializeBase(this, [element]);
	this._productInfo = null;
    this._pageSize = null;
    this._currentPage = null;
    this._totalProducts = null;
	this._clickDelegate = null;
	this._navDelegate = null;
	this._openpopupDelegate = null;
}

Shopping.Controls.CatalogControl.prototype = 
{
	initialize: function() {
		Shopping.Controls.CatalogControl.callBaseMethod(this, 'initialize');
		
		if (this._navDelegate === null) {
            this._navDelegate = Function.createDelegate(this, this._navHandler);
        }
        
        if (this._clickDelegate === null) {
            this._clickDelegate = Function.createDelegate(this, this._clickHandler);
        }
        if (this._openpopupDelegate === null) {
            this._openpopupDelegate = Function.createDelegate(this, this._openpopupHandler);
        }
	},
	
	dispose: function() {
        $clearHandlers(this.get_element());
    },
    
	// Page Size
	get_pageSize: function() { return this._pageSize; },
	set_pageSize: function(value) { this._pageSize = value; }, 

	// Current Page
	get_currentPage: function() { return this._currentPage; },
	set_currentPage: function(value) { this._currentPage = value; }, 

	// Total Products
	get_totalProducts: function() { return this._totalProducts; },
	set_totalProducts: function(value) { this._totalProducts = value; }, 

    // Products
	get_productInfo: function() { return this._productInfo; },
	set_productInfo: function(value) { this._productInfo = value; }, 

	dataBind: function()
	{
	    this.render();
	},
	
    // Bind and unbind to click event.
    add_click: function(handler) {
        this.get_events().addHandler('click', handler);
    },
    remove_click: function(handler) {
        this.get_events().removeHandler('click', handler);
    },
	
	// Expose the navigate event and add to EventHandlerList
    add_navigate: function(handler) {
        this.get_events().addHandler('navigate', handler);
    },
    remove_navigate: function(handler) {
        this.get_events().removeHandler('navigate', handler);
    },
    
    //הוספת ארוע של לחיצה על תמונה פותחת חלון
    add_openpopup: function(handler) {
        this.get_events().addHandler('openpopup', handler);
    },
    remove_openpopup: function(handler) {
        this.get_events().removeHandler('openpopup', handler);
    },
    
    _clickHandler: function(event, args) {
        var h = this.get_events().getHandler('click');
        if (h) h(this, args);
    },
    
    _navHandler: function(event, args){
        var h = this.get_events().getHandler('navigate');
        if (h) h(this, args);
    },
    
    _openpopupHandler: function(event, args){
        var h = this.get_events().getHandler('openpopup');
        if (h) h(this, args);
    },
	
	render: function() 
	{
	   
      	var element = this.get_element();

		$clearHandlers(this.get_element());
		
        // Clear content from the element
		element.innerHTML = '';

        var nextPrev = document.createElement('div');
        this.addNavigation(nextPrev);
        nextPrev.className="nextPrev";
        element.appendChild(nextPrev);
		for (var i = 0; i < this._productInfo.Products.length; i++) 
		{
		/*
		 <div class="book" >
              <strong class="bookName">דודי ואודי מצילים את הכפר</strong>
              <img src="App_Themes/superSefer/images/noBook.jpg" class="imgBook" />
              <div class="bookData">
              מחיר: 256 ש"ח
              <br /><br /><br />
              <span class="bookDescription">
              כאן יש מידע על הספר.. ועוד ועוד...
              </span> 
              </div>
              <img class="selectbutton" src="images/selectbutton.jpg" />
            </div>*/
		
            var div = document.createElement('div');
            div.className="book";
            var strong = document.createElement('strong');
            div.appendChild(strong);
            strong.className="bookName";
            strong.innerHTML = this._productInfo.Products[i].ProductName;
            var book_img = document.createElement('img');
            book_img.setAttribute("src", "../files/" + this._productInfo.Products[i].Picture);
            book_img.setAttribute("border", "0");
            book_img.className="imgBook";
            book_img.setAttribute("alt", this._productInfo.Products[i].ProductName);
            //book_img.setAttribute("onclick", "window.open('popUpBookDetail.aspx?bookID=" + this._productInfo.Products[i].ProductId+"','cal','width=500,height=500,left=270,top=180');");  
          //  $addHandler(book_img, 'click', openPopUpWin); 
            $addHandler(book_img, 'click', Function.createCallback(this._openpopupDelegate, this._productInfo.Products[i]));
              
           // var a = document.createElement('a');
            // a.setAttribute("target", "_blank");
           // a.setAttribute("href", "bookDetail.aspx?bookID=" + this._productInfo.Products[i].ProductId);
//            a.appendChild(book_img);
//            div.appendChild(a);
            
            div.appendChild(book_img);
            element.appendChild(div);
            var subDiv = document.createElement('div');
            subDiv.className="bookData";
            subDiv.innerHTML ="מחיר: " + this._productInfo.Products[i].Price +" ש''ח";
            subDiv.appendChild(document.createElement('br'));
            subDiv.appendChild(document.createElement('br'));
            subDiv.appendChild(document.createElement('br'));
            var span = document.createElement('span');
            span.className="bookDescription";
            span.innerHTML = this._productInfo.Products[i].Description;
            subDiv.appendChild(span);
            div.appendChild(subDiv);
            var  selectImg = document.createElement('img');
            selectImg.className="selectbutton";
            selectImg.setAttribute("src", "../images/selectbutton.jpg");
            selectImg.setAttribute("border", "0");
            div.appendChild(selectImg);

            $addHandler(selectImg, 'click', Function.createCallback(this._clickDelegate, this._productInfo.Products[i]));
            element.appendChild(div);
		}
		

     


	},
	
	addNavigation: function (tableBody)
	{
	
	     // See if there are any previous
        //Add The prev image
    
        var img = document.createElement('img');
        if (this._productInfo.StartIndex == 1)
        {
            img.src = "../images/previous_button_disabled.jpg";
            img.disabled = true;
        }
        else
        {
            img.src = "../images/previous_button.jpg";
            img.disabled = false;
            $addHandler(img, 'click', Function.createCallback(this._navDelegate, 'Previous'));
            $create(Samples.HighlightBehavior, 
                    {"highlightImage":"../images/previous_button_highlight.jpg"},
                    null, 
                    null,
                    img);            
        }
        tableBody.appendChild(img);
	
	     //Add The next image
        var img = document.createElement('img');
        if (this._productInfo.EndIndex >= this._productInfo.TotalCount)
        {
            img.src = "../images/next_button_disabled.jpg";
            img.disabled = true;
        }
        else
        {
            img.src = "../images/next_button.jpg";
            img.disabled = false;
            $addHandler(img, 'click', Function.createCallback(this._navDelegate, 'Next'));
            $create(Samples.HighlightBehavior, 
                    {"highlightImage":"../images/next_button_highlight.jpg"},
                    null, 
                    null,
                    img);

        }
        tableBody.appendChild(img); 
	
	
	    
   
     
       	
        
      
       
	}
	
}
  

Shopping.Controls.CatalogControl.registerClass('Shopping.Controls.CatalogControl', Sys.UI.Control);
Sys.Application.notifyScriptLoaded();