// FashionDesign Caraousel.js
var asmxRandomDesign = "/API/Fashion/JSWSRandomDesign.asmx/";

var $RandomAjaxOptions = {
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "text",dataFilter: function (data, type) {return $.parseJSON(data);},
    error: function(result) {
        if (this.console && typeof console.log != "undefined")
            console.log("Error in server:" + result.status + ' ' + result.statusText);
    }
};

var getNextResult = false;
var pageNo =1;
var currentImageIndex = 1;
var previouspageNo =0;
var nextpageNo =0;
var currentPageIndex = 0;
var arrayOfElements = new Array();
var addElements = false;
$(document).ready(function(){ 

    $(".mainCarousel").jCarouselLite({
		visible: 1,		
		start: 0,
		speed:700,		
		circular: true,
		btnNext: ".carouselControls .next",
		btnPrev: ".carouselControls .prev",	
		afterEnd: function(a){GetDesigns();}
	});
    //HideShowPreviounsButton();
	AddElementToArray();	
	$(function() {
		$('.carouselContainer').click(function(){
			$('.carouselControls').find('.selected').removeClass('selected');
			$(this).addClass('selected');
		});
		$('a.next').click(function(){					    
			    getNextResult = true;
		});
		$('a.prev').click(function(){		     		        
		     getNextResult = false;		
		});
	});
});

/*function HideShowPreviounsButton() {
    if (pageNo <= 1) {
        $('a.prev').css("display","none");
    }
    else {
         $('a.prev').css("display","inline-block");
    }
}*/


function InitializeVariable() {
    if (getNextResult) {
	pageNo =pageNo +1;
	NextClick();
  }
    else {
        if (pageNo > 0) {
	      pageNo =pageNo-1;
	    }
	 PreviousClick();
  }    
    //HideShowPreviounsButton();
}



function NextClick() {
    if (currentPageIndex === 2 || currentPageIndex === '2') {
		currentPageIndex =0;
	} else if(currentPageIndex < 2) { 
		currentPageIndex = currentPageIndex + 1;
	}
}
function PreviousClick() {
    if (currentPageIndex === 0 || currentPageIndex === '0') {
		currentPageIndex =2; 
	} else if(currentPageIndex > 0) {
		currentPageIndex = currentPageIndex - 1;
	}
}

function AddElementToArray() {
   var prefixedID = $("#hdnRandomDesignPrefixedID").val() + "_";        
   var idCollection = $("#"+prefixedID + "strDesignId").text();   
   var idsArray = new Array();   
   idsArray = idCollection.split(',');
   var strIds='';
   $.each(idsArray,function( intIndex, objValue ){                     
      strIds = strIds + objValue +",";                                                
        if ((intIndex + 1) % 5 === 0) {
			 arrayOfElements.push(strIds);                   
			 strIds ='';     
			}
   });
}
function ImageLoaded(img, totalCount) {
    currentImageIndex +=1;

    if (currentImageIndex >= totalCount) {
        /*$("#loader").hide();
        $("#ancRndDesignPrev").css("cursor","pointer");
        $("#ancRndDesignNext").css("cursor", "pointer");*/
    }
  
}

function BrokenImage(totalCount) {
   currentImageIndex +=1;

    if (currentImageIndex >= totalCount) {
        /*$("#loader").hide();
        $("#ancRndDesignPrev").css("cursor","pointer");
        $("#ancRndDesignNext").css("cursor", "pointer");*/
   }
}
 
