/* functions */

function view(viewname) {
	
	if (viewname != '' || viewname != null) 	
	$.get(('views/'+viewname+'.html'), function(result){ 
			$('.collection').hide();
		  	$('div.content').html(result); 
			$('div.content').show();
		});
}

function start() {
	
	/* start ups */
		
	view('home');	
	
	/* bindings */
	
	$('.nav li').css('cursor', 'pointer');
	
	$('.nav li').click(
		function(e) {
			view($(this).attr('id'));
		}
	);	
	
	$('.nav li').hover(
		function(e) {
			$(this).css('text-decoration', 'underline');
		},
		function(e) {
			$(this).css('text-decoration', 'none');
		}
	);
}

$(document).ready(function(){
	start();
});

/*	catalog related js	*/

// function declarations
function SetupCategoryClick() {
	$('.catalog li').click(function(e) {
		var id = $(this).attr('id');
		var name = $(this).attr('category');
		FetchProducts(id, name, '');
	});
}

function SetupProductClick() {
	$('.catalog li').click(function(e) {
		var id = $(this).attr('id');
		FetchProduct(id);
	});
}

function refreshcatalogs() {
	
	$('.display').hide();
	$('.back').hide();
	$('.categoryheader h2').hide();	 
	$('.categoryheader h2').attr('category', '');
	$('#findit').attr('value', ('Search in all categories'));
	
	$.get('proxy.php', { url:'http://simple-ocean-30.heroku.com/categories.xml' }, 
	function(data) 
		{			
			var lis = ""
			var list = "<ul>__li__</ul>";
			var li = "<li id='__id__' category='__cate__'><h3>__name__</h3><img src='__img__'></img></li>";
			
			$('category', data).each(
				function(i) {
					var name = $(this).find("name").text();						
					var id = $(this).find("id").text();	
					var file = $(data).find("image-file-name").text();					
					var cnt = $(data).find("image-content-type").text();					
					
					img = 'http://s3.amazonaws.com/vapco/categories/'+ id +'.jpg';
					
					if (file == null)
						img = 'http://s3.amazonaws.com/vapco/categories/missing.png';
					
					lis += li
							.replace(/__name__/g, name)							
							.replace(/__cate__/g, name)							
							.replace(/__img__/g, img)							
							.replace(/__id__/g, id);							
				}
			);

			list = list.replace(/__li__/g, lis);							
			$(".catalog").html(list); 

			SetupCategoryClick();
		},
	'xml'
	 );
}

function FetchProducts(category, categoryname, filter) {
	
	$('.back').show();
	$('.categoryheader h2').html(categoryname);
	$('.categoryheader h2').show();
	$('.categoryheader h2').attr('category', category);
	
	var path = 'http://simple-ocean-30.heroku.com/products.xml?cate='+category+'&query='+filter;
	
	$.get('proxy.php', { url:path }, 
	function(data) 
		{ 
			var lis = ""
			var list = "<ul>__li__</ul>";
			var li = "<li id='__id__'><h3>__name__</h3><img src='__img__'></img></li>";
			
			$('product', data).each(
				function(i) {
					
					var name = '';
					var id = '';
					var file = '';
					var img = "http://s3.amazonaws.com/vapco/products/__id__.jpg";
					
					name = $(this).find("name").text();						
					id = $(this).find("id").text();	
					file = $(this).find("image-file-name").text();
					
					if (file == '')
						img = 'http://s3.amazonaws.com/vapco/products/missing.png';
					
					lis += li
							.replace(/__name__/g, name)							
							.replace(/__img__/g, img)							
							.replace(/__id__/g, id);
				}
			);

			list = list
				.replace(/__cateid__/g, category)
				.replace(/__li__/g, lis);							
			$(".catalog").html(list);
			
			SetupProductClick();
		},
	'xml'
	 );
}

