
System.Extend.Gallery = {};

var pathPrefix = window.pathPrefix;

function galCbViewInit(){
    		    $(".galCbViewImg").fadeIn(150);
				$(".galCbViewThbSet").fadeIn(150);
				window.imgNavHtml = $("#galCbViewImgNavHolder").html();

				var tempImgData = $.parseJSON(tempDataGet("galCbViewImgDataContainer"));
				window.maxImgIndex = parseInt(tempImgData.maxImgIndex);
				window.galCbViewImgCycle = tempImgData.imgCyclic;
				window.galCbViewShowImgNav = tempImgData.showImgNav;
				window.galCbViewImgHolderWidth = tempImgData.imgHolderWidth;
				window.galCbViewImgHolderHeight = tempImgData.imgHolderHeight;
                window.imgLightboxView = tempImgData.imgLightboxView;

				window.galleryId = tempImgData.galleryId;

				var tempSetData = $.parseJSON(tempDataGet("galCbViewSetDataContainer"));
				window.maxSetIndex = parseInt(tempSetData.maxSetIndex);
				var thumbView = tempSetData.thumbView;
				window.galCbViewThbCycle = tempSetData.thumbCyclic;

				window.toLoad = tempSetData.thumbSet;
				window.toLoad.push(tempImgData.imgPath);

				if(window.galCbViewShowImgNav){
					galCbViewImgNavEnable();
				}

                galCbViewThumbNavEnable();
}

function galCbViewImgChangeAjax(request){
	$(".galCbViewImgArrow").fadeOut(150);
        if(window.imgLightboxView){
             $("#galCbViewImgEnlarge").fadeOut(150);
        }
	$("#galCbViewImgNavHolder").fadeOut(150,function(){$(this).html("");})
	$(".galCbViewImg").fadeOut(150,function(){
		$(this).remove();
		$("#galCbViewImgLoader").fadeIn(10);
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "controllers/gal_controller.php",
            data: request,
            success: function(data){
				//$("#galCbViewHolder").append(data.imgHtml);
				//$("#galCbViewImgNavHolder").html(data.imgHtml);
				/*$("#galCbViewImgHolder").html(data.imgHtml);*/
                    $("#galCbViewImgHolder").animate({
						width: data.imgViewWidth,
						height: data.imgViewHeight
					},200,function(){
						$.xLazyLoader({	
							img: [data.imgPath],
							success: function(){	
								$("#galCbViewImgLoader").fadeOut(10,function(){
                                    $("#galCbViewImgHolder").html(data.imgHtml);
                                    $(".galCbViewImg").fadeIn(150,function(){
                                        $("#galCbViewImgNavHolder").animate({
                                            width: data.imgViewWidth,
                                            height: data.imgViewHeight
                                        },10,function(){
                                            if(window.galCbViewShowImgNav){
                                                $("#galCbViewImgNavHolder").html(window.imgNavHtml);
                                                galCbViewImgNavEnable();
                                                galCbViewImgEnlargePathUpdate(data.imgPath);
                                                $("a[rel^='cbViewThbFancyBox']").fancybox({
                                                    'speedIn': 300,
                                                    'speedOut': 300,
                                                    'cyclic': true
                                                });
                                            }
                                        });
                                    });
                                });

							}
						});	
					});
			  
				
			}
		});
	});
}