function GetDesigns() {
  InitializeVariable();  
  var prefixedID = $("#hdnRandomDesignPrefixedID").val() + "_";  
  var newLI = $("#"+prefixedID + "liRndFashionDesign" + pageNo);   
  var sectionID = $("#"+prefixedID + "lblSectionID").text();       
  var FashionSupplierID = $("#"+prefixedID + "lblFashionSupplierID").text(); 
    //$("#loader").show();
  currentImageIndex = 0;
    //$("#ancRndDesignPrev").css("cursor", "wait");
    //$("#ancRndDesignNext").css("cursor", "wait");
    if (getNextResult) {
        if ((pageNo + 1) <= arrayOfElements.length) {
        addElements = false;


        var q = $RandomAjaxOptions;
        q.url = asmxRandomDesign + "GetPrevNextRandomFashionDesign";
        q.data = "{designIdCollection: '" + arrayOfElements[pageNo] + "'}";
        q.success = AssociateCallbackNext;
        $.ajax(q);
        //RandomDesign.GetPrevNextRandomFashionDesign(arrayOfElements[pageNo],AssociateCallbackNext);   
    }
        else {
        addElements = true;
        var q = $RandomAjaxOptions;
        q.url = asmxRandomDesign + "GetRandomDesign";
        q.data = "{sectionID: '" + sectionID + "', fashionSupplierID: '" + FashionSupplierID + "'}";
        q.success = AssociateCallbackNext;
        $.ajax(q);
        //RandomDesign.GetRandomDesign(sectionID,FashionSupplierID,AssociateCallbackNext);     
    }
  }
    else {
   var previousID = pageNo -2;
        if (previousID < 0) {
       previousID = 0;
   }
        addElements = false;
        var q = $RandomAjaxOptions;
        q.url = asmxRandomDesign + "GetPrevNextRandomFashionDesign";
        q.data = "{designIdCollection: '" + arrayOfElements[pageNo] + "'}";
        q.success = AssociateCallbackPrevious;
        $.ajax(q);
        //RandomDesign.GetPrevNextRandomFashionDesign(arrayOfElements[previousID],AssociateCallbackPrevious);   
  }
 
}

function AssociateCallbackNext(result) {
 var liToUpdate;
 var idCollection = '';
 if(currentPageIndex === 0 || currentPageIndex ==='0'){liToUpdate=1;}
 if(currentPageIndex === 1 || currentPageIndex ==='1'){liToUpdate=2;}
 if(currentPageIndex === 2 || currentPageIndex ==='2'){liToUpdate=0;}
 var prefixedID = $("#hdnRandomDesignPrefixedID").val() + "_";       
 var ulTag = $("#"+ prefixedID +"ulRndFashionDesign");
 ulTag.find("li").each(function(){
	 var newLI = $("#"+prefixedID + "liRndFashionDesign" + liToUpdate).attr("id"); 
	 if($(this).attr("id")== newLI ) {
			$(this).html(result.d);
			$(this).find("div").each(function(){      
                if ($(this).attr("class") == "rndFashionDesign") {
                    if (idCollection.indexOf($(this).attr("id") + ",") < 0) {
					 idCollection = idCollection + $(this).attr("id") +",";
					}
				} 
			});
		}
	});
    if (addElements) {
		arrayOfElements.push(idCollection); 
	}
    /*$("#ancRndDesignPrev").css("cursor", "pointer");
	$("#ancRndDesignNext").css("cursor","pointer");
    $("#loader").hide();*/
}


function AssociateCallbackPrevious(result) {
 var liToUpdate;
 var idCollection = '';
 if(currentPageIndex ===0 || currentPageIndex ==='0'){liToUpdate=2;}
 if(currentPageIndex ===1 || currentPageIndex ==='1'){liToUpdate=0;}
 if(currentPageIndex ===2 || currentPageIndex ==='2'){liToUpdate=1;}
 var prefixedID = $("#hdnRandomDesignPrefixedID").val() + "_";       
 var ulTag = $("#"+prefixedID+"ulRndFashionDesign");
 ulTag.find("li").each(function(){
 var newLI = $("#"+prefixedID + "liRndFashionDesign" + liToUpdate).attr("id"); 
 if($(this).attr("id")== newLI ) {
  $(this).html(result.d);  
 }
 });
    /*$("#loader").hide();
    $("#ancRndDesignPrev").css("cursor","pointer");
    $("#ancRndDesignNext").css("cursor", "pointer");*/
}

