
/* Event Handling */

Event.observe(window, 'load', function() {

        // slide show
        if($('next_work')){
            $('next_work').observe('click', function(e){
                    stop_slideshow();
                    slide_right();
                    e.stop();
            });

            $('prev_work').observe('click', function(e){
                    stop_slideshow();
                    slide_left();
                    e.stop();
            });

            setTimeout('play_slideshow()', 10000)
        }

        // club profile image swap
        $$('.club_prof_thumb').each(function(el){
            el.observe('click', function(e){
                $('main_img').src = el.href;
                $$('.club_prof_thumb').each(function(ele){
                    $('img_'+ele.id).removeClassName('active');
                });
                $('img_'+el.id).addClassName('active');
                e.stop();
            });
        });

});

/* functions */

var slideTimer = null;
var finished = true;

function play_slideshow() {
	slide_right();
	slideTimer = setTimeout('play_slideshow()', 7500);
}

function stop_slideshow() {
	clearTimeout(slideTimer);
}

function slide_right() {
	if(finished){
		finished = false;

		var xpos = $('slider').style.left;
		if(xpos != '-2712px') {
			new Effect.Move($("slider"), { x: -678, mode: 'relative' });
		} else {
			new Effect.Move($("slider"), { x: 0, mode: 'absolute' });
		}
		setTimeout('finished = true', 1000);
	}
}

function slide_left() {
	if(finished){ // Giving previous animation time to finish before running again, out of sync.
		finished = false;

		var xpos = $('slider').style.left;
		if(xpos != '0px' && xpos != '') {
			new Effect.Move($("slider"), { x: 678, mode: 'relative' });
		} else {
			new Effect.Move($("slider"), { x: -2712, mode: 'absolute' });
		}

		setTimeout('finished = true', 1000);
	}
}

/*
 *  Club Rating AJAX
 */

function rateClub(rating, club_id) {
        $('rating_controls').innerHTML = '...';
	var url = '/_ajax.rate.php';
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: 'rating='+rating+'&type=club&type_id='+club_id, onComplete: updateRating});
}

function updateRating (originalRequest) {
	var newData = originalRequest.responseText;
	if (newData === 'Already Voted') {
		alert('We have already recieved a vote from you for this club.  Thanks for voting!');
	} else {
            $('rating').innerHTML = newData;
            $('rating').style.width = newData;
            $('rating_controls').innerHTML = 'Thanks for your vote!';
	}
}

/*
 *  Comments
 */

// RATE

function rateComment(rating, comment_id, user_id) {
	var url = '/_ajax.rate.php';
	var myAjax = new Ajax.Request(url, {
		method: 'get',
		parameters: 'comment_rating='+rating+'&comment_id='+comment_id,
		onComplete: updateCommentRating
	});
}

function updateCommentRating (originalRequest) {
	var newData = originalRequest.responseText;
	if (newData === 'Already Voted') {
		alert('We have already recieved a vote from you for this club.  Thanks for voting!');
	} else {
		var data = newData.evalJSON();
		var AffectedDivId = data.div;

		$(AffectedDivId).display = "none";
		$(AffectedDivId).innerHTML = '<b>'+ data.rating +'</b> votes';
		$(AffectedDivId).appear();
	}
}

// REPLY

// create reply box

function openCommentReplyBox(replyCommentID, returnURI){
	$('reply_'+replyCommentID).innerHTML = '\
		<form method="post" action="/_post_comment_reply.php">\
			Name <input type="text" name="alias" /><br />\
			<input type="hidden" name="comment_id" value="'+ replyCommentID +'" />\
			<input type="hidden" name="return_uri" value="'+ returnURI +'" />\
			<textarea name="comment" rows="5"></textarea><br />\
			<input type="submit" name="add_comment_reply" value="Save Reply" class="button" />\
		</form>';
}

/*
 * 	Guest List
 */

function addGuestToList() {

	// validate email
	var email 	= $('gl_email');
	var filter 	= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (!filter.test(email.value)) {
		alert('Please provide a valid email address');
		email.focus();
		return false;
	}

	// make ajax call
	var url = '/ajax/guest_list.php';
	var pars = $('guest_list_form').serialize();

	var myAjax = new Ajax.Request(url, {
		method: 'get',
		parameters: pars,
		onComplete: updateList
	});
}