function FetchProduct(product) {
	
	$.get('proxy.php', { url:'http://simple-ocean-30.heroku.com/products/'+product+'.xml' }, 
	function(data) 
		{ 
			var lis = ""
			var list = "<ul>__li__</ul>" + "<span class='close'>Close</span>";
			var li = "<li id='__id__'><h2>__name__</h2><img src='__img__'></img><span class='pdf' style='__pdfstyle__'>MSDS:&nbsp;<a href='__msds__'>Click to download</a></span><textarea readonly='true' class='copy'>__copy__</textarea></li>";
			var img = "http://s3.amazonaws.com/vapco/products/"+ product +".jpg";
			
			var name = $(data).find("name").text();						
			var copy = $(data).find("copy").text();	
			var file = $(data).find("image-file-name").text();				
			var pdf = $(data).find("msds-file-name").text();				
			var id = $(data).find("id").text();	
			
			if (file == '')
				img = 'http://s3.amazonaws.com/vapco/products/missing.png';					

			var pdfstyle = ''
			if (pdf == '')
				pdfstyle = "display:none;"
			else 
				pdf = "http://s3.amazonaws.com/vapco/products/"+ id +".pdf";
			
			lis += li
					.replace(/__name__/g, name)							
					.replace(/__copy__/g, copy)							
					.replace(/__img__/g, img)							
					.replace(/__msds__/g, pdf)							
					.replace(/__pdfstyle__/g, pdfstyle)							
					.replace(/__id__/g, id);

			list = list.replace(/__li__/g, lis)								
			$(".display").html(list);
			$.blockUI({ 
				message: $('.display'), 
				css: { position: 'absolute', left: '2em', top:'2em', border: 'none', height: '0' }, 
				overlayCSS: { cursor: 'default' }
			}); 
			
			$('.close').click(function(){ $.unblockUI(); })
			$('.close').focus();
			$('textarea').live('focus', function() { $(this).blur(); })
		},
	'xml'
	 );
}

function onBefore() { 
	
	var _h3 = $(this).attr('alt');
	var _p = $(this).attr('copy');
	
	if (_h3 == undefined)
		_h3 = $('.imagewrapper img').attr('alt');
	if (_p == undefined)
		_p = $('.imagewrapper img').attr('copy');
	
	//<textarea name="copy" rows="8" cols="40"></textarea>		
	var h3 = '<h3>' + _h3 + '</h3>';
	var p = '<textarea name="copy" readonly="true">' + _p + '</textarea>';
	
	$('.featuredCopy').html((h3 + p)); 
}

function FetchFeatured() {

	var path = 'http://simple-ocean-30.heroku.com/products.xml?featured=1';	

	$.get('proxy.php', { url:path }, 
	function(data) 
	{ 
		var lis = "";
		var list = "__li__";
		$('product', data).each(
			function(i) {	

				var li = "<img prodid='__id__' id='i__id__' src='__img__' alt='__name__' copy='__copy__' />";

				var name = '';
				var id = '';
				var file = '';
				var copy = '';
				var img = "http://s3.amazonaws.com/vapco/products/__id__.jpg";

				name = $(this).find("name").text();						
				id = $(this).find("id").text();	
				file = $(this).find("image-file-name").text();
				copy = $(this).find("copy").text();

				if (file != '')
				lis += li
					.replace(/__name__/g, name)							
					.replace(/__img__/g, img)							
					.replace(/__copy__/g, copy)							
					.replace(/__id__/g, id);	
			}
		);

		list = list.replace(/__li__/g, lis);
		
		$('.imagewrapper').html(list);					
		$('.imagewrapper').cycle({ 
		    fx:     'fade', 
		    timeout: 5000,
			before: onBefore
		 });
		
		onBefore();
		SetupProductClick();
	},
	'xml');
	
	function SetupProductClick() {
		$('.imagewrapper img').click(function(e) {
			var id = $(this).attr('prodid');
			FetchProduct(id);
		});
	}
}

function FetchAboutUs() {
	$.get('proxy.php', { url:'http://simple-ocean-30.heroku.com/sitecopies/1.xml' }, 
	function(data) 
		{ 
			var copy = $(data).find("aboutus").text();
			$(".aboutus .copy").html(copy);
		},
	'xml'
	 );
}
function FetchDealers() {
	$.get('proxy.php', { url:'http://simple-ocean-30.heroku.com/sitecopies/1.xml' }, 
	function(data) 
		{ 
			var copy = $(data).find("localdealers").text();
			$(".dealers .copy").html(copy);
		},
	'xml'
	 );
}
