
var xmlFileNameHolder;
var disciplineName;
var tFlag = 1;
var tFlag2 = 0;
var tSender;
var tIsbn;
var tPrev="";
var prevDisc="";
var brandId;
$(document).ready(function() {
	$.ajax({
		type: "GET",
		url: "/classwareweb/subjectsXML/brandingXMLMapping.xml",
		dataType: "xml",
		success: function(xml) {
			var browserURL=window.location.href;
			browserURL = browserURL.substr(browserURL.indexOf("en_US/")+6);
			brandId = browserURL.split("/")[0];
			xmlFileNameHolder=$(xml).find("brands brand[brandId='"+brandId+"']").text().split("|");
			disciplineName=$(xml).find("brands brand[brandId='"+brandId+"']").attr("name");
			getBooks();
		}
	});
});

function getBooks() {
	var isbn = "";
	for(var i=0;i<xmlFileNameHolder.length;i++) {
	$.ajax({
		type: "GET",
		url: "/classwareweb/subjectsXML/"+xmlFileNameHolder[i],
		dataType: "xml",
		success: function(xml) {
		var html='<div class="booksContainer" style="display: none;"><div>';
		if( $(xml).find("imprint discipline subdiscipline[brandId='"+brandId+"']").length > 0 )	{
			$(xml).find("imprint discipline subdiscipline[brandId='"+brandId+"'] book").each(function(){
					if(isbn.indexOf($(this).attr('isbn') + ",") < 0) {
						var bookTitle = $(this).attr('title');
						var bookEdition = $(this).attr('edition');
						var authorName = $(this).find('author:first').attr('name');
						html+=$("#bookStructureId").html();
						while(html.indexOf("##BOOK_ISBN##")!=-1)
							html=html.replace("##BOOK_ISBN##",$(this).attr('isbn'));
						while(html.indexOf("##BOOK_TITLE##")!=-1)
							html=html.replace("##BOOK_TITLE##",authorName+' - '+bookTitle+' '+bookEdition+'e');
						var disName=$(this).parent().parent().parent().parent().attr('name');
						html=html.replace("##IMPRINT_NAME##", disName);
						while(html.indexOf("##DISC_NAME##")!=-1)
							html=html.replace("##DISC_NAME##",disName.replace(/\s+/g,''));
						isbn = isbn + $(this).attr('isbn') + ",";
					}
			});
		}
		//One Imprint mathematics has brandid mathematics for its subdiscipline and discipline both so not putting this in an else block.
		$(xml).find("imprint discipline[brandId='"+brandId+"'] book").each(function(){
			var disName=$(this).parent().parent().parent().attr('name');
			if (disName != null) {
					if(isbn.indexOf($(this).attr('isbn') + ",") < 0) {
						var bookTitle = $(this).attr('title');
						var bookEdition = $(this).attr('edition');
						var authorName = $(this).find('author:first').attr('name');
						html+=$("#bookStructureId").html();
						while(html.indexOf("##BOOK_ISBN##")!=-1)
							html=html.replace("##BOOK_ISBN##",$(this).attr('isbn'));
						while(html.indexOf("##BOOK_TITLE##")!=-1)
							html=html.replace("##BOOK_TITLE##",authorName+' - '+bookTitle+' '+bookEdition+'e');
						//One brandid management is having same brandid for its subdiscipline also so putting it in the if block.
						//var disName=$(this).parent().parent().parent().attr('name');
						html=html.replace("##IMPRINT_NAME##", disName);
						while(html.indexOf("##DISC_NAME##")!=-1)
							html=html.replace("##DISC_NAME##",disName.replace(/\s+/g,''));
						isbn = isbn + $(this).attr('isbn') + ",";
					}
				}
			});
		html+="</div></div>";
		if($(".booksContainer").length==0) {
			$(html).appendTo("#innerDisciplineContainer");
			$(".booksContainer").show("");
		} else {
			$(html).appendTo("#innerDisciplineContainer");
			$(".booksContainer").show("");
		}
		}
	});
	}
}