function updateList(originalRequest){
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();

	var glButton 	= $('gl_add_button');
	var glUl 		= $('gl_responce_msg');
	var nameInput	= $('gl_name');
	var emailInput 	= $('gl_email');
	var glTitle	= $('gl_title');

	glButton.value = 'Add Another Person';
	nameInput.value = '';
	emailInput.value = '';

	if(glTitle.style.display === 'none'){
		glTitle.appear();
	}

	glLi = document.createElement('li');
	glLi.innerHTML = '<b>'+ data.name +'</b>'+ data.date;
	glUl.appendChild(glLi);
	glUl.show();
	glLi.hide();
	glLi.appear({to: 0.8});

	nameInput.focus();

}

//slideshow
function SlideShow(slideShow){
    this.slideShow = slideShow;
    this.i = 0;
    me = this;

    this.init = function() {
        this.runSlides();
    };

    this.runSlides = function(){
        this.runSlide();
        ((this.i + 1) == this.slideShow.length)? this.i=0: this.i++;
        setTimeout('me.runSlides()', 5000);
    };

    this.runSlide = function(){
        var html;
        html  = '<a href="' + this.slideShow[this.i].link + '">';
        html += '   <div id="slide">';
        html += '   <img src="' + this.slideShow[this.i].image + '" />';
        html += '       <div id="title" style="display: none">' + this.slideShow[this.i].title + '</div>';
        html += '       <div id="sub_title" style="display: none">' + this.slideShow[this.i].subTitle + '</div>';
        html += '   </div>';
        html += '</a>';

        $('slideshow').innerHTML = html;

        new Effect.Move($('slide'),     { x: 0, y: 0, mode: 'absolute', duration: 1.6  });

        $('sub_title').appear({ duration: 3.0 });
        new Effect.Move($('sub_title'), { x: 35, y: 275, mode: 'absolute', duration: 2.25 });

        $('title').appear({ duration: 3.0 });
        new Effect.Move($('title'),     { x: 35, y: 240, mode: 'absolute', duration: 2.4 });
    };
}

// Handles the changing of the days on the events interface.
var marker=0;
function changeDay(id, thumbSrc, linkHref, desc){
    // Change Tab
    elms = document.getElementsByTagName('li');
    for(i=0; i < elms.length; i++) {
        if(elms[i].getAttribute('attr') == 'days'){
            elms[i].setAttribute('class', '');
            elms[i].setAttribute('className', '');
        }
    }
    newDay = document.getElementById('days_'+id);
    newDay.setAttribute('class', 'selected');
    newDay.setAttribute('className', 'selected');

    // Change Day
    elms = document.getElementById('day').childNodes;
    for(i=0; i < elms.length; i++){
        if(elms[i].nodeType == 1){
            if(elms[i].id !== 'featured') elms[i].style.display='none';
        }
    }
    newDay = document.getElementById('day_'+id);
    newDay.style.display='block';

    // Change Thumbnail
    if(thumbSrc){
        changeThumb(thumbSrc, linkHref, desc);
    }
}

function changeThumb(thumbSrc, linkHref, desc){
    if(thumbSrc){
        thumb = document.getElementById('featured_event_img');
        thumb.setAttribute('src', '/uploads/' + thumbSrc);

        link = document.getElementById('featured_event_link');
        link.setAttribute('href', '/city/events/' + linkHref + '/featured event');

        description = document.getElementById('featured_event_desc');
        description.innerHTML = desc;

    }
}

function toggleMult(id){
    var pt2 = $('read_all'+id);
    var btn = $('readAllBtn'+id);

    if (pt2.style.display == 'none'){
        pt2.blindDown();
        btn.innerHTML = 'Less';
    } else {
        pt2.blindUp();
        btn.innerHTML = 'More';
    }
}




/*
 * 	Email Photo
 */

function emailPhoto() {
	
	// validate email
	var email 	= $('i_email');
	var filter 	= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(email.value)) {
		alert('Please provide a valid email address');
		email.focus();
		return false;
	}
	
	// get form info and place in var
	var pars = $('email_photo_form').serialize();
	
	// give ajax progress indicator
	$('email_photo').innerHTML = '<img src="/front_end/images/icons/loading.gif" alt="" />';
	
	// make ajax call
	var url = '/ajax/email_photo.php';
	
	var myAjax = new Ajax.Request(url, {
		method: 'get', 
		parameters: pars, 
		onComplete: updatePhotoForm
	});	
}

function updatePhotoForm(originalRequest){
	$('email_photo').innerHTML = '<img src="/front_end/images/icons/email_sent.png" alt="" />';
}
