// GALLERY BUILDER

var blank_pic = '/bitrix/templates/blog_2.0/i/_.gif';
var g_index = 0;

$(document).ready(function(){
	
	$('.fottGallery').each(function(){
		
	
	//galleryManager[g_index] = new Array();
	$(this).attr('id','galllery_'+g_index);
	g_index++;
		
		$(this).append('<div class="galleryControl regular clear"><span class="galleryStatus brown"></span><div class="galleryButtons"><span class="btnPrev" onclick="getImage(this)">&nbsp;</span><span class="btnNext" onclick="getImage(this)">&nbsp;</span></div><div class="galleryLegend"></div></div>');	
		var ind = $('a',$(this)).index($('a.current'))+1;
					
		if (ind==0) {
			ind = 1;
			$('a:first',$(this)).addClass('current');
		}
		
		var len = $('a',$(this)).length;
		$('.galleryStatus',$(this)).text(ind+'/'+len);
	
		showImage($('a.current',$(this)));
		
	});
	

});

function getImage(item) {
	
	var ind = $('a',$(item).parents('.fottGallery')).index($('a.current')) + 1;
	var len = $('a',$(item).parents('.fottGallery')).length;		
	
	// other way to get index
	ind = 0;
	var lnk = $('a:first',$(item).parents('.fottGallery'));
	var non_stop = true;
	while (non_stop) {
		ind++;
		
		if (lnk.hasClass('current') || ind>len)
			non_stop = false;
		else
			lnk = lnk.next();
	}
	
	
	//alert(ind);
	
	if ($(item).hasClass('btnNext')) {
			
		if (ind==len) {			
			ind = 1;
			$('a.current',$(item).parents('.fottGallery')).removeClass('current');
			$('a:first',$(item).parents('.fottGallery')).addClass('current');									
		} else {				
			ind = ind + 1;				
			$('a.current',$(item).parents('.fottGallery')).removeClass('current').next('a').addClass('current');							
		}
		
		
		
	} else {
			
		if (ind==1) {
			ind = len;
			$('a.current',$(item).parents('.fottGallery')).removeClass('current');
			$('a:last',$(item).parents('.fottGallery')).addClass('current');
		} else {
			ind = ind - 1;
			$('a.current',$(item).parents('.fottGallery')).removeClass('current').prev('a').addClass('current');
		}
	}
	

	
	showImage($('a.current',$(item).parents('.fottGallery')));
	
	$('.galleryStatus',$(item).parents('.galleryControl')).text(ind+'/'+len);
}

function showImage(data) {
	
	var img = new Image();
	var g_id = $(data).parent().attr('id');
	//alert(g_id);
	$('#'+g_id+' img').attr('src',blank_pic);
	
	$(img).load(function(){
		$('#'+g_id+' img').remove();
		$('#'+g_id).prepend(this);
		$(this).attr('width',this.width);
		$(this).attr('height',this.height);
		$(this).click(function(){
			$('#'+g_id+' .btnNext').click();
		});
		
	}).attr('src',$(data).attr('href')).attr('title',$(data).text()).addClass('current').addClass('clickable');
	
	//var rel = $(data).attr('rel');
	var legend = $(data).html();
	$('#'+g_id+' .galleryLegend').html(legend);
	
}

function parse_url (str, component) {
 
    var  o   = {
        strictMode: false,
        key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
        q:   {
            name:   "queryKey",
            parser: /(?:^|&)([^&=]*)=?([^&]*)/g
        },
        parser: {
            strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
            loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-protocol to catch file:/// (should restrict this)
        }
    };
    
    var m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
    uri = {},
    i   = 14;
    while (i--) {uri[o.key[i]] = m[i] || "";}
   
    switch (component) {
        case 'SCHEME':
            return uri.protocol;
        case 'HOST':
            return uri.host;
        case 'PORT':
            return uri.port;
        case 'USER':
            return uri.user;
        case 'PASS':
            return uri.password;
        case 'PATH':
            return uri.path;
        case 'QUERY':
            return uri.query;
        case 'FRAGMENT':
            return uri.anchor;
        default:
            var retArr = {};
            if (uri.protocol !== '') {retArr.scheme=uri.protocol;}
            if (uri.host !== '') {retArr.host=uri.host;}
            if (uri.port !== '') {retArr.port=uri.port;}
            if (uri.user !== '') {retArr.user=uri.user;}
            if (uri.password !== '') {retArr.pass=uri.password;}
            if (uri.path !== '') {retArr.path=uri.path;}
            if (uri.query !== '') {retArr.query=uri.query;}
            if (uri.anchor !== '') {retArr.fragment=uri.anchor;}
            return retArr;
    }
}
