mobileBrowser = false;

if(navigator.userAgent.indexOf('mobile') != -1 || navigator.userAgent.indexOf('Mobile') != -1){
	mobileBrowser = true;
}
   	

$.fn.focketFancy = function(options) {
    var defaults = {
        theme : 'gm-style'
    }
        
    options = $.extend(defaults, options);  
    
   	initializeFancy = function(){
   	
   	 
    $('.focketFancy').remove(); // remove any open focket fancy's
    
    // Create new instance
    
    uid = giveUnusedID();
    
    $('body').prepend("<div class='focketFancy' id='"+uid+"'></div>");
    
    theFocket = $('#'+uid);
    
    theFocket.append("<div class='fckt-overlay'></div>");
    fcktOverlay = theFocket.children('.fckt-overlay'); 
        
    theFocket.append("<div class='fckt-container'></div>");
    fcktContainer = theFocket.children('.fckt-container');
    
    fcktContainer.append("<div class='fckt-title'></div>");
    fcktTitle = fcktContainer.children('.fckt-title');
    
    fcktContainer.append("<div class='fckt-share'><div style='float:left;'>SHARE:&nbsp;</div><a style='display:block; float:left; height:20px; width:20px;' onclick='fbs_click();' href='javascript:void(0);'><img style='position:relative; top:-1px;' src='"+source+"images/fb-share.png' alt='Share on Facebook' /></a><a style='display:block; margin-left:5px; float:left; height:20px; width:20px;' onclick='hyves_click();' href='javascript:void(0);'><img width='20' height='20' style='position:relative; top:-1px;' src='"+source+"images/hyves-share.png' alt='Share on Hyves' /></a><a style='display:block; margin-left:5px; float:left; height:20px; width:20px;' onclick='twitter_click();' href='javascript:void(0);'><img width='20' height='20' style='position:relative; top:-1px;' src='"+source+"images/twit-share.png' alt='Share on Hyves' /></a></div>");
    fcktShare = fcktContainer.children('.fckt-share');
    
    fcktContainer.append("<div class='fckt-close'></div>");
    fcktClose = fcktContainer.children('.fckt-close');
    
    fcktContainer.append("<div class='fckt-box'></div>");
    fcktBox = fcktContainer.children('.fckt-box');

	fcktContainer.append("<div class='fckt-wrap'><ul class='fckt-gallery'></ul></div>");
	fcktWrap = fcktContainer.children('.fckt-wrap'); 
	fcktGallery = fcktWrap.children('.fckt-gallery'); 

	fcktContainer.append("<div class='fckt-arwprev'></div>");
	fcktPrev = fcktContainer.children('.fckt-arwprev');  
	
	fcktContainer.append("<div class='fckt-arwnext'></div>");
	fcktNext = fcktContainer.children('.fckt-arwnext');  
	
	fcktBox.append("<div class='fckt-mainprev'></div>");
	fcktMainPrev = fcktBox.children('.fckt-mainprev');  
	
	fcktBox.append("<div class='fckt-mainnext'></div>");
	fcktMainNext = fcktBox.children('.fckt-mainnext');	
   	
   	}    
   	
   	// MAIN STYLES
   	
   	applyTheme = function(){
   	
   	if(!mobileBrowser){
   	
   	theFocket.
   	add(fcktOverlay).css({
   		'position' 	: 'fixed',
   		'left'	   	: '0px',
   		'right'		: '0px',  		
   		'top'		: '0px',
   		'bottom'	: '0px'
   	})
   	
   	}
   	else{
   	
   	theTop = $(window).scrollTop()-50;
   	theHeight = $(window).height()+100;
   	
   	
   	theFocket.css({
   		'position' 	: 'absolute',
   		'left'	   	: '0px',
   		'right'		: '0px',  		
   		'top'		: theTop+'px',
   		'height'	: theHeight+'px'
   	})
   	
   	fcktOverlay.css({
   		'position' 	: 'absolute',
   		'left'	   	: '0px',
   		'right'		: '0px',  		
   		'top'		: '0px',
   		'bottom'	: '0px'
   	})
   	
   	}
   	
   	
   	fcktTitle.css({
   		'position'	  : 'absolute',
   		'left'		  : '0',
   		'right'		  : '0',
   		'top'		  : '-28px',
   		'text-align'  : 'center',
   		'text-transform' : 'uppercase',
		'font-family' : "'MS Sans Serif', Geneva, sans-serif",
		'color'		  : '#FFF',
		'font-size'	  : '12px',
   		'line-height' : '14px'
   	})
   	
   	fcktShare.css({
   		'position'	  : 'absolute',
   		'left'		  : '30px',
   		'top'		  : '-30px',
   		'line-height' : '20px',
   		'text-transform' : 'uppercase',
		'font-family' : "'MS Sans Serif', Geneva, sans-serif",
		'color'		  : '#FFF',
		'font-weight' : 'bold',
		'font-size'	  : '12px'
   	})
   	
   	theFocket.css({
   		'z-index'	: '200'
   	});
   	
   	fcktContainer.css({
   		'position'	  : 'absolute',
   		'z-index'	  : '200',
   		'width'		  : '1000px',
   		'height'	  : '500px',
   		'left'		  : '50%',
   		'top'		  : '50%',
   		'margin-left' : '-500px',
   		'margin-top'  : '-269px'
   	});
   	
   	fcktBox.css({
   		'background-color' : '#FFF',
   		'width'		 	   : '940px',
   		'margin'		   : 'auto',
   		'height'		   : '420px',
   		'border-radius'	   : '6px',
   		'position'		   : 'relative'
   	})
   	
   	fcktBox.children('table').css({
   		'position'	: 'absolute',
   		'left'		: '0',
   		'top'   	: '0',
   		'z-index'	: '1'
   	})
   	
   	fcktBox.children('table').hide();
   	
    fcktGallery.
    add(fcktGallery.children('li')).css({
    	'padding'	: 0,
    	'margin'	: 0,
    	'list-style': 'none'
    });    
   	
   	fcktWrap.css({
   		'width'		   : '940px',
   		'margin'	   : 'auto',
   		'overflow'	   : 'hidden',
   		'height'	   : '100px',
   		'margin-top'   : '20px'
   	})
    
    fcktGallery.children('li').css({
    	'float'		: 'left',
    	'width'		: '100px',
    	'height'	: '100px',
   		'margin-left'  : '20px',
   		'background-color' : '#FFF',
   		'border-radius'	   : '6px',
   		'cursor'	: 'pointer'
    })
   	
   	fcktGallery.children('li:first-child').css({
   		'margin-left'	:	'0'
   	})
   	
   	fcktClose.css({
   		'background-image'	:	'url('+source+'includes/js/focketImage/close.png)',
   		'width'				: 	'27px',
   		'height'			:	'27px',
   		'position'			:	'absolute',
   		'right'				:	'30px',
   		'top'				:	'-38px',
   		'cursor'			:	'pointer'
   	})
   	
   	fcktPrev.
   	add(fcktNext).css({
   		'height' : '27px',
   		'width'  : '15px',
   		'position' : 'absolute',
   		'top'    : '476px',
   		'cursor' : 'pointer'
   	})
   	
   	fcktPrev.add(fcktNext).hide();
   	
   	fcktPrev.css({
   		'background-image'	:	'url('+source+'includes/js/focketImage/arwLeft.png)', 
   		'left'				:   '0'
   	})
   	
   	fcktNext.css({
   		'background-image'	:	'url('+source+'includes/js/focketImage/arwRight.png)', 
   		'right'				:   '0'
   	})
   	
   	fcktMainPrev.
   	add(fcktMainNext).css({
   		'height' : '87px',
   		'width'  : '44px',
   		'position' : 'absolute',
   		'top'    : '167px',
   		'cursor' : 'pointer',
   		'z-index':	'100'
   	})
   	
   	fcktMainPrev.add(fcktMainNext).hide();
   	
   	fcktMainPrev.css({
   		'background-image'	:	'url('+source+'includes/js/focketImage/arwLeft000.png)',
   		'background-position' : 'center center',
   		'background-repeat'	: 'no-repeat', 
   		'left'				:   '0px'
   	})
   	
   	fcktMainNext.css({
   		'background-image'	:	'url('+source+'includes/js/focketImage/arwRight000.png)', 
   		'background-position' : 'center center', 
   		'background-repeat'	: 'no-repeat', 
   		'right'				:   '0px'
   	})
   	
   	fcktClose.click(removeFancy);
   	
   	if(!mobileBrowser){
   	
   	fcktContainer.hide();
   	fcktOverlay.fadeTo(0,0);
   	fcktOverlay.fadeTo(300,0.85,function(){
   		fcktContainer.fadeIn(400);
   	});
   	
   	}
   	
   	
   	if(options.theme == 'gm-style'){
		
		// THEME STYLES
		   	
	   	fcktOverlay.css({
	   		'background-color'	: '#000'
	   	})
	   	
   	
   	}
   	
   	}
    
    removeFancy = function(){
    	$('.ios-overlay').find('img').hide();
    	fcktContainer.fadeOut(400,function(){
    		fcktOverlay.fadeTo(300,0,function(){
    			theFocket.remove(); 
    			$('.ios-overlay').hide();
    			$('#ios-bgs').hide();
    			$('#ios-bgs').show();   		
    		});    	
    	});
    }
    
    createMainSlider = function(){
    	fcktMainPrev.add(fcktMainNext).show();
    	
    	fcktMainPrev.click(function(){
    		
    		
    		if(fcktGallery.children('.active').prev().length != '0'){
    			
    			fcktGallery.children('.active').prev().click();
    			
    			val = Math.ceil((fcktGallery.children('.active').index()+1) / 8) - 1;
    			
    			//document.title =  Math.ceil((fcktGallery.children('.active').index()+1) / 8) - 1;
    			
	    		if(val < curr){
	    			setTimeout("slidePrev()",100);
	    		}
    		}
    		else{
    			fcktGallery.children('li:last').click();
    			
    			if(isInt(theAselection.length/8)){
    				curr = theAselection.length/8;
    			}
    			else{
    				curr = Math.ceil(theAselection.length/8)-1;
    			}
    			
    			
    			setTimeout("fcktGallery.animate({'margin-left' : '-"+960*curr+"'},300)",100); 			
    			
    			fcktNext.hide();
    			fcktPrev.show(); 			
    		}    		
    		
    	})
    	
    	fcktMainNext.click(function(){
    		
    		
    		if(fcktGallery.children('.active').next().length != '0'){
    			
    			fcktGallery.children('.active').next().click();
    			
    			val = Math.ceil((fcktGallery.children('.active').index()+1) / 8) - 1;
	    		if(val > curr && val!=0){
	    			setTimeout("slideNext()",100);
	    		}
    		}
    		else{
    			fcktGallery.children('li:first').click();
    			    			
    			setTimeout("fcktGallery.animate({'margin-left' : '0'},300)",100);
    			curr = 0;
    			
    			
    			fcktNext.show();
    			fcktPrev.hide();
    			
    		}    		
    		
    	})
    	
    }
    
    createSlider = function(length){
    	fcktGallery.css('width',length*120+'px');
    	fcktNext.show();
    	
    	fcktNext.click(slideNext);
       	fcktPrev.click(slidePrev);
    	
    	curr = 0;
    	max = length/8;
    }
    
    slideNext = function(){
    	if(curr+1 <= max){
    		fcktGallery.animate({'margin-left' : '-=960'},300);
    		curr++;
    	
    		if(curr > 0){
    			fcktPrev.show();
    		}
    	
    		if(curr+1 > max){
    			fcktNext.hide();
    		}
    	}
    	else{
    		fcktNext.hide();
    	}
    }
    
    
    slidePrev = function(){
    	if(curr-1 >= 0){
    		fcktGallery.animate({'margin-left' : '+=960'},300);
    		curr--;
    	
    		fcktNext.show();
    	
    		if(curr == 0){
    			fcktPrev.hide();
    		}
    	}
    	else{
    		fcktPrev.hide();
    	}
    }
    
    return this.each(function() {
    	
    	$(this).click(function(){
    		initializeFancy();
    		
    		theAselection = $('a[rel='+this.getAttribute('rel')+']');
    		
    		theAselection.each(function(){
    			indID = giveUnusedID();
    			this.setAttribute('indID',indID)
    			
    			
    			fcktGallery.append("<li imageID='"+this.getAttribute('imageID')+"'  class='"+this.getAttribute('imageID')+"' desc='"+$(this).children('img')[0].getAttribute('alt')+"' url='"+this.getAttribute('href')+"' indID='"+indID+"' id='link"+indID+"'><table cellpadding='0' width='100%' height='100%' cellspacing='0'><tr><td valign='middle' align='center'><img src='"+$(this).children('img')[0].src+"' /></td></tr></table></li>");
    			$('#link'+indID).click(function(){
    			
    				fcktGallery.children('li').removeClass('active');
    				//fcktGallery.children('li').css('background-color','#FFF');
    				    				
    				$(this).addClass('active');
    				//$(this).css('background-color','#000');
    				if($('#'+this.getAttribute('indID'))[0] != fcktBox.children('.active')[0]){
    			
    				fcktBox.children('.active').fadeOut().removeClass('active');
    				$('#'+this.getAttribute('indID')).css('z-index','2');
    				$('#'+this.getAttribute('indID')).fadeIn(function(){
    					$('#'+this.getAttribute('indID')).css('z-index','3');
    				});
    				$('#'+this.getAttribute('indID')).addClass('active');
    				
    				fcktTitle[0].innerHTML = this.getAttribute('desc');
    				}
    				
    			})
    			
    			
    			if(this.getAttribute('external') && this.getAttribute('external').indexOf('http://') != -1){
    				external = "<div loca='"+this.getAttribute('external')+"' id='viewexternal"+indID+"' style='position:absolute; cursor:pointer; background-position:center center; left:0; display:none; right:0; bottom:0; z-index:4; top:0; background-image:url("+source+"images/pub_overlay.png);'></div>";
    			}else{
    				var external = '';
    			}
    			
    			fcktBox.append("<table id='"+indID+"' cellpadding='0' width='100%' height='100%' cellspacing='0'><tr><td valign='middle' align='center'>"+external+"<img src='"+this.getAttribute('href')+"' /></td></tr></table>");    			
    			if(this.getAttribute('external') != ''){
    				$('#'+indID).mouseover(function(){
    					if(this.className == 'active'){
    						$('#viewexternal'+this.id).show();
    						$('#viewexternal'+this.id).click(function(){
    							newWindow = window.open(this.getAttribute('loca'), '_blank');
								newWindow.focus();
								this.unbind('click');
    						})
    					}
    				});
    				$('#'+indID).mouseout(function(){
    					if(this.className == 'active'){
    						$('#viewexternal'+this.id).hide();
    					}
    				})
    			}
    		})
    		
   			applyTheme();
   			fcktGallery.children('li:first').addClass('active');
   			//fcktGallery.children('li:first').css('background-color','#000');
    		$('#'+this.getAttribute('indID')).css('z-index','3');
    		$('#'+this.getAttribute('indID')).show();
    		$('#'+this.getAttribute('indID')).addClass('active');
    		fcktTitle[0].innerHTML = $(this).find('img')[0].getAttribute('alt');
    		
    		if(theAselection.length > 8){
    			createSlider(theAselection.length);
    		}
    		
    		if(theAselection.length > 1){
    			createMainSlider();
    		}
    		
    	})
    	
    });
}


function giveUnusedID(){

   var chars = '0123456789abcdef'.split('');

   var uuid = [], rnd = Math.random, r;
   uuid[8] = uuid[13] = uuid[18] = uuid[23] = '';
   uuid[14] = '4'; 

   for (var i = 0; i < 30; i++)
   {
      if (!uuid[i])
      {
         r = 0 | rnd()*16;

         uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
      }
   }

   return "lo" + uuid.join('');
}

function isInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false; 
   return x==y && x.toString()==y.toString(); 
 } 
