var j = jQuery.noConflict();
j(document).ready(function() {
	
	Shadowbox.init(); //initialise

	j(".media").cycle({
		fx: 'scrollLeft',
		timeout: 7000,
		sync: true,
    	pager:  '.view-promotions-cycle',
    	pagerAnchorBuilder: function(idx, slide) {
			var src = j(slide).eq(0).attr("src");
			var alt = j(slide).eq(0).attr("alt");
			src.replace('1000','207');
			src.replace('260','100');
			return '<li><img src="'+src+'" alt="'+alt+'" width="207" height="100" /></li>';
    	}
	});
	j(".view-promotions-cycle").cycle({
		fx: 'scrollLeft',
		timeout: 7000,
		sync: true,
		slideResize: 1,
		nowrap:false,
		manualTrump:false,
		pager: '.view-promotions-pager',
		onPagerEvent: function(idx, slide) {
			j(".view-promotions-cycle").cycle(idx);
			j(".media").cycle(idx);
		}
	});

	j(".only-at-cycle").cycle({
		fx: 'scrollRight',
		timeout: 7000,
		sync:true
	});

	j(".cycle").cycle({
		fx: 'scrollLeft',
		timeout: 7000,
		sync:true
	});

	j("ul li:last-child").addClass('last-child');
	
	//Promotions and signup slides .sign-up, .view-promotions
	j('.sign-up').click(function(){
		j('.sign-up-hover').slideToggle();	
	});
	j('.view-promotions').click(function(){
		j('.view-promotions-hover').slideToggle();	
	});
	
	j('.career-form-expand').click(function(){
		j('#main-form.career-form').slideToggle();
	});
	
	
	j('.photoLarge:empty').html(function(){
		var fullSize = j('.photoContainer a.photo:first').attr('href');	
		var imgTitle = j('.photoContainer a.photo:first').attr('title');
		var imgTag = '<img align="center" class="photo-fullsize" src="' + fullSize + '" alt="' + imgTitle + '" />';
		j('.photoLarge').html(imgTag);
		j('.photoLarge').delay(500).slideDown();
	});
	j('.photoContainer a.photo').click(function(e){
		e.preventDefault();
		var fullSize = j(this).attr('href');
		var imgTitle = j(this).attr('title');
		var imgTag = '<img class="photo-fullsize" src="' + fullSize + '" alt="' + imgTitle + '" />';
		//$(".class1:not(:empty)").each(function(i){
		j('.photoLarge').slideUp(500,function(){
			j('.photoLarge').html(imgTag);
			j('.photoLarge').delay(250).slideDown(500);
		});
	});

	//Clear Inputs / Textareas that are not of the type 'submit'
	j('input,textarea').each(function() {
		if(j(this).attr('type')!= 'submit'){
			var default_value = this.value;
			j(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			j(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		}
		j(this).hover(function(){j(this).addClass('display');},function(){j(this).removeClass('display');});
	});
	
	j('a.refine').click(function(){
		j('.advanced-search').slideToggle(300);
	});







j('a.play,a.rewind').bind('click', function(e) {
	e.preventDefault();
	var count = 0;
	var pos = -1;
	var $mySound = j(this).attr('title');
	soundManager.onready(function() {
	
		var soundFile = soundManager.createSound({
			usePeakData: true,
			autoLoad: true,
			autoPlay: true,
			id: 'aSound',
			url: $mySound,
			allowPolling: true,
			setPolling: true,
			whileplaying: function() {
				if (count == 0) {
					if (this.position > 1000) {
						this.pause();
						pos = this.position;
						count++;
						this.resume();
					}
				} else if (count == 1) {
					soundManager._writeDebug('old position: ' + pos);
					soundManager._writeDebug('new position: ' + this.position);
					// See that this.position is less than pos!
					count++;
				}
				j('.cur-pos').css('left',(count / this.durationEstimate) * 100 + '%');
			},
			onfinish: function() {
				this.destruct();
			}
		});
	
		j('a.play,a.rewind').bind('click', function() {soundFile.play();});
		j('a.stop,#sb-nav-close').bind('click', function() {soundFile.stop();j('.soundFile .sm2_position').html('00:00');});
		//$('#sb-nav-close').bind('click', function() {soundManager.destroySound('soundFile');});
	
	});
});
	
		// Returns millisecond time to time format of 00:00.
		function time(ms) {
			var seconds = ms / 1000;
			var m = Math.floor(seconds/60);
			var s = Math.floor(seconds - (m * 60));
			if(m<10) {m = "0"+m;}
			if (s<10){s="0"+s;}
			return m+":"+s;
		};
		function writeTime(mt) {
			var seconds = mt;
			j('.sm2_position').html(time(seconds));
		}	

/*
	soundManager.url = '/wp-content/themes/circle_k/scripts/soundmanager/swf/';
	soundManager.debugMode = false;

	soundManager.onready(function(){
		var soundFile = soundManager.createSound({
			flashVersion: 9,
			usePeakData: true,
			autoLoad: true,
			autoPlay: false,
			id: 'aSound',
			volume: 50,
			whileplaying: function() {
				j('.sm2_position').html(time(soundFile.position));
				j('.cur-pos').css('left',(soundFile.position / soundFile.durationEstimate) * 100 + '%');
			}
		});
		j('a.play,a.rewind').bind('click', function() {soundFile.play();});
		j('a.stop,#sb-nav-close').bind('click', function() {soundFile.stop();j('.soundFile .sm2_position').html('00:00');});
		//$('#sb-nav-close').bind('click', function() {soundManager.destroySound('soundFile');});
	});
*/
});
