/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2008 Phoenix Medien GmbH & Co. KG (http://www.phoenix-medien.de)
 */

var PhoenixDistributorInstance = '';

var PhoenixDistributor = Class.create();


function lightboxHrefObj(href) {
	this.href = href;
}

PhoenixDistributor.prototype = {
    initialize: function(ajaxUrl){
        this.ajaxUrl	= ajaxUrl;
        this.submitEl   = '';
        this.win		= '';
        this.country	= '';
        this.countryResponse = '';
        this.distributorResponse = '';
    },
    
    submit: function(ev) {
		if (PhoenixDistributorInstance.ajaxUrl == '')
			return true;

    		// close open dialogs
	    if (document.getElementById('lbContent') && typeof PhoenixDistributorInstance.win == 'object')
	    	PhoenixDistributorInstance.win.deactivate();

   			// don't proceed on validation errors
		if (productAddToCartForm && (typeof ev == 'undefined' || ev.type == 'submit') && !productAddToCartForm.validator.validate())
   			return true;

			// stop event and save event target for later processing
    	if (ev != null) {
			if (window.event)
				ev.currentTarget = window.event.srcElement;
    	
    		if (ev.type == 'submit' || ev.type == 'click')
	    		Event.stop(ev);
	    		
    		if (ev.currentTarget && ev.currentTarget.tagName != 'A' && ev.currentTarget.tagName != 'FORM')
    			this.submitEl = ev.currentTarget.parentNode;
    		else
	    		this.submitEl = ev.currentTarget;
	    }
	    	
	    	// check for current country selection
		new Ajax.Request(PhoenixDistributorInstance.ajaxUrl + 'country/',
		  {
		    method:'get',
		   	onSuccess: function(transport) { PhoenixDistributorInstance.setCountryCode(transport.responseText); },
		    onComplete: function() { PhoenixDistributorInstance.controller(); }
		  });
    },

    controller: function () {
    	if (PhoenixDistributorInstance.country == '') {
			new Ajax.Request(PhoenixDistributorInstance.ajaxUrl + 'country/list/',
			  {
			    method:'get',
		    	onSuccess: function(transport) { PhoenixDistributorInstance.setCountryResponse(transport.responseText); },
			    onComplete: function() { PhoenixDistributorInstance.countrySelectionView(); }
			  });
    	} else {
			new Ajax.Request(PhoenixDistributorInstance.ajaxUrl + 'list/country/' + PhoenixDistributorInstance.country + '/' + PhoenixDistributorInstance.getCategoryInfo(),
			  {
			    method:'get',
		    	onSuccess: function(transport) { PhoenixDistributorInstance.setDistributorResponse(transport.responseText); },
			    onComplete: function() { PhoenixDistributorInstance.distributorListView(); }
			  });
    	}
	},
	
    countrySelectionView: function () {
//		if (this.countryResponse == '') {
//			this.submitForm();
//		} else {
			
			var test = new lightboxHrefObj(PhoenixDistributorInstance.ajaxUrl + 'country/list/');
			PhoenixDistributorInstance.win = new lightbox(test);
			PhoenixDistributorInstance.win.activate();
		
//			this.win = new Window({className:"magento", width:350, height:200, draggable:false, showEffect:Element.show, hideEffect:Element.hide, destroyOnClose:true});
//			this.win.getContent().update(this.countryResponse);
//			this.win.showCenter();
//			return true;
//		}
    },
    
    distributorListView: function(country) {
    
		if (PhoenixDistributorInstance.distributorResponse == '') {
			PhoenixDistributorInstance.submitElement();
		} else {
			var test = new lightboxHrefObj(PhoenixDistributorInstance.ajaxUrl + 'list/country/'+PhoenixDistributorInstance.country+'/'+PhoenixDistributorInstance.getCategoryInfo());
			this.win = new lightbox(test);
			this.win.activate();

//			this.win = new Window({className:"magento", width:350, height:200, draggable:false, showEffect:Element.show, hideEffect:Element.hide, destroyOnClose:true});
//			this.win.getContent().update(this.distributorResponse);
//			this.win.showCenter();
			return true;
		}
    
    },
    
    submitElement: function() {
    	if (PhoenixDistributorInstance.submitEl) {
    		if (PhoenixDistributorInstance.submitEl.tagName == 'A')
    			window.location.href = PhoenixDistributorInstance.submitEl.href;
    		if (PhoenixDistributorInstance.submitEl.tagName == 'FORM')
    			PhoenixDistributorInstance.submitEl.submit();
    	}
    },

	getCategoryInfo: function() {    
   		var catInfo = '';
   		if (this.submitEl.tagName == 'A' && this.submitEl.id.substr(0,4) == 'cat_')
   			catInfo = 'category/'+this.submitEl.id.substr(4)+'/';
   		return catInfo;
	},    	


    observeCollectionLinks: function() {
    	var catParent = document.getElementsByClassName('active', document.getElementById('nav'));
		var cnt = 0;
		for(i=0;i<catParent.length;i++) {
  			if (catParent[i].hasClassName('level1')) {
  				var catParent = catParent[i];
  				break;
  			}
  		}
  		
  		var collectionLinks = document.getElementsByClassName('collection_link', catParent);
  		for(i=0;i<collectionLinks.length;i++) {
  			Event.observe(collectionLinks[i].id, 'click', function(ev){ PhoenixDistributorInstance.submit(ev); return false; });
  		}
    },
    
    setCountryCode: function(countryCode) {
    	this.country = countryCode;
    },
    
    setCountryResponse: function(html) {
    	this.countryResponse = html;
    },
    
    setDistributorResponse: function(html) {
    	this.distributorResponse = html;
    }

};
