﻿var aowtime;
var female=true;

function articlesofweek() {
	$.getJSON("/rarticlesofweekforman", function(artfm) {
		$.getJSON("/rarticlesofweekforwoman", function(artfw) {
			$.getJSON("/rarticlescategories", function(cate) {
				for(var a = 0; a<artfm.length; a++){
					for(var b = 0; b<cate.length; b++){
						if (artfm[a].fields.category == cate[b].pk)
						{
							artfm[a].fields.category = cate[b].fields.name;
							artfm[a].fields.categoryslug = cate[b].fields.slug;
						};
					};
				};
				for(var a = 0; a<artfw.length; a++){
					for(var b = 0; b<cate.length; b++){
						if (artfw[a].fields.category == cate[b].pk)
						{
							artfw[a].fields.category = cate[b].fields.name;
							artfw[a].fields.categoryslug = cate[b].fields.slug;
						};
					};
				};
		
				$("a.male").click(function () {
					aowstop();
					female=false;
					aowload();
					aowstart();
				});
				$("a.female").click(function () {
					aowstop();
					female=true;
					aowload();
					aowstart();
				});
				
				$("a.male").hover(function () {
					$(this).css("cursor","pointer");
				},function () {
					$(this).css("cursor","default");
				});
				$("a.female").hover(function () {
					$(this).css("cursor","pointer");
				},function () {
					$(this).css("cursor","default");
				});
				
				aowload();
				aowstart();

				function aowstart(){
					aowtime = setInterval(function() { aowrefresh(); }, 10000); 
				};

				function aowstop(){
					clearInterval(aowtime); 
				};

				
				function aowload() {
					var ai=0;
					if (female==false){
						$("a.male").css("color","#363636");
						$("a.female").css("color","#969696");
						$("div.mcontentg.articles img").attr("src","http://www.twojakosmetyczka.vbiz.pl/img/artykuly/" + artfm[ai].fields.image);
						$("div.mcontentg.articles div.links a").remove();
						for (ai; ai<=artfm.length-1; ai++){
							$("div.mcontentg.articles div.links").append("<a id="+ai+" href='/artykul/" + artfm[ai].fields.categoryslug + "/" + artfm[ai].pk + "," + slugify(artfm[ai].fields.title) + ".html'><p><span>" + artfm[ai].fields.title + "</span><br />" + artfm[ai].fields.category + "</p></a>");
						};
						$("div.mcontentg.articles div.links a:first").addClass("actual");
						
						$("div.mcontentg.articles div.links a").hover(
							function () {
								$("div.mcontentg.articles div.links a.actual").removeClass("actual");
								var actual = $(this).attr("id");
								$("div.mcontentg.articles img").attr("src","http://www.twojakosmetyczka.vbiz.pl/img/artykuly/" + artfm[actual].fields.image);
								$(this).addClass("actual");
							}, 
							function () {}
						);
						
						female=true;
					}else{
						$("a.male").css("color","#969696");
						$("a.female").css("color","#363636");
						$("div.mcontentg.articles img").attr("src","http://www.twojakosmetyczka.vbiz.pl/img/artykuly/" + artfw[ai].fields.image);
						$("div.mcontentg.articles div.links a").remove();
						for (ai; ai<=artfw.length-1; ai++){
							$("div.mcontentg.articles div.links").append("<a id="+ai+" href='/artykul/" + artfw[ai].fields.categoryslug + "/" + artfw[ai].pk + "," + slugify(artfw[ai].fields.title) + ".html'><p><span>" + artfw[ai].fields.title + "</span><br />" + artfw[ai].fields.category + "</p></a>");
						};
						$("div.mcontentg.articles div.links a:first").addClass("actual");
						
						$("div.mcontentg.articles div.links a").hover(
							function () {
								$("div.mcontentg.articles div.links a.actual").removeClass("actual");
								var actual = $(this).attr("id");
								$("div.mcontentg.articles img").attr("src","http://www.twojakosmetyczka.vbiz.pl/img/artykuly/" + artfw[actual].fields.image);
								$(this).addClass("actual");
							}, 
							function () {}
						);
						
						female=false;
					};
				}	
				
				function aowrefresh() {
					if ( $("div.mcontentg.articles div.links a.actual").is(":last-child") ){
						aowload();
					}else{
						$("div.mcontentg.articles div.links a.actual").removeClass("actual").next().addClass("actual");
						var actual = $("div.mcontentg.articles div.links a.actual").attr("id");
						if (female==false){
							$("div.mcontentg.articles img").attr("src","http://www.twojakosmetyczka.vbiz.pl/img/artykuly/" + artfw[actual].fields.image);
						}else{
							$("div.mcontentg.articles img").attr("src","http://www.twojakosmetyczka.vbiz.pl/img/artykuly/" + artfm[actual].fields.image);
						}
					}
				};
			});
		});
	});
};
