var ArtWindow = function() {  
	/**
	 * Stores the Scrollable API
	 */
	var _api;     
	
	/**
	 * Current index helper
	 */
	var _index = 0;  
   	   	
	function _close(e) {
		if (_isEscapeKey(e.keyCode)) {
			pub.close();
		}
	}
	
	/**
	 * Checks whether the key pressed is the escape key
	 * @param Int keycode
	 * @return Boolean
	 */
	function _isEscapeKey(keycode) {
		return (keycode == 27) ? true : false;
	}
	
	/**
	 * Adds event handlers
	 */
	function _init() {
		$('#closewindow').click(pub.close);
		$(document).click(pub.close);
		// this is to stop the click event bubbling up to the document
		$('#artwindow-content').click(function(e) { e.stopPropagation(); });

		$('a', '#thumbnails-placeholder').click(function(e) {
		  var url = $(this).attr('href');
  		
		  var hash = location.hash;
  	      var parts = hash.split('/');
      
        if (hash == '' || parts[1] != 'phones') {
		    var hash = '#/phones/' + _selectedPhone + '/features/' + url;       
        }
        else {
		    var hash = '#/' + parts[1] + '/' + parts[2] + '/features/' + url;       
		  }

  		hash = hash.replace(/^.*#/, '');
  		
  		$.history.load(hash);
  		
  		return false;
		});
		$(document).keypress(_close);
	}
	
	/**
	 * Unbinds event handlers
	 */
	function _shutDown() {
		$('#closewindow').unbind('click', pub.close);
		$(document).unbind('click', pub.close);
		$('#leftarrow').unbind('click', _open);
		$('#rightarrow').unbind('click', _open);
		$('a', '#thumbnails-placeholder').unbind('click', _open);
		$(document).unbind('keypress', _close);
	}
	
	/**
	 * Loads a url into the window
	 * @param Event
	 * @return false
	 */
	function _open(e) {
		var url = $(this).attr('href');
		pub.open(url);
		
		return false;
	}
	
	// Public API
	var pub = {};
	
	pub.open = function(url) {
		var el = $('#artwindow');
		var init = _init; // store reference to the private _init fn
				           				
		$("#artwindow-content").load(url, {}, function() {
			if (el.css('display') != 'block') {
		    	el.css('display', 'block');

		    	document.getElementById("artwindow-content").style.display = 'block';    
		  	}
		  	
		  	_api = $('.carousel', '#art').scrollable({
        		size: 1,
        		api: true,
        		clickable: false
        	});         
        	          
        	_index = 0;
        	// Load relevant page                         
            $.each($('.artwork'), function(index, value) {     
                if ($(value).attr('rel') == url && $(window).scrollTop() == 0) {    
					$.scrollTo('#art',
							{
						duration: 1000
							}
					);
					        
                   _api.seekTo(_index);                
                } 
                else if($(value).attr('rel') == url) {
                    _api.seekTo(_index);                
                }    
                
                _index++;
            });        	
        	
    		// Deep-linking Art Next click
    		$('.next', '#art').click(function() {    			
    			var index = _api.getIndex();      
    			    			
    			if (index == 2) index = 0;
    			if (index == 3) index = 1;  		

    			var tag = $('.artwork').get(index);    		
    			var url = $(tag).attr('rel');      
    			
            	var label = $('img', tag).get(0);
            	var label = $(label).attr('alt');
            	
                TrackEvent.addLabel({action: 'art', label: label});

    			location.hash = '/inq/art/' + url;	
    		});    

    		// Deep-linking Art Prev click
    		$('.prev', '#art').click(function() {
    			var index = _api.getIndex();

    			if (index == 2) index = 0;
    			if (index == 3) index = 1;  		

    			var tag = $('.artwork').get(index);    		
    			var url = $(tag).attr('rel');     
    			
            	var label = $('img', tag).get(0);
            	var label = $(label).attr('alt');

            	TrackEvent.addLabel({action: 'art', label: label});    			

    			location.hash = '/inq/art/' + url;
    		});
		
			init();
		});
	};
		
	pub.close  = function() {
		document.getElementById("artwindow-content").style.display = 'none';  
		document.getElementById("artwindow").style.display = 'none';

		_shutDown();
	};
	
	return pub;
}();