function galCbViewThbChangeAjax(request){
	$(".galCbViewThbSet").animate({
		opacity: 0.5
	},150,function(){
		//$("#galCbViewThumbLoader").fadeIn(10);
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "controllers/gal_controller.php",
		    data: request,
		    success: function(data){
				$.xLazyLoader({	
						img: data.thumbSet,
						success: function(){	
							$(".galCbViewThbHolder").html(data.thumbSetHtml);
							$("a[rel^='cbViewThbFancyBox']").fancybox({ 
							  	'speedIn': 300, 
								'speedOut': 300,
								'cyclic': true
							});
							$(".galCbViewThbSet").fadeIn(150);
						}
				});	
			}
		});
	});
	/*$(".galCbViewThbSet").fadeOut(150,function(){
		$(this).remove();
		$("#galCbViewThumbLoader").fadeIn(10);
		$.ajax({
			type: "POST",
			dataType: "json",
			url: pathPrefix+"controllers/gal_controller.php",
		    data: request,
		    success: function(data){
				$("#galCbViewThumbLoader").fadeOut(10,function(){
					$.xLazyLoader({	
						img: data.thumbSet,
						success: function(){	
							$(".galCbViewThbHolder").append(data.thumbSetHtml);
							$("a[rel^='cbViewThbFancyBox']").fancybox({ 
							  	'speedIn': 300, 
								'speedOut': 300,
								'cyclic': true
							});
							$(".galCbViewThbSet").fadeIn(150);
						}
					});	
				});
			}
		});
	});*/
}

function galCbViewNextImg(galleryId,curImgIndex){
	if(typeof(curImgIndex) === "undefined"){
		curImgIndex = "";
	}
	var request = "action=GetCbViewNextImg&CurImgIndex="+curImgIndex+"&GalleryId="+galleryId;
	galCbViewImgChangeAjax(request);
}

function galCbViewPrevImg(galleryId,curImgIndex){
	if(typeof(curImgIndex) === "undefined"){
		curImgIndex = "";
	}
	var request = "action=GetCbViewPrevImg&CurImgIndex="+curImgIndex+"&GalleryId="+galleryId;
	galCbViewImgChangeAjax(request);
}

function galCbViewNextSet(galleryId,curSetIndex){
	if(typeof(curSetIndex) === "undefined"){
		curSetIndex = "";
	}
	var request = "action=GetCbViewNextSet&CurSetIndex="+curSetIndex+"&GalleryId="+galleryId;
	galCbViewThbChangeAjax(request);
}

function galCbViewPrevSet(galleryId,curSetIndex){
	if(typeof(curSetIndex) === "undefined"){
		curSetIndex = "";
	}
	var request = "action=GetCbViewPrevSet&CurSetIndex="+curSetIndex+"&GalleryId="+galleryId;
	galCbViewThbChangeAjax(request);
}

function galCbViewLoadImg($galleryId,input,type){
	var request;
	if(type === "index"){
		request = "action=GetCbViewImg&GetType="+type+"&ImgIndex="+input+"&GalleryId="+galleryId;
		galCbViewImgChangeAjax(request);
	}else if(type === "path"){
		request = "action=GetCbViewImg&GetType="+type+"&ImgPath="+input+"&GalleryId="+galleryId;
		galCbViewImgChangeAjax(request);
	}else if(type === "false"){
		return false;
	}
}

function galCbViewImgNavEnable(){
	$("#galCbViewImgRtArrowHolder").hover(
		function(){
			$("img#galCbViewImgRtArrow").fadeIn(150);
		},
		function(){
			$("img#galCbViewImgRtArrow").fadeOut(150);
		}
	);
	$("#galCbViewImgLtArrowHolder").hover(
		function(){
			$("img#galCbViewImgLtArrow").fadeIn(150);
		},
		function(){
			$("img#galCbViewImgLtArrow").fadeOut(150);
		}
	);
	$("#galCbViewImgRtArrow").click(function(){
		var curImgId = $("#galCbViewImgHolder img.galCbViewImg").attr('id');
		var curImgIdArray = curImgId.split("-");
		var curImgIndex = parseInt(curImgIdArray[1]);
		if(curImgIndex < window.maxImgIndex){
			galCbViewNextImg(window.galleryId,curImgIndex);
		}else if(curImgIndex === window.maxImgIndex){
			if(window.galCbViewImgCycle){
				curImgIndex = -1;
				galCbViewNextImg(window.galleryId,curImgIndex);
			}
		}
	});
	$("#galCbViewImgLtArrow").click(function(){
		var curImgId = $("#galCbViewImgHolder img.galCbViewImg").attr('id');
		var curImgIdArray = curImgId.split("-");
		var curImgIndex = parseInt(curImgIdArray[1]);
			if(curImgIndex > 0){
				galCbViewPrevImg(window.galleryId,curImgIndex);
			}else if(curImgIndex === 0){
				if(window.galCbViewImgCycle){
					curImgIndex = window.maxImgIndex + 1;
					galCbViewPrevImg(window.galleryId,curImgIndex);
				}
			}
	});	
}

