/*
 * ---------------------------------------------------------------- 
 *  
 *  Purity HTML/CSS Template custom jQuery scripts.
 *  
 * ----------------------------------------------------------------  
 */

/*
 * ---------------------------------------------------------------- 
 *  HTML validated popup windows
 * ----------------------------------------------------------------  
 */
function popWin(url, target) {
	if(!target) {target = '_blank';}
	blankWin = window.open(url,target);
};

jQuery(document).ready(function(){	
	

	$('.popwin').click(function(event){
		event.preventDefault();
		
		popWin($(this).attr('href'), '_blank');
	});
/*
 * ---------------------------------------------------------------- 
 *  NivoSlider
 * ----------------------------------------------------------------  
 */

	$(window).load(function() {
		
		$('#slider').nivoSlider({
			pauseTime:4000, // How long each slide will show
			effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
			animSpeed:700, // Slide transition speed	
			directionNav:false // Direction nav (prev/next arrow)
		});
		
	});


/*
 * ---------------------------------------------------------------- 
 *  Twitter
 * ----------------------------------------------------------------  
 */
 	
	getTwitters('tweet', { 
	  id: 'dingbat5000',  /* Your Twitter ID */
	  count: 2, /* Number of tweets that will be shown */
	  enableLinks: true, 
	  ignoreReplies: true, 
	  clearContents: true,
	  template: '<div class="twitter-content">"%text%" </div><div class="quote"><a href="http://twitter.com/%user_screen_name%/statuses/%id_str%/"><span>%time%</span></a></div>'
	});
	
 
/*
 * ---------------------------------------------------------------- 
 *  Dropdown menu
 * ----------------------------------------------------------------  
 */
	
	function mainmenu(){
	$('.nav li').hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(300);
	},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
	});
	}
	
	mainmenu();
	

/*
 * ---------------------------------------------------------------- 
 *  Image hover effect
 * ----------------------------------------------------------------  
 */
 	
	// Over field
	
	$('.over').stop().animate({ "opacity": 0 }, 0);
 	function over() {
		$('.over').hover(function() {
			$(this).stop().animate({ "opacity": .9 }, 250);
		}, function() {
			$(this).stop().animate({ "opacity": 0 }, 250);
		});	
	}
	
	over();
	
	// Firefox fix
	
	if (window.addEventListener) { 
        window.addEventListener('unload', function() {}, false); 
	} 
	
	
	// Opacity change on hover
	
	function hover_opacity() {
		$('.portfolio img,.content .gallery, .button, .big_button, .search_submit, .flickr_badge_image img').hover(function() {
			$(this).stop().animate({ "opacity": .4 }, 250);
		}, function() {
			$(this).stop().animate({ "opacity": 1 }, 250);
		});
	}
	
	hover_opacity();
	
	
	// Social icons
	
	$('.social img').animate({"opacity": .5 }, 0);
	$('.social img').hover(function() {
    	$(this).stop().animate({ "opacity": 1 }, 250);
    }, function() {
    	$(this).stop().animate({ "opacity": .5 }, 250);
    });
	
	
/*
 * ---------------------------------------------------------------- 
 *  Simple codes
 * ----------------------------------------------------------------  
 */
	
	// Tabs
	
	$('.tabs > ul').tabs();
	
	
	// Toggles
	
	$('.toggle-view li').click(function () {
		var text = $(this).children('p');
		
		if (text.is(':hidden')) {
			text.slideDown('fast');
			$(this).children('h6').addClass('active');		
		} else {
			text.slideUp('fast');
			$(this).children('h6').removeClass('active');		
		}		
	});
	
	
