/**
 * Código Javascript - Marcelo Coelho
 * @author Volo - www.voloweb.com.br
 */

$('document').ready(function(){



var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
                       string: navigator.userAgent,
                       subString: "iPhone",
                       identity: "iPhone"
                },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		},
		{
			string: navigator.platform,
			subString: "iPad",
			identity: "iPad"
		}
	]

};
BrowserDetect.init();



    $('#loading').fadeOut(1000);

	if(firstVisit == 'no') {
		$('#home_intro').hide();
	} else {
	    setTimeout(function(){
	        $('#home_intro').fadeOut(1000);
	    },4000)		
	}


    $("#menu ul li ul").hide();
    
    $("#menu ul li").hover(function(){
        $(this).children('ul').show();
        $(this).addClass('active');
        $(this).children('.children').dequeue();
    },function(){
        $(this).removeClass('active');
        $(this).children('ul').hide();
        
    });

    // PANEL
    
    var panelWidth;
    var panelHeight;

    var photosWidth;
    var photosHeight;

    var positionX;
    var positionY;
    
    var coeficienteY;
    var coeficienteX;

    var topAdjust = 130;

    var countPhotos;
	var marginPhotos = 6;

    countPhotos = $('#photos').children().length - 1;

    if(countPhotos >= 15) {
        $('#photos').width((5 * 280)+(5*marginPhotos));
    }

    if(countPhotos < 15) {
        $('#photos').width((5 * 280)+(5*marginPhotos));
    }

    if(countPhotos < 12) {
        $('#photos').width((4 * 280)+(4*marginPhotos));
    }

    if(countPhotos < 8) {
        $('#photos').width((3 * 280)+(3*marginPhotos));
    }

    if(countPhotos < 6) {
        $('#photos').width((3 * 280)+(3*marginPhotos));
    }

    if(countPhotos < 4) {
        $('#photos').width((2 * 280)+(2*marginPhotos));
    }

    function center() {
        $('#photos').css({
            marginLeft : (panelWidth/2)-(photosWidth/2),
            marginTop: 0
        })
    }

    function resize() {

        $('#panel_wrapper').css({
            'height':'100%'
        });

        panelWidth = $('#panel_wrapper').width();
        panelHeight = $('#panel_wrapper').height();

        $('#panel_wrapper').css({
            'height':panelHeight-topAdjust
        });

        panelHeight = $('#panel_wrapper').height();

        photosWidth = $('#photos').width();
        photosHeight = $('#photos').height();

        coeficienteX = (photosWidth - panelWidth) / panelWidth;
        coeficienteY = (photosHeight - panelHeight) / panelHeight;
    }

    $('#panel_wrapper').mousemove(function(event){

        if(photosWidth > panelWidth || photosHeight > panelHeight) {
        positionX = ((event.pageX*coeficienteX)*-1);
        positionY = ((event.pageY-topAdjust)*coeficienteY)*-1;

		if(photosHeight < panelHeight) {
			positionY = 0;
		}
        
        $('#photos').animate({
            marginLeft: positionX,
            marginTop: positionY
          },1000,'easeOutQuint'
        );

        $('#photos').clearQueue();

        } else {
            center();
        }
    });

    if(BrowserDetect.OS != 'iPad' && BrowserDetect.OS != 'iPhone') {

        resize();
        center();

        $(window).resize(function() {
            $('#log').empty();
            $('#log').append('<div>Handler for .resize() called.</div>');
            resize();
            if(photosWidth < panelWidth || photosHeight < panelHeight) {
                center();
            }

        });

    } else {
        adjustToMobile();
    }

    function adjustToMobile() {
        $('body').addClass(BrowserDetect.OS);
    }

    $('#actions a').hover(function(){
        $(this).fadeTo(200,0.5);
    }, function(){
        $(this).fadeTo(200,1);
    });

    if(BrowserDetect.OS != 'iPad' && BrowserDetect.OS != 'iPhone') {
        $('#actions').fadeTo(0,0);
    
        $('#photo_container').hover(function(){
            $('#actions').fadeTo(200,1);
        }, function(){
            $('#actions').fadeTo(200,0);
        });

    }

    $('#controls a').hover(function(){
        $(this).fadeTo(200,0.5);
    }, function(){
        $(this).fadeTo(200,1);
    });

    
    $('#about_click').click(function(){
    });


    $("a#zoom").fancybox({
            'overlayShow'	: false
    });

	$("#about_click a").fancybox();
	$("#footer a").fancybox();

    
    var num_el = $('#block #content_block ul li').length;
    var i = 0;
    
    $('#block #content_block ul li').each(function(){
        i++;
        if( i%2 == 0 ) {
            $(this).after('<div class="clear"></div>');
        }
    })

    $('#photo_categories li').hover(function(){
        $(this).children('.theme_description').fadeIn();
    }, function(){
        $(this).children('.theme_description').hide();
    });

})