function galCbViewImgNavDisable(){
	$("#galCbViewImgRtArrow").fadeOut(150);
	$("#galCbViewImgLtArrow").fadeOut(150);
	$("#galCbViewImgRtArrowHolder").unbind('mouseenter').unbind('mouseleave');
	$("#galCbViewImgLtArrowHolder").unbind('mouseenter').unbind('mouseleave');
}
/*function galCbViewLoader(type,time){
	if(typeof(time) === "undefined"){
		time = 150;
	}
	if(type === "fadeIn"){
		$("#galCbViewImgLoader").fadeIn(time);
	}else if(type === "fadeOut"){
		$("#galCbViewImgLoader").fadeOut(time);
	}
}

function galCbViewHolder(type,time){
	if(typeof(time) === "undefined"){
		time = 150;
	}
	if(type === "fadeIn"){
		$("#galCbViewImgHolder").fadeIn(time);
	}else if(type === "fadeOut"){
		$("#galCbViewImgHolder").fadeOut(time);
	}
}*/

function galCbViewImgEnlargePathUpdate(imgPath){
    $("#galCbViewImgNavHolder a").attr({"href":imgPath});
}

function galCbViewThumbNavEnable(){
				$("#galCbViewThbLtArrow").click(function(){
					var curSetId = $(".galCbViewThbSet").attr('id');
					var curSetIdArray = curSetId.split("-");
					var curSetIndex = parseInt(curSetIdArray[1]);
					if(curSetIndex > 0){
						galCbViewPrevSet(window.galleryId,curSetIndex);
					}
					if(window.galCbViewThbCycle){
						if(curSetIndex === 0){
							curSetIndex = window.maxSetIndex + 1;
							galCbViewPrevSet(window.galleryId,curSetIndex);
						}
					}
				});
				$("#galCbViewThbRtArrow").click(function(){
					var curSetId = $(".galCbViewThbSet").attr('id');
					var curSetIdArray = curSetId.split("-");
					var curSetIndex = parseInt(curSetIdArray[1]);
					if(curSetIndex < window.maxSetIndex){
						galCbViewNextSet(window.galleryId,curSetIndex);
					}
					if(window.galCbViewThbCycle){
						if(curSetIndex === window.maxSetIndex){
							curSetIndex = -1;
							galCbViewNextSet(window.galleryId,curSetIndex);
						}
					}
				});
}

//NEW

System.Extend.Gallery.ThumbView = function(args){
    if(typeof(args) == 'undefined'){args = {};}
    if(typeof(args.galleryID) == 'undefined'){args.galleryID = '';}
    /*if(typeof(args.images) == 'undefined'){args.images = [];}*/
    if(typeof(args.thumbsCount) == 'undefined'){args.thumbsCount = 6;}
    if(typeof(args.rowsCount) == 'undefined'){args.rowsCount = 2;}
    if(typeof(args.galleryControllerPath) == 'undefined'){args.galleryControllerPath = 'controllers/gal_controller.php';}
    if(typeof(args.thumbSize) == 'undefined'){args.thumbSize = '40x40';}
    if(typeof(args.autoInit) == 'undefined'){args.autoInit = true;}
    
    this.args = args;
    this.ID = System.generateID();
    this.holderID = "UI_galleryThumbViewHolder"+this.ID;
    this.holderSelector = "#"+this.holderID;
    this.html = '';
    
    this.init = function(){
        var self = this;
        this.html = '<div id="'+self.holderID+'">';
        this.html += '</div>';
    }
    
    this.getData = function(){
        $.ajax({
            type: "POST",
            dataType: "json",
            data: "action=thumbViewGetData&galleryID="+this.args.galleryID,
            success: function(){
                alert(data);
            }
        });
    }
    
    
}
 
