/*
	artsway.js
	this is the js file where all frontend functionality should be
*/

function is_defined(func) {
    return (typeof(window[func]) == "undefined")?  false: true;
}

function resize_on_load() {
	var cookie = $.cookie('text-size');
	
	if (typeof(cookie) != "undefined") {
		resize_text(cookie);
	}
}

function RenderPaging(page, totalpages) {

    if (page >= totalpages) {
        $("div.gallery-navigation a.next").hide();
        $("div.gallery-navigation a.previous").show();
    } else if (page <= 1) {
        $("div.gallery-navigation a.next").show();
        $("div.gallery-navigation a.previous").hide();
    } else {
        $("div.gallery-navigation a.next").show();
        $("div.gallery-navigation a.previous").show();
    }

}

$(document).ready(function() {
	/// Open links with a class of external in a new window/tab
    $("a.external").click(function() {
		window.open($(this).attr("href"));
		return false;
	});
	
	// Group twitter feed
	$("div.twitter").each(function() {
		var id = $(this).attr('id');

		var s = {
			username: "artsway",
			list: "",
			loading_text: "Loading twitter feed...",
			count: 3,
			callback: function() {resize_on_load();}
		};
		
		if (id != '') {
			s.username = id;	
		} 
		$(this).twitter(s);
	});
	
	//This is the resize function
/* 	if ($("div.comments").length == 0) { */
	    setTimeout("resize(0)",1000);
/* 	} */
	
	if ($('.image-button').length > 0) {
		//List all images then add them to the site
		$('.image-button').each(function() {
			var src = $(this).next().children('img').attr('src');
			var desc = $(this).next().children('img').attr('alt');
			var link_src = src.replace('images/sized/','').replace('-434x0','').replace('-0x434','');
			var html = $('.mediaSlot').html();
			var new_link = "<a rel='gallery' class='article-image-link' href='" + link_src + "'><img class='article-image' src='" + src + "' alt='" + desc + "' /></a>";
			
			$('.mediaSlot').html(html + new_link);
			var $caption = $("<p class='caption')>" + desc + "</p>");
			$caption.insertAfter('.mediaSlot'); 						
		});
	
		//Hide all but the first image + caption
		$('.mediaSlot a:not(.mediaSlot a:first)').hide();
		$('.caption:not(.caption:last)').hide();

	
		//Adding a class to the first one
		$('.mediaSlot a:first').addClass('active');
		$('.caption:last').addClass('active');

		//Image replacement (thumbnail to large image)		
		$('.image-button').click(function() {
			var src = $(this).next().children('img').attr('src');
			var link_src = src.replace('images/sized/','').replace('-434x0','').replace('-0x434','');
			if ($('.article-image-link.active').attr('href') != link_src) {
				$('.article-image-link.active').removeClass('active').fadeOut(250, function() {
					$('.article-image-link').each(function() {
						var this_link_src = $(this).attr('href');
						if (this_link_src == link_src) {
							$(this).fadeIn(250).addClass('active');
							$('.caption.active').text($(this).find('img').attr('alt'));
						}
					});
				});
			}
			return false;
		});
	
		//fancybox
		$('.article-image-link').fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	true,
			'showNavArrows' :	true
		});
	}
	
	if ($('.image-button').length > 1) {
        $("div.gallery").css({ "position": "relative" });
        $("div.gallery ul.thumbnails").css({ "position": "absolute" });
        var totalThumbnails = $("ul.thumbnails li").size();
        var totalPages = Math.ceil(totalThumbnails / 2);
        var page = 1;
        var html = "";
        var _position = 0;
        var _positionTotal = (_position-((totalPages-1)*106));

        if (totalPages > 1) {
			var html = "<div class=\"gallery-navigation\"><a href=\"javascript:void(0);\" class=\"previous\" style=\"display:none;\" title=\"Previous\">Previous</a><a href=\"javascript:void(0);\" class=\"next\" title=\"Next\">Next</a></div>";
	        $(html).insertAfter("div.gallery");
        }

        $(html).insertAfter("div.gallery");

        $("div.additional").disableSelection(); 

        $("div.gallery-navigation a").live("click", function () {

            if ($(this).hasClass("next")) {
                
                if (_position > _positionTotal)
                {
                    $("div.gallery ul.thumbnails").animate({
                        left: "-=106px",
                    }, 300, function() {
                        RenderPaging(page, totalPages);
                    });

                    _position = _position-106;
                    page++;
                }

            } else {

                if (_position < 0)
                {
                    $("div.gallery ul.thumbnails").animate({
                        left: "+=106px",
                    }, 300, function() {
                        RenderPaging(page, totalPages);
                    });

                    _position = _position+106;
                    page--;
                }
                
            }

            return false;

        });
	}
	
	$('.image-button:first').click();

	//Controlling the text size
	$('.text-size').click(function() {
		$('.text-size').removeClass('active');
		$(this).addClass('active');
		$.cookie('text-size',null);
		$.cookie(
			'text-size', 
			$(this).attr('rel'), {
				expires: 7, 
				path: '/'
			}
		);
		resize_text($(this).attr('rel'));
		return false;
	});
	
	
	//JS-Slider Toggle (Accordion)
	$('.js-slider div').hide();
	$('.js-slider h3 a').click(function() {
		$(this).parent().next().toggle('fast');
		return false;
	});
});

//Changing the text size of the whole page
function resize_text(size) {
	if (size == 'normal') {
		$('#wrapper').css('font-size','1em');
		$('ul.tweet_list li:last-child').show();
		$('.text-size').removeClass('active');
		$('.text-size.normal').addClass('active');
	} else if (size == 'large') {
		$('#wrapper').css('font-size','1.1em');
		$('ul.tweet_list li:last-child').hide();
		$('.text-size').removeClass('active');
		$('.text-size.large').addClass('active');
	} else if (size == 'extra') {
		$('#wrapper').css('font-size','1.2em');
		$('ul.tweet_list li:last-child').hide();
		$('.text-size').removeClass('active');
		$('.text-size.extra').addClass('active');
	}
}

function resize(additional) {	

	$("div.pages-content").css({ "height":"auto" });
	$("div.pages-content div.tiny").css({ "height":"auto" });
	
	/* Increase page size of content area */
	var contentsize = ($("div.pages-content div.tiny").height() + 16);
	var divided = Math.ceil(contentsize / 230);

	if (divided > 0) {
	    $("div.pages-content").height((divided * 230));
	    $("div.pages-content div.tiny").height($("div.pages-content").height() - 31);
	}
}

function RenderPaging(page, totalpages) {

    if (page >= totalpages) {
        $("div.gallery-navigation a.next").hide();
        $("div.gallery-navigation a.previous").show();
    } else if (page <= 1) {
        $("div.gallery-navigation a.next").show();
        $("div.gallery-navigation a.previous").hide();
    } else {
        $("div.gallery-navigation a.next").show();
        $("div.gallery-navigation a.previous").show();
    }

}

jQuery.fn.extend({ 
        disableSelection : function() { 
                this.each(function() { 
                        this.onselectstart = function() { return false; }; 
                        this.unselectable = "on"; 
                        jQuery(this).css('-moz-user-select', 'none'); 
                }); 
        } 
});
