$(document).ready(function(){
    $('#header-slides').cycle({
		fx: 'fade'
	});

    $('.media').media({ width: 328 });

    $('#search input[name="q"]').bind('focus', function(){
        $(this).val('');
    });

    $('#sort').bind('change', function(){
        var uri = $("option:selected", this).val();
        $(location).attr('href', uri);
    });

    $('.lang').bind('click', function(e){
        e.preventDefault();
        var lang = $(this).attr('href');
        var uri = $(location).attr('href');
        
        $.ajax({'url': '/i18n/setlang/',
                'type': 'POST',
                'data': 'language='+lang,
                success: function(response){
                    $(location).attr('href', uri);
                }
            });
    });

    var scroll = $(".scrollable").scrollable({'next': '.next-image',
                                              'prev': '.prev-image',
                                              'api': true
                                             });

    $(".items img").click(function() {
            
	    // see if same thumb is being clicked
	    if ($(this).hasClass("selected")) { return; }
        
	    // calclulate large image's URL based on the thumbnail URL (flickr specific)
	    var url = $(this).attr("alt");
        var title = $(this).attr("title");
        var link = $(this).attr("id");

	    // get handle to element that wraps the image and make it semi-transparent
	    var wrap = $("#image_wrap").fadeTo("medium", 0.5);

	    // the large image from www.flickr.com
	    var img = new Image();

	    // call this function after it's loaded
	    img.onload = function() {

		    // make wrapper fully visible
		    wrap.fadeTo("fast", 1);

		    // change the image
		    wrap.find("img").attr("src", url);

            // Change the title and the download link
            $("h2.title").text(title);
            $(".download").attr("href", link);
	    };

	    // begin loading the image from www.flickr.com
	    img.src = url;

	    // activate item
	    $(".items img").removeClass("selected");
	    $(this).addClass("selected");
        
        // when page loads simulate a "click" on the first image
    });


    $('#nav li a').live('click', function(e){
        var html = $(this).html();
        var _el = $(this);

        if(html.indexOf('<img') != -1 || html.indexOf('<IMG') != -1){
            e.preventDefault();

            var ul = $(this).parent().find('ul');
			
            if(_el.hasClass('subnav')){
                $('ul.expand').removeAttr('style').removeClass('expand');
                _el.removeClass('subnav');
            } else {
                ul.css({'padding': '0', 'top': '42px', 'min-width': '90px' }).addClass('expand');
                _el.addClass('subnav');
            }
        }
    });

    $('body').bind('click', function(){
        $('ul.expand').removeAttr('style').removeClass('expand');
    });

    if($('.register-payment').length > 0){
        $('.register-payment').trigger('submit');
    }
});

function set_offset(offset){
    var scroll = $(".scrollable").scrollable({'next': '.next-image',
                                              'prev': '.prev-image',
                                              'api': true
                                             });
    scroll.move(offset);
}

$(function() {
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
});