System.Extend.Gallery.CombView = function(args){
    if(typeof(args) == 'undefined'){args = {};}
    if(typeof(args.galleryID) == 'undefined'){args.galleryID = '';}
    /*if(typeof(arga.images) == 'undefined'){args.images = [];}
    if(typeof(arga.thumbs) == 'undefined'){args.thumbs = [];}*/
    if(typeof(arga.imgHolderWidth) == 'undefined'){args.imgHolderWidth = 500;}
    if(typeof(arga.imgHolderHeight) == 'undefined'){args.imgHolderHeight = 300;}   
    if(typeof(arga.thumbHolderWidth) == 'undefined'){args.thumbHolderWidth = 500;}
    if(typeof(arga.thumbSize) == 'undefined'){args.thumbSize = 40;}
    if(typeof(arga.thumbView) == 'undefined'){args.thumbView = "lightbox";}
    if(typeof(arga.thumbCyclic) == 'undefined'){args.thumbCyclic = true;}
    if(typeof(arga.imgCyclic) == 'undefined'){args.imgCyclic = true;}
    if(typeof(arga.showImgNav) == 'undefined'){args.showImgNav = true;}
    if(typeof(arga.showThumbNav) == 'undefined'){args.showThumbNav = true;}
    if(typeof(arga.imgLightboxView) == 'undefined'){args.imgLightboxView = true;}
    if(typeof(arga.thumbSpacing) == 'undefined'){args.thumbSpacing = 2;}
    if(typeof(arga.loaderPath) == 'undefined'){args.loaderPath = "images/gal-loader.gif";}
    if(typeof(arga.controllerPath) == 'undefined'){args.controllerPath = "controllers/gal_controller.php";}
    
    this.args = args;
    
    this.init = function(){
        var self = this;
        if(args.galleryID == ''){
            var initArgs = {imagesArray:self.args.images,thumbsArray:self.args.thumbs};
            this.getInit(args);
        }else{
            var initArgs = {galleryID:self.args.galleryID};
            this.getInit();
        }
    }
    
    this.getInit = function(initArgs){
        if(typeof(initArgs.galleryID) == 'undefined'){initArgs.galleryID = ''}
        
        /*if(initArgs.galleryID == ''){
            var imagesArray = encodeURIComponent($.toJSON(initArgs.imagesArray));
            var thumbsArray = encodeURIComponent($.toJSON(initArgs.thumbsArray));
            $.ajax({
                type: "POST",
                dataType: "json",
                data: "cbViewGetImagesData&galleryId=&thumbsArray="+thumbsArray+"&imagesArray="+imagesArray,
                success: function(data){
                    alert(data);
                }
            });
        }else{
            $.ajax({
                type: "POST",
                dataType: "json",
                data: "cbViewGetImagesData&galleryId="+initArgs.galleryID,
                success: function(data){
                    alert(data);
                }
            });    
        }*/
            $.ajax({
                type: "POST",
                dataType: "json",
                data: "cbViewGetImagesData&galleryId="+initArgs.galleryID,
                success: function(data){
                    alert(data);
                }
            });        
    }
}

