jQuery.noConflict();

var BP = {}; 

BP.namespace = function(name)
{ 
    var parts = name.split('.'); 
    var current = BP; 
    for (var i in parts)
    { 
        if (!current[parts[i]])
        { 
             current[parts[i]] = {}; 
        } 
        current = current[parts[i]]; 
    } 
} 

BP.namespace('fm');
BP.namespace('fetch');
BP.namespace('data.stores');
BP.namespace('util');

BP.data.stores.pages = {'home':'index.html','finder':'finder.html','product':'product.html'};
BP.data.productImagePath = 'bespoke-public-product-images.s3.amazonaws.com/';

BP.fetch.getCategories = function()
{
	jQuery.ajax(
    {
        type: "GET",
        async: false,
        url: "/catalog/products/categories",
        dataType: "json",
        timeout: 4000,
        success: BP.data.storeCategories,
        error: BP.data.errorHandler
    });
}

BP.fetch.getHomeProducts = function(successCallback)
{
	jQuery.ajax(
    {
        type: "GET",
        async: true,
        url: "/catalog/products/",
        data: {'max_results':6, 'sort_key':'random', 'image_width':220, 'image_quantity':'single'},
        dataType: "json",
        timeout: 4000,
        success: function(data, textStatus) {
        	BP.data.storeProducts(data, textStatus);
        	if(successCallback) {
        		successCallback();
        	}
        },
        error: BP.data.errorHandler
    });
}

BP.fetch.getRecentProducts = function(successCallback)
{
	jQuery.ajax(
    {
        type: "GET",
        async: true,
        url: "/catalog/products/",
        data: "sort_key=added&sort_direction=down&max_results=100&image_width=175&image_quantity=single",
        dataType: "json",
        timeout: 4000,
        success: function(data, textStatus) {
        	BP.data.storeProducts(data, textStatus);
        	if(successCallback) {
        		successCallback();
        	}
        },
        error: BP.data.errorHandler
    });
}

BP.fetch.getFinderProducts = function(filters, successCallback)
{
	jQuery.ajax(
    {
        type: "GET",
        async: true,
        url: "/catalog/products/",
        data: filters,
        dataType: "json",
        timeout: 4000,
        success: function(data, textStatus) {
        	BP.data.storeProducts(data, textStatus);
        	if(successCallback) {
        		successCallback();
        	}
        },
        error: BP.data.errorHandler
    });
}

BP.fetch.getCategoryProducts = function(filters, successCallback)
{
	jQuery.ajax(
    {
        type: "GET",
        async: true,
        url: "/catalog/products/category",
        data: filters,
        dataType: "json",
        timeout: 4000,
        success: function(data, textStatus) {
        	BP.data.storeProducts(data, textStatus);
        	if(successCallback) {
        		successCallback();
        	}
        },
        error: BP.data.errorHandler
    });
}

BP.fetch.getManufacturerProducts = function(filters, successCallback)
{
	jQuery.ajax(
    {
        type: "GET",
        async: true,
        url: "/catalog/products/product-manufacturer",
        data: filters,
        dataType: "json",
        timeout: 4000,
        success: function(data, textStatus) {
        	BP.data.storeProducts(data, textStatus);
        	if(successCallback) {
        		successCallback();
        	}
        },
        error: BP.data.errorHandler
    });
}

BP.fetch.getProduct = function(product_id, filters, successCallback)
{
	jQuery.ajax(
    {
        type: "GET",
        async: true,
        url: "/catalog/products/" + product_id,
        data: filters,
        dataType: "json",
        timeout: 1500,
        success: function(data, textStatus) {
        	BP.data.storeProduct(data, textStatus);
        	if(successCallback) {
        		successCallback();
        	}
        },
        error: BP.data.errorHandler
    });
}

BP.data.storeCategories = function(data, textStatus)
{
    if(textStatus === "success") 
    {
        BP.data.stores.categoriesStore = data.categories;
    }
}

BP.data.storeProducts = function(data, textStatus)
{
    if(textStatus === "success") 
    {
        BP.data.stores.productsStore = data.products;
    	if(data.product_images)
    	{
	    	BP.data.stores.productImagesStore = data.product_images;
    	}	
    }	
}

BP.data.storeProduct = function(data, textStatus)
{
    if(textStatus === "success") 
    {
        BP.data.stores.productStore = data.product;
    	if(data.product_images)
    	{
	    	BP.data.stores.productImagesStore = data.product_images;
    	}
    	if(data.product_main_image)
    	{
    		BP.data.stores.productMainImagesStore = data.product_main_image;
    	}
    }	
}

BP.data.errorHandler = function(XMLHttpRequest, textStatus, errorThrown)
{
    // log error
    var logInfo = {requestUrl:this.url, currentPage:window.location.href, errorMsg:errorThrown};
    jQuery.post('/error/log', logInfo);
    // redirect
    window.location.href = '/404.html';
}

BP.getCategoryInfoFromStore = function(cat_id)
{	
	for(id in BP.data.stores.categoriesStore)
	{
		var category = BP.data.stores.categoriesStore[id];
		if(parseInt(category.id) == parseInt(cat_id))
		{
			return category;
			break;
		}
		
	}
}

BP.getCategoryInfoFromStoreByWebname = function(webname)
{	
	for(index in BP.data.stores.categoriesStore)
	{
		var category = BP.data.stores.categoriesStore[index];
		if(category.web_name == webname)
		{
			return category;
			break;
		}
		
	}
}