/**
 * @author nipahn
 * jquery functions for shopping.sky.com
 */

//to avoid conflict with prototype
jQuery.noConflict();

/*
 * Sets the search dropdown list
 */
var setSearchDropList = function ( urlSegment ) {
	urlSegment = urlSegment.toLowerCase();
	jQuery("#categorySearchSelect ul.categoryOptions li a").each(function (i) {	
		tempSegemt = String(jQuery(this).attr('href')).toLowerCase();
		if ( tempSegemt.indexOf(urlSegment) > 0 ) {
			//jQuery(this).attr("selected","selected");
			jQuery("#categorySearchSelect #displayCategoryHolder span").html( jQuery(this).text() );		
			jQuery("#categorySearchSelect ul.categoryOptions li a").removeClass("selected");
			jQuery(this).addClass("selected");
		
		}
	});
}

/*
 * category landing page function to rebuild list
 */
 var rebuildHTML = function(){
	
	if(jQuery('ul').hasClass('categoriesUL')){
		
		var count = 1;
		var ix = jQuery('.list > li').size();
		
		var code = '<ul>';
		
		jQuery('.list > li').each(function(i){
		
			
			code +=  '<li>' + jQuery(this).html() + '</li>' ;
			
			if (count % 4 == 0) {
	
				code += '</ul>';
				
				if(count != ix){
					
					code += '<ul>';						
				}
				
			}
			
			count++;
			
		});
		
		code += '</ul>';
		
		jQuery('#formattedList').html(code);
		
	}
	
}

/*
 * price finder functions ajax functionality to rebuild price finder
 */

var updateListeners = function(){
				
	jQuery('.priceFinderLink').bind('click', function(e){
		
		catId = jQuery(this).attr("id").replace(/catTab-/, '');

		catUpdate(catId);
		
		return false;
		
	})
	
	jQuery('.priceRangeLink').bind('click', function(e){
		
		range = jQuery(this).attr("id").split('_');
		
		priceUpdate(range[0], range[1], range[2]);
		
		return false;
		
	})
	
}

var catUpdate = function(catId){
	
	jQuery.post('/inc/php/price_finder/price_finder.php', {func: 'catUpdate', catId: parseInt(catId)}, function(data){
	//jQuery.post('/tests/pf.php', {func: 'catUpdate', catId: parseInt(catId)}, function(data){
			
		jQuery('#priceRange-widget').html(data);

		updateListeners();
		
		return false;
		
	});
	
}

var priceUpdate = function(catId, pFrom, pTo){
	
	jQuery.post('/inc/php/price_finder/price_finder.php', {func : 'priceUpdate', catId : parseInt(catId), from : pFrom, to : pTo}, function(data){
	//jQuery.post('/tests/pf.php', {func : 'priceUpdate', catId : parseInt(catId), from : pFrom, to : pTo}, function(data){	
			
		jQuery('#priceRange-widget').html(data);

		updateListeners();
		
		return false;
		
	});
	
	
}

jQuery(document).ready(function(){
	
	//category landing page
	if(jQuery('ul').hasClass('categoriesUL')){
		
		var count = 1;
		var ix = jQuery('.list > li').size();
		
		var code = '<ul class="categoriesUL">';
		
		jQuery('.categoriesUL > li').each(function(i){
		
			if (count % 4 == 0) {

				code +=  '<li class="categoryWrapper last">' + jQuery(this).html() + '</li>' ;

			}else{

				code +=  '<li class="categoryWrapper">' + jQuery(this).html() + '</li>' ;
			}
			
			if (count % 4 == 0) {
	
				code += '</ul>';
				
				if(count != ix){
					
					code += '<ul class="categoriesUL">';						
				}
				
			}
			
			count++;
			
		});
		
		code += '</ul>';
		
		jQuery('#formattedList').html(code);
		jQuery('#orginalUL').hide();
		
	}
	
	//price finder
	
	if((jQuery('a').hasClass('priceFinderLink')) && (jQuery('a').hasClass('priceRangeLink'))){
		
		jQuery('.priceFinderLink').click(function(){
					
			catId = jQuery(this).attr("id").replace(/catTab-/, '');
			
			catUpdate(catId);
			
			return false;
				
		});
		
		jQuery('.priceRangeLink').click(function(){
			
			range = jQuery(this).attr("id").split('_');
			
			priceUpdate(range[0], range[1], range[2]);
			
			return false;
			
		})
		
	}
	
	if (jQuery('ul').hasClass('directoryLayout')) {
			
		var count = 1;
		var ix = jQuery('.directoryLayout > li').size();
		
		var code = '<div class="directoryColumn">';
		
		jQuery('.directoryLayout > li').each(function(i){
			
			code += '<div class="dirItem">'+jQuery(this).html()+'</div>';

			if ( ((count % 3)== 0) && (count != ix) ) {
	
				code += '</div><div class="directoryColumn">';
				
			}
			
			count++;
			
		});
		
		//output
		jQuery('#jsDisplay').html(code);
		
		jQuery('ul.directoryLayout').hide();
		
	}
	
	
})