System.Extend.Gallery.TabView = function(selector,args){
    //args.tabsData = ["tabTitle:galleryID"];
    if(typeof(args) == 'undefined'){args = {};}
    if(typeof(args.tabData) == 'undefined'){args.tabData = {};}
    if(typeof(args.tabIndex) == 'undefined'){args.tabIndex = 0;}
    if(typeof(args.prefixText) == 'undefined'){args.prefixText = "LOKACIJE: ";}
    if(typeof(args.prefixTextMargin) == 'undefined'){args.prefixTextMargin = 10;}
    if(typeof(args.controllerScript) == 'undefined'){args.controllerScript = "controllers/gal_controller.php";}
    if(typeof(args.width) == 'undefined'){args.width = 500;}
    if(typeof(args.tabMargin) == 'undefined'){args.tabMargin = 5;}
    /*if(typeof(args.loaderPath) == 'undefined'){args.loaderPath = "images/gal_loader.gif";}
    if(typeof(args.loaderText) == 'undefined'){args.loaderText = "Loading...";}
    if(typeof(args.loaderType) == 'undefined'){args.loaderType = "image";}*/
    if(typeof(args.autoInit) == 'undefined'){args.autoInit = true;}
    
    this.args = args;
    this.tabIndex = this.args.tabIndex;
    this.ID = System.generateID();
    this.containerID = "UI_gallery_tabView_container"+this.ID;
    this.containerSelector = "#"+this.containerID;
    this.tabsContainerID = "UI_gallery_tabView_tabContainer"+this.ID;
    this.tabsContainerSelector = "#"+this.tabsContainerID;
    this.contentID = "UI_gallery_tabView_content"+this.ID;
    this.contentSelector = "#"+this.contentID;
    this.galleryIDs = [];
    this.tabIDs = [];
    this.html = '';
    
    this.init = function(){
        var self = this;
        var tabCtr = 0;
        var tabMargin = 0;
        self.html += "<div class='UI_gallery_tabView_container' id='"+self.containerID+"' style='width:"+self.args.width+"px;'>";
         self.html += "<div class='UI_gallery_tabView_tabContainer' id='"+self.tabsContainerID+"'>";
          self.html += '<table><tr>';
          for(var tabTitle in self.args.tabData){
            var galleryID = self.args.tabData[tabTitle];
            var tabID = "UI_gallery_tabView_tab_"+tabCtr;
            self.tabIDs.push(tabID);
            self.galleryIDs.push(galleryID);
            var tabClass = "UI_gallery_tabView_tab";
            if(tabCtr != 0){
                tabMargin = self.args.tabMargin;
            }else{
                self.html += '<td><span class="UI_gallery_tabView_prefixText">'+self.args.prefixText+'</span></td>';
            }
            if(self.tabIndex == tabCtr){
                tabClass = "UI_gallery_tabView_tab_selected";
            }
            self.html += '<td><span style="margin-left:'+tabMargin+'px;" class="'+tabClass+'" id="'+tabID+'">'+tabTitle+'</span></td>';
            self.html += '<script type="text/javascript">$("#'+tabID+'").click(function(){'+self.args.instance+'.loadContent('+galleryID+')})</script>';
            tabCtr++;
          }
          self.html += '</tr></table>';
         self.html += "</div>";
         self.html += "<div class='UI_gallery_tabView_content' id='"+self.contentID+"'>";
         self.html += "</div>";
        self.html += "</div>";
        $(selector).html(self.html);
        self.loadContent(self.galleryIDs[self.tabIndex]);
    }
    
    this.loadContent = function(galleryID){
        var self = this;
        $("#"+self.tabIDs[self.tabIndex]).removeClass("UI_gallery_tabView_tab_selected");
        $("#"+self.tabIDs[self.tabIndex]).addClass("UI_gallery_tabView_tab");
        self.tabIndex = self.galleryIDs.indexOf(galleryID);
        $("#"+self.tabIDs[self.tabIndex]).addClass("UI_gallery_tabView_tab_selected");
        //var selectorWidth = $(selector).width();
        var loaderHtml = "<div style='width:30px;height:30px;margin-top:100px;margin-left:250px;'>Loading...</div>";
        /*$(self.contentSelector).fadeOut(100,function(){
            $(self.contentSelector).html(loaderHtml);
            $(self.contentSelector).fadeIn(100);
        });*/$(self.contentSelector).html(loaderHtml);
        $.ajax({
            type : "POST",
            url : self.args.controllerScript,
            data : "action=getTabViewThumbs&containerWidth="+self.args.width+"&galleryID="+galleryID,
            success: function(data){
                $(self.contentSelector).fadeOut(100,function(){
                    $(self.contentSelector).html(data);
                    $(self.contentSelector).fadeIn(300);
                });
            }
        });
    }
    
    if(this.args.autoInit){
        this.init();
    }
    
}

