/**
 * @author allenchow
 */

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);

function getBaseUrl(_url){
	var _baseUrlPath = _url;
};

$(document).ready(function(){

	$.ajax({
		type: "GET",
		url: "/content/sellmarykay/profiles/xml/profiles.xml",
		dataType: "xml",
		success: function(xml){
			$(xml).find('profile').each(function(){
				
				var catagory = $(this).find('catagory').text();
				var featured = $(this).find('featured').text();
				
				var consultant_name = $(this).find('name').text();
				var title_name = $(this).find('title').text();
				var thumb_location = "/content/sellmarykay/profiles/images/prev_thumbs/" + $(this).find('thumb').text();
				var profile_url = _baseUrlPath + $(this).find('story').text();
				var prev_url = $(this).find('preview').text();
				
				var _consultant_name = "'" + consultant_name + "'";
				var _title_name = "'" + title_name + "'";
				var _profile_url = "'" + profile_url + "'";
				var _prev_url = "'" + prev_url + "'";
				
				if(prev_url == ""){
					noVideo(catagory,featured,consultant_name,title_name,thumb_location,profile_url,prev_url,_consultant_name,_title_name,_profile_url,_prev_url);
				}else{
					hasVideo(catagory,featured,consultant_name,title_name,thumb_location,profile_url,prev_url,_consultant_name,_title_name,_profile_url,_prev_url);
				};
				
			})
			$('.profiles div:nth-child(3n) p').addClass("last");
			s.tl(this,'o','See All - On Load');
		}
	});
});

function hasVideo(catagory,featured,consultant_name,title_name,thumb_location,profile_url,prev_url,_consultant_name,_title_name,_profile_url,_prev_url){
	$('<div></div>')
		.html(
			'<p>' + 
			'<img src="' + thumb_location + '" />' + 
			'<br />' + 
			consultant_name + 
			'<br />' + 
			'"' + title_name + 
			'"<br />' +
			'<a href="' + profile_url + '">Read my story</a>' + 
			'<br />' +
			'<a href="javascript:playPreviewVideo(' + _consultant_name + "," + _title_name + "," + _profile_url + "," + _prev_url + ');">Watch my video</a>' +
			'</p>'
			)
			.appendTo('.all');
		$('.all').shuffle();
				
	if(featured == "true"){
		$('<div></div>')
			.html(
			'<p>' +
			'<img src="' + thumb_location + '" />' + 
			'<br />' + 
			consultant_name + 
			'<br />' + 
			'"' + title_name + 
			'"<br />' + 
			'<a href="' + profile_url + '">Read my story</a>' + 
			'<br />' +
			'<a href="javascript:playPreviewVideo(' + _consultant_name + "," + _title_name + "," + _profile_url + "," + _prev_url + ');">Watch my video</a>' +
			'</p>'
			)
			.appendTo('.featured');
					
		$('.featured').shuffle();
	};
				
	if(catagory == "money"){
		$('<div></div>')
			.html(
				'<p>' +
				'<img src="' + thumb_location + '" />' + 
				'<br />' + 
				consultant_name + 
				'<br />' + 
				'"' + title_name + 
				'"<br />' +
				'<a href="' + profile_url + '">Read my story</a>' + 
				'<br />' +
				'<a href="javascript:playPreviewVideo(' + _consultant_name + "," + _title_name + "," + _profile_url + "," + _prev_url + ');">Watch my video</a>' +
				'</p>'
				)
				.appendTo('.money');
				
		$('.money').shuffle();
	}else if(catagory == "difference"){
		$('<div></div>')
			.html(
			'<p>' +
			'<img src="' + thumb_location + '" />' + 
			'<br />' + 
			consultant_name + 
			'<br />' + 
			'"' + title_name + 
			'"<br />' +
			'<a href="' + profile_url + '">Read my story</a>' + 
			'<br />' +
			'<a href="javascript:playPreviewVideo(' + _consultant_name + "," + _title_name + "," + _profile_url + "," + _prev_url + ');">Watch my video</a>' +
			'</p>'
			)
			.appendTo('.difference');
					
		$('.difference').shuffle();
	}else if(catagory == "independence"){
		$('<div></div>')
			.html(
			'<p>' +
			'<img src="' + thumb_location + '" />' + 
			'<br />' + 
			consultant_name + 
			'<br />' + 
			'"' + title_name + '"<br />' +
			'<a href="' + profile_url + '">Read my story</a>' + 
			'<br />' +
			'<a href="javascript:playPreviewVideo(' + _consultant_name + "," + _title_name + "," + _profile_url + "," + _prev_url + ');">Watch my video</a>' +
			'</p>'
			)
			.appendTo('.independence');
				
		$('.independence').shuffle();
	};
};

function noVideo(catagory,featured,consultant_name,title_name,thumb_location,profile_url,prev_url,_consultant_name,_title_name,_profile_url,_prev_url){
	$('<div></div>')
		.html(
			'<p>' + 
			'<img src="' + thumb_location + '" />' + 
			'<br />' + 
			consultant_name + 
			'<br />' + 
			'"' + title_name + 
			'"<br />' +
			'<a href="' + profile_url + '">Read my story</a>' + 
			'</p>'
			)
			.appendTo('.all');
		$('.all').shuffle();
				
	if(featured == "true"){
		$('<div></div>')
			.html(
			'<p>' +
			'<img src="' + thumb_location + '" />' + 
			'<br />' + 
			consultant_name + 
			'<br />' + 
			'"' + title_name + 
			'"<br />' + 
			'<a href="' + profile_url + '">Read my story</a>' + 
			'</p>'
			)
			.appendTo('.featured');
					
		$('.featured').shuffle();
	};
				
	if(catagory == "money"){
		$('<div></div>')
			.html(
				'<p>' +
				'<img src="' + thumb_location + '" />' + 
				'<br />' + 
				consultant_name + 
				'<br />' + 
				'"' + title_name + 
				'"<br />' +
				'<a href="' + profile_url + '">Read my story</a>' + 
				'</p>'
				)
				.appendTo('.money');
				
		$('.money').shuffle();
	}else if(catagory == "difference"){
		$('<div></div>')
			.html(
			'<p>' +
			'<img src="' + thumb_location + '" />' + 
			'<br />' + 
			consultant_name + 
			'<br />' + 
			'"' + title_name + 
			'"<br />' +
			'<a href="' + profile_url + '">Read my story</a>' + 
			'</p>'
			)
			.appendTo('.difference');
					
		$('.difference').shuffle();
	}else if(catagory == "independence"){
		$('<div></div>')
			.html(
			'<p>' +
			'<img src="' + thumb_location + '" />' + 
			'<br />' + 
			consultant_name + 
			'<br />' + 
			'"' + title_name + '"<br />' +
			'<a href="' + profile_url + '">Read my story</a>' + 
			'</p>'
			)
			.appendTo('.independence');
				
		$('.independence').shuffle();
	};
};
