/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(function(){

    /*mapa*/
    $('#map').hide();
    $('.mapshow').click(function(){
        $('#map').slideToggle();
        var mapdata = '<iframe width="940" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.es/maps?f=q&amp;source=s_q&amp;hl=es&amp;geocode=&amp;q=miscelanea,+guardia+10+08001,+barcelona&amp;aq=&amp;sll=41.378137,2.173651&amp;sspn=0.004178,0.009645&amp;ie=UTF8&amp;hq=miscelanea,+guardia+10&amp;hnear=08001+Barcelona,+Catalu%C3%B1a&amp;ll=41.377806,2.173876&amp;spn=0.006295,0.012142&amp;output=embed"></iframe><br /><small><a href="http://maps.google.es/maps?f=q&amp;source=embed&amp;hl=es&amp;geocode=&amp;q=miscelanea,+guardia+10+08001,+barcelona&amp;aq=&amp;sll=41.378137,2.173651&amp;sspn=0.004178,0.009645&amp;ie=UTF8&amp;hq=miscelanea,+guardia+10&amp;hnear=08001+Barcelona,+Catalu%C3%B1a&amp;ll=41.377806,2.173876&amp;spn=0.006295,0.012142" style="color:#0000FF;text-align:left">Ver mapa más grande</a></small><div class="col12 final">';
      
        $('#map').html(mapdata);
    })

    /* portfolio 26*/
    var $container = $('#primary');

    $container.isotope({
        itemSelector : '.workitem',
        masonry : {
            columnWidth : 240
            //singleMode:true
        },
        animationEngine : $.browser.opera ? 'jquery' : 'best-available',
        animationOptions: {
            duration: 550,
            easing: 'linear',
            queue: false
        }
    });
	// filter buttons
    $('.filters a').live('click',function(){
        var selector = $(this).attr('data-filter');
        $container.isotope({
            filter: selector
        });
        //$(this).parents('ul').find('.selected').removeClass('selected');
        $('.filters a.selected').removeClass('selected');
        $(this).addClass('selected');
        //para colocar el hastag en la url
        window.location.hash = selector.substring(1);

        return false;
    });

	//cargar proyecto mediante ajax
    
    $('.worktrigger').live('click',function(e){

        e.preventDefault();//so the link doesn't try to go somewhere

       //$(this).find('h3').show();
        var workful = $(this).attr("href");
        var workdiv = $(this).parent('div');
		window.location.hash = workful;
		$("#loadwork").slideUp('slow');
        var iconurl = "public/icons/loader.gif";
         $("#loadwork").prepend('<img src="'+ iconurl +'">');//+workful
		//$("#loadwork").slideDown('fast');
		//$("#loadwork").css("width", '310px');
         		$("#loadwork").show();
		
        
		//alert(workdiv);
		
		
        $.ajax({
            url:  workful,
            type: "POST",
            success: function(data){
                $('#loadwork').hide();
                $('#loadwork').slideUp();
                $('#loadwork').html(data);
                //$(workdiv).html(data).css("width", '620px');
                
                $('html, body').animate({scrollTop:0}, 'slow');
                 //reattach slider
                $('.slideshow')
                    .after('<div id="slidenav">')
                    .cycle({
                        fx:      'scrollLeft',
                        speed:    600,
                        timeout:	0,
                        pager:  '#slidenav',
                        next:   'img',
                        before: onBefore
                    });
			
					
            },
            error: function(){
                alert("An error has occurred. Please try again.");
            },
            complete: function() {
                $('#loadwork').hide();
                $('#loadwork').slideDown('slow');
            }
		});
									
		return false;	
	
    });
    
	//si hay hash,, disparar eventos segun ruta
    if(window.location.hash) {
		var hash = window.location.hash;
		
		if(hash.indexOf("/") != -1){ //true			
			
			$('a[href=' + hash.substring(1) + ']').click();
			//alert(hash);

		}else{
			$('.filters a[href=' + window.location.hash + ']').click();

		};		

    }
   

    //slideshow portfolio view
    function onBefore(curr, next, opts, fwd){
        //get the height of the current slide
        var $ht = $(this).height();
        //set the container's height to that of the current slide
        //$(this).parent().animate({"height": $ht}, 50);
                $(this).parent().css("height", $ht);

    }

    $('.slideshow')
    .after('<div id="slidenav">')
    .cycle({
        fx:      'scrollLeft',
        speed:    600,
        timeout:	0,
        pager:  '#slidenav',
        next:   'img',
        before: onBefore


    });

    // jQuery SmoothScroll | Version 10-04-30
    $('a[href*=#]').live('click',function() {

        // duration in ms
        var duration=1000;

        // easing values: swing | linear
        var easing='swing';

        // get / set parameters
        var newHash=this.hash;
        var target=$(this.hash).offset().top;
        var oldLocation=window.location.href.replace(window.location.hash, '');
        var newLocation=this;

        // make sure it's the same location
        if(oldLocation+newHash==newLocation)
        {
            // set selector
            if($.browser.safari) var animationSelector='body:not(:animated)';
            else var animationSelector='html:not(:animated)';

            // animate to target and set the hash to the window.location after the animation
            $(animationSelector).animate({
                scrollTop: target
            }, duration, easing, function() {

                // add new hash to the browser location
                window.location.href=newLocation;
            });

            // cancel default click action
            return false;
        }
    });







});

