// Fix FOUC
//$('html').addClass('js')
if ($.browser.msie && $.browser.version == '6.0') DD_belatedPNG.fix('.sliderStar li img');

function downloadme(x){
myTempWindow = window.open(x,'','left=10000,screenX=10000');
myTempWindow.document.execCommand('SaveAs','null','download.pdf');

}


$(function() {
    // twitter links - fix @ links
    $('.username').each(function(e) {
        $(this).attr('href', 'http://www.twitter.com'+$(this).attr('href'));
    });

	// Font replaces
	Cufon.replace('#slideshow h3');
	Cufon.replace('#nav a');
	Cufon.replace('.column h2');
	Cufon.replace('#email-signup h2');
	Cufon.replace('#event #main h2');
	Cufon.replace('#artist #main h2');
	Cufon.replace('#aside h3');
	Cufon.replace('#questions h3');
	Cufon.replace('#pagination #prev, #pagination #next');
	Cufon.replace('#news-story #main h2 a');
	Cufon.replace('#comments h3 span');
	Cufon.replace('#make-comment h3');
	Cufon.replace('#artists #content h2');
	Cufon.replace('#events #content h2');
	Cufon.replace('#gallery #content h2');
	Cufon.replace('#video-gallery #content h2');
        Cufon.replace('#judges #content h2');
        Cufon.replace('#noslideshow ul a, #noslideshow h1, #inspiration h1, #nice-form h1, #nice-form h2, #judges-list h3, #star-item #page h2, #star-item #page h1, #history h2, .item-title, #flatpage h1.johnson, .cufon, .ornamentish, #noslideshow.homebanner a');
// 	Cufon.replace('#filter-images li a', {hover: true});

	// Slideshow - homepage
	if ($('#home #slideshow').length > 0){
		$('#home #slideshow').after('<div id="slide-nav">').cycle({
		    fx:     'fade',
		    speed:  'slow',
		    timeout: 9000,
		    pager:  '#slide-nav'
		});
	}

	// Slideshow - event page and artist page
	if ($('#event #slideshow ul,#artist #slideshow ul').length > 0){
		$('#event #slideshow ul,#artist #slideshow ul').after('<div id="slide-nav">').cycle({
		    fx:     'fade',
		    speed:  'slow',
		    timeout: 9000,
		    pager:  '#slide-nav'
		});
	}

    $('.sliderStar').cycle({
		    fx:     'fade',
		    speed:  'slow',
		    timeout: 3000
		    
		});

	// Artist carousel
	$('#artist-carousel').jCarouselLite({
        btnNext: '#next',
        btnPrev: '#prev',
        auto: 8000,
		speed: 1000,
		pauseOnHover: true 
    });

	// Latest news scrollbars
	if ($('#latest-news ol').length > 0){
	    $('#latest-news ol').css('height','325px');
		$('#latest-news ol').jScrollPane({showArrows:false, scrollbarWidth: 9, dragMinHeight:19, dragMaxHeight:19 });
	}
    
	// Dealer locator scrollbars
	if ($('#dealer-results').length > 0){
	    $('#dealer-results').css('height','230px');
		$('#dealer-results').jScrollPane({showArrows:false, scrollbarWidth: 9, dragMinHeight:19, dragMaxHeight:19 });
	}

	// Latest Tweets scrollbars
	if ($('#latest-tweets ul').length > 0){
		$('#latest-tweets ul').css('height','360px');
		$('#latest-tweets ul').jScrollPane({showArrows:false, scrollbarWidth: 9, dragMinHeight:19, dragMaxHeight:19 });
	}

	// Modal windows
	//$('#about').jqm({modal: true, trigger: 'a.about'});
	$('#email-signup').jqm({modal: true, trigger: 'a.email-signup'});
	// $('#about,#email-signup')
	$('#email-signup').append('<a href="#" class="jqmClose"><img src="/assets/images/modals/close.png" alt="Close" height="28" width="28"/></a>');

	// Artists list consistent hover effect
	$('#artist-list li a').hover(function() {
		$(this).find('.artist-profile').addClass('img-hover')
	},
	function () {
	    $(this).find('.artist-profile').removeClass("img-hover");
	});

	// ARTIST REGION FILTER
	if ($('#artists').length > 0){

		function filterArtists(nation) {
			// Current class
			$('#artist-region li').removeClass('current');

			// List filter
			$('#artist-list li').each(function(i) {
			  	if (!($(this).hasClass(nation))) {
					$(this).hide()
				} else {
					$(this).show()
				}
			});
			
			// Re-apply padding-right 
			//$('#artist-list li').css('padding-right', '19px')
			//$('#artist-list li:visible').each(function (i) {
			//    if (i % 4 == 0) $(this).css('padding-right', '0');
			//});
		}

		// Insert controls
		$('#artists #content h2').before($([
			'<ul id="artist-region" class="hello">',
			'<li class="belgium"><span>Belgium</span></li>',
			'<li class="denmark"><span>Denmark</span></li>',
		    '<li class="uae"><span>Dubai</span></li>',
            '<li class="france"><span>France</span></li>',
		    '<li class="germany"><span>Germany</span></li>',
            '<li class="greece"><span>Greece</span></li>',
		    '<li class="italy"><span>Italy</span></li>',
			'<li class="portugal"><span>Portugal</span></li>',
		    '<li class="spain"><span>Spain</span></li>',
			'<li class="sweden"><span>Sweden</span></li>',
            '<li class="switzerland"><span>Switzerland</span></li>',
		    '<li class="uk"><span>United Kingdom</span></li>',
			'</ul>'
		].join('')));

		// Hide region controls
		$('#artist-region').hide();

		// Modify title to creat space for button
		$('#artists #content h2').addClass('js-artists-h2');

		// Add button
		$('<div id="find-artist">Find artist by region</div>').insertAfter('#artists #content h2')

		Cufon.replace('#find-artist', {hover: true});

		// EVENT: button clicked
		$('#find-artist').toggle(function(){
		  	$('#artist-region').slideDown('slow')
			$(this).html('Collapse region panel').addClass('open')
			Cufon.replace('#find-artist', {hover: true});
		}, function() {
		  	$('#artist-region').slideUp('slow')
			$(this).html('Find artist by region').removeClass('open')
			Cufon.replace('#find-artist', {hover: true});
		});

		// EVENT: Region link clicked
		$('#artist-region li').toggle(function(e){
			filterArtists($(this).attr('class'))
			$(this).addClass('current');
		}, function() {
			$('#artist-list li').each(function(i) {
				$(this).show();
			});
			$(this).removeClass('current');
		});
	}

	// EVENT LIST FILTER
	if ($('#events').length > 0){

		function filterResults(c) {
			// Current class
			$('#events-by-month li a,#events-by-region li a').removeClass('current');

			// List filter
			$('#event-list li').each(function(index) {
			  	if (!($(this).hasClass(c))) {
					$(this).hide()
				} else {
					$(this).show()
				}
			});
		}

		// Event: Month or Region link clicked
		$('#events-by-month li a,#events-by-region li a').click(function(e){
			e.preventDefault();
			filterResults($(this).attr('class'))
			$(this).addClass('current');
		});

	}

	// IMAGE / VIDEO GALLERY

		$('#gallery #gallery-nav li:nth-child(6n)').css('width','100px')
    	$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'dark_square', opacity: 0.5 });


	
	// Fix FOUC
	
	//$('html').removeClass('js')
    $(".jselect").val('Alberto de Pedro').attr('selected',true);
    $(".jselect").bind('change', function(e){ 
        var href = $(this).attr('value');
        $('#artist-history-bio').load('/artists/history/' + href, function(){
            Cufon.replace('.history-artist h4');
            $(".jselect").val(href).attr('selected',true);
        }); 
    });
    $('.nextbt').live("mousedown",function(e){
        if($('.jselect option:selected').next('option').attr('value')) {

            var href = $('.jselect option:selected').removeAttr('selected').next('option').attr('value');
            if(href) { 
                $('#artist-history-bio').load('/artists/history/' + href, function(){
                    Cufon.replace('.history-artist h4');
                    $(".jselect").val(href).attr('selected',true);
                });                                  
            }
        }
        else {              
            var star = $(".jselect").val('Alberto de Pedro');
            star.attr('selected',true);
            var page = star.attr('value');   
            $('#artist-history-bio').load('/artists/history/' + page, function(){
                    Cufon.replace('.history-artist h4');
                    $(".jselect").val(href).attr('selected',true);
                });
        }    
    });         
     $('.previousbt').live("mousedown",function(e){
        if($('.jselect option:selected').prev('option').attr('value')) {
            var href = $('.jselect option:selected').removeAttr('selected').prev('option').attr('value'); 
            if(href) { 
                $('#artist-history-bio').load('/artists/history/' + href, function(){
                    Cufon.replace('.history-artist h4');
                    $(".jselect").val(href).attr('selected',true)
                });
            }
        }
        else {
            $(".jselect").val('Zosen').attr('selected',true); 
            $('#artist-history-bio').load('/artists/history/zosen', function(){
                Cufon.replace('.history-artist h4');
                $(".jselect").val('zosen').attr('selected',true)
            });
        }
   });
    $("a.close-button").live("click", function(e){
        $(this).parent().toggle();
        e.preventDefault();
    });
    var urlPath = window.location.pathname;
    if (urlPath == '/gallery/events/') {
        $('.changeMe').text('Events Pictures');
        Cufon.replace('.changeMe');
    }

    $('.show-mentor').click(function(e) {
        e.preventDefault();
        $($(this).attr('href')).show();
    });

    $('a.mentor-close-button').click(function(e) {
        $('.mentor-data').hide();
    });
    
    if ($.browser.msie && jQuery.browser.version == '7.0' || jQuery.browser.version == '6.0') {
        $('.two-col-wide #share ul span, #share span').css({
           'margin-top': '8px',
           'cursor':'pointer' 
        });
        $('.entry_detail ul span').css({
           'margin-top': '0px',
           'cursor':'pointer',
           'left': '0px'
        });
        if (jQuery.browser.version == '6.0') {
        $('.entry_detail ul .facebook span').css({
           'margin-top': '8px',
           'cursor':'pointer',
           'left': '0px'
        });
        }
    }
});