/*
 * ---------------------------------------------------------------- 
 *  Quicksand (Sortable Portfolio)
 * ----------------------------------------------------------------  
 */

	
 
	if (jQuery().quicksand) {

        (function($) {
            
            $.fn.sorted = function(customOptions) {
                var options = {
                    reversed: false,
                    by: function(a) {
                        return a.text();
                    }
                };
        
                $.extend(options, customOptions);
        
                $data = jQuery(this);
                arr = $data.get();
                arr.sort(function(a, b) {
        
                    var valA = options.by($(a));
                    var valB = options.by($(b));
            
                    if (options.reversed) {
                        return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;              
                    } else {        
                        return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;  
                    }
            
                });
        
                return $(arr);
        
            };
        
        })(jQuery);
        
        jQuery(function() {
        
            var determine_sort = function($buttons) {
                var $selected = $buttons.parent().filter('[class*="current"]');				
                return $selected.find('a').attr('data-value');
            };
        
            var determine_kind = function($buttons) {
                var $selected = $buttons.parent().filter('[class*="current"]');
                return $selected.find('a').attr('data-value');
            };
        
            var $preferences = {
                duration: 500,
                adjustHeight: 'auto'
            }
        
            var $list = jQuery('.portfolio');
            var $data = $list.clone();
        
            var $controls = jQuery('.filter');
        
            $controls.each(function(i) {
        
                var $control = jQuery(this);
                var $buttons = $control.find('a');
        
                $buttons.bind('click', function(e) {
        
                    var $button = jQuery(this);
                    var $button_container = $button.parent();
                    var button_properties = $(this).text().toLowerCase().replace(' ','-');      
                    var selected = button_properties.selected;
                    var button_segment = button_properties.segment;

                    if (!selected) {
        
                        $buttons.parent().removeClass();
                        $button_container.addClass('current');
        
                        var sorting_type = determine_sort($controls.eq(1).find('a'));
                        var sorting_kind = determine_kind($controls.eq(0).find('a'));
        
                        if (button_properties == 'all-projects') {
                            var $filtered_data = $data.find('li');
                        } else {
                            var $filtered_data = $data.find('li.' + button_properties);
                        }
        
                        var $sorted_data = $filtered_data.sorted({
                            by: function(v) {
                                //return jQuery(v).text().toLowerCase();
								return jQuery(v).find('span[class="item-sort"]').attr('rel');
                            }
                        });

                        $list.quicksand($sorted_data, $preferences, function () {
                                over();
								hover_opacity();
                                prettyPhoto();
                        });
            
                    }
            
                    e.preventDefault();
                    
                });
            
            }); 
            
        });
    
    }
	
/*
 * ---------------------------------------------------------------- 
 *  PrettyPhoto
 * ----------------------------------------------------------------  
 */
	
	function prettyPhoto() {
		$(".gallery a[rel^='gallery']").prettyPhoto({animation_speed:'normal',theme:'pp_default',deeplinking:false,slideshow:3000});
	}
	
	prettyPhoto();
	
/*
 * ---------------------------------------------------------------- 
 *  Grid highlighter
 * ----------------------------------------------------------------  
 */
	
	var col = 0;
	var row = 0;
	var total_rows = 0;
	$('.sf-result-row').each(function(){
		total_rows++;
	})
	
	$('.sf-td-highlight').hover(function(){
		var div_id = $(this).attr('id');
		var id_parts = div_id.split('-');
		row = id_parts[1];
		col = id_parts[2];
		
		col = col.replace('c', '');
		row = row.replace('r', '');
		
		//highlight rows
		for (i=1; i<=34; i++) {
			$('#td-r'+row+'-c'+i).addClass('td-hover');
		}
		
		//highlight cols
		for (j=1; j<=total_rows; j++) {
			$('#td-r'+j+'-c'+col).addClass('td-hover');
		}
	}, function(){
		//remove attrs
		$('.sf-td').each(function(){
			$(this).removeClass('td-hover');
		});
	});

	
/*
 * ---------------------------------------------------------------- 
 *  Grid descriptions popup
 * ----------------------------------------------------------------  
 */

	var descriptions;
	var old_html;
	
	$.get('/help/theme/json', function(data) {
		descriptions = jQuery.parseJSON(data);
	});

	$('.description').hover(function(event){		
		event.preventDefault();
		var theme = $(this).attr('rel');

		old_html = $('#description-box').html();
		$('#description-box').html(descriptions[theme]);
	}, function(){
		$('#description-box').html(old_html);
	});
	

/*
 * ---------------------------------------------------------------- 
 *  Colorbox Popup
 * ----------------------------------------------------------------  
 */

	$('.colorbox').click(function(eve){
		eve.preventDefault();
		if ($(this).attr('rel')) {
			rel_val = $(this).attr('rel');
		} else {
			rel_val = 900;
		}
		$(this).colorbox({width:rel_val+"px", height:"85%"});
		$('#colorbox').show();
	});
	
	
/*
 * ---------------------------------------------------------------- 
 *  Misc
 * ----------------------------------------------------------------  
 */

	//clicks the portfolio
	var $div_to_click = jQuery(this).find('.a-current').attr('id');

	$(window).load(function() {
		$('#'+$div_to_click).trigger('click');
	});
	

});