function rollMe(sender) {
	$(sender).css("background-color", "#ebeaea");
}
function unrollMe(sender) {
	$(sender).css("background-color", "#ffffff");
}  
/*This method accepts parameters in form of key:value pair seperated by a comma(,)
	example - getTitle({key1:value1,key2:value2,..})
*/
function getTitle(options) {
        tSender = options.sender;
        tIsbn = options.isbn;
        disciplineName = options.discName;
        if (tFlag == 1) {
			if( tPrev == tIsbn && prevDisc == disciplineName) {
            	$("#bookDn" + prevDisc.replace(/\s+/g,'') + tPrev).hide();
            	$("#book" + prevDisc.replace(/\s+/g,'') + tPrev).show();
            	$(".bookItems").slideUp("slow", closeTitle);
           	 	tFlag = 0;
            	discFlag = 1;
			} else {	
				$("#bookDn" + prevDisc.replace(/\s+/g,'') + tPrev).hide();
				$("#book" + prevDisc.replace(/\s+/g,'') + tPrev).show();
				discFlag = 1;
				getBookInfo();
			}
        } else if (tFlag == 0) {
        	discFlag = 1;
			getBookInfo();
        }
}
function getBookInfo() {
	for(var i=0;i<xmlFileNameHolder.length;i++) {
	$.ajax({
		type: "GET",
		url: "/classwareweb/subjectsXML/"+xmlFileNameHolder[i],
		dataType: "xml",
		success: function(xml) {
			var html='<div class="bookItems" style="display: none;"><div>';
			$(xml).find("imprint[name='"+disciplineName+"'] discipline subdiscipline[brandId='"+brandId+"'] book[isbn='"+tIsbn+"']").each(function(){
				var disName=$(this).parent().parent().parent().parent().attr('name');
				if (disName != null) {
						html+=$("#bookItemStructureId").html();
						while(html.indexOf("##BOOK_ISBN##")!=-1)
							html=html.replace("##BOOK_ISBN##",$(this).attr('isbn'));
						html=html.replace("##IMG_URL##",$(this).attr('imageURL'));
						html=html.replace("##BOOK_TITLE##",$(this).attr('title'));
						html=html.replace("##BOOK_AUTHORS##",$(this).find('author:first').text());
						html=html.replace("##BOOK_EDITION##",$(this).attr('edition'));
						html=html.replace("##PUBLISHED_DATE##",$(this).attr("publisheddate"));
						html=html.replace("##BOOK_DESC##",$(this).find('longdesc').text());
					}
			});
			$(xml).find("imprint[name='"+disciplineName+"'] discipline[brandId='"+brandId+"'] book[isbn='"+tIsbn+"']").each(function(){
				var disName=$(this).parent().parent().parent().attr('name');
				if (disName != null) {
						html+=$("#bookItemStructureId").html();
						while(html.indexOf("##BOOK_ISBN##")!=-1)
							html=html.replace("##BOOK_ISBN##",$(this).attr('isbn'));
						html=html.replace("##IMG_URL##",$(this).attr('imageURL'));
						html=html.replace("##BOOK_TITLE##",$(this).attr('title'));
						html=html.replace("##BOOK_AUTHORS##",$(this).find('author:first').text());
						html=html.replace("##BOOK_EDITION##",$(this).attr('edition'));
						html=html.replace("##PUBLISHED_DATE##",$(this).attr("publisheddate"));
						html=html.replace("##BOOK_DESC##",$(this).find('longdesc').text());
					}
			});
			html+="</div></div>";
			if($(html).find(".item").length>0){
			if($(".bookItems").length==0) {
				$(html).appendTo(tSender);
				$(".bookItems").slideToggle("slow");
				$("#book" + disciplineName.replace(/\s+/g,'') + tIsbn).hide();
				$("#bookDn" + disciplineName.replace(/\s+/g,'') + tIsbn).show();
				tPrev = tIsbn;
				prevDisc = disciplineName;
				tFlag = 1;
			} else {
				$(".bookItems").slideUp("slow",function() {
					 closeTitle();
					$(html).appendTo(tSender); 
					$(".bookItems").slideToggle("slow");
					$("#book" + disciplineName.replace(/\s+/g,'') + tIsbn).hide();
					$("#bookDn" + disciplineName.replace(/\s+/g,'') + tIsbn).show();
					discFlag = 1;
					tFlag = 1;
				});
			}
			}
		}
	});
	}
}

function clearTitle() {
    $("div[class^=bookItems]").remove();
    setTimeout(try2, 5000);
}

function closeTitle() {
    $("div[class^=bookItems]").remove();
    $("#bookDn" + disciplineName.replace(/\s+/g,'') + tIsbn).hide();
    $("#book" + disciplineName.replace(/\s+/g,'') + tIsbn).show();
    tPrev = tIsbn;
    prevDisc = disciplineName;
    discFlag = 0;
    tFlag = 0; 
}
