jQuery.fn.exists = function(){return jQuery(this).length>0;}

function slideshow() {
	$('.banner-slides').cycle({ 
	    fx:     'fade', 
	    prev:   $(this).find('a.prev'),  
	    next:   '.banner-nav a.next',
	    pager:	'.banner-nav #pager'
	});
	$('.banner-nav .pause').click(function() { 
	    $('.banner-slides').cycle('pause'); 
	});
	$('.banner-nav .prev, .banner-nav .next').click(function() { 
	    $('.banner-slides').cycle('resume'); 
	});
};

function thumbnails() {
	$('.thumbnails table tbody').each(function() {
		var p = $(this).parents("div.right");
		$(this).cycle({
			fx: 'scrollHorz',
			timeout: 0,
			next: $('a.project-next', p),
			prev: $('a.project-prev', p)
		});
	});
};

function initMenu() {
  $('.accordion ul ul').hide();
  if($('.accordion .active-trail').exists()){
		$('.accordion ul li.active-trail ul').show();
	} else if(active_project) { //Check if you're passing in an active mlid (menu link id)
		$('.accordion ul li.menu-mlid-'+active_project+' ul').show();
	} else {
		//$('.accordion ul li:first ul').show();
		//$('.accordion ul li:first').addClass("active-trail");
	}
  $('.accordion ul li a').click(
    function() {
			$(".accordion ul li.active-trail").removeClass("active-trail");
			$(this).parent().addClass("active-trail");
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('.accordion ul ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
      }
    }
  );
}

$(function() {
	slideshow();
	thumbnails();
	if($('.accordion').exists()){
		initMenu();
	}
  $('.slideshow-images img').wrap('<div class="slide" />');
	$(".slideshow-images").cycle();
  $(".colorbox").click(function(){
    //hrefid = ;
    //alert($(this).attr("rel"));
	  if($(this).attr("rel")){
			//alert('test');
			$(".colorbox").colorbox({inline:true, href:$(this).attr("rel")});
		} else {
			//alert('test2');
			$(".colorbox").colorbox({inline:false, height: '470px', width: '600px', onComplete:function(){
				$('.cycle').cycle();
			}});
		}
  });
  
  $('#project-photo-slideshow').cycle({ 
	activePagerClass: 'activeSlide',
	fx:     'fade',
	speed:  1000, 
	timeout: 6000, 
	easing: 'jswing',
	next: "#next2",
	prev: '#prev2',
	pager:  '#project-photo-thumbs',
	fastOnEvent: 1000,
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#project-photo-thumbs a:eq(' + idx + ')'; }
	}); 

});


function OpenCoreMotivesWindow() {
    var width = 650;
    var height = 500;
    var left = Math.floor((screen.availWidth - width) / 2);
    var top = Math.floor((screen.availHeight - height) / 2);
    var windowFeatures = "width=" + width + ",height=" + height + 
            ",menubar=no,toolbar=no,scrollbars=yes,resizable=yes," + 
            "left=" + left + ",top=" + top + 
            "screenX=" + left + ",screenY=" + top;
    window.open("http://coremotivesstorageus.blob.core.windows.net/library/6e91613c-1b2f-4312-a58b-c82a4ee16126/248/webforms/c3c202cf-6b76-e011-8dcf-78e7d162dedb.htm", "CoreMotivesWindow", windowFeatures);
}


