// JavaScript Document

// jQuery fade in fade out
jQuery(function() {
	// OPACITY OF BUTTON SET TO 50%
	jQuery(".fade-img").css("opacity","0.5");
			
	// ON MOUSE OVER
	jQuery(".fade-img").hover(function () {
											  
	// SET OPACITY TO 100%
	jQuery(this).stop().animate({
		opacity: 1.0
		}, "fast");
	},
			
	// ON MOUSE OUT
	function () {	
		// SET OPACITY BACK TO 50%
		jQuery(this).stop().animate({
			opacity: 0.5
			}, "fast");
	});
});
	
// Latest 
jQuery('.set1').cycle({ 
    fx:     'scrollLeft', 
    speed:  'slow', 
    timeout: 0, 
    next:   '#next3', 
    prev:   '#prev3' 
});	
	
// jQuery fade in fade out
jQuery(function() {
	// OPACITY OF BUTTON SET TO 50%
	jQuery(".fade-img-2").css("opacity","0.8");
			
	// ON MOUSE OVER
	jQuery(".fade-img-2").hover(function () {
											  
	// SET OPACITY TO 100%
	jQuery(this).stop().animate({
		opacity: 1.0
		}, "fast");
	},
			
	// ON MOUSE OUT
	function () {	
		// SET OPACITY BACK TO 50%
		jQuery(this).stop().animate({
			opacity: 0.8
			}, "fast");
	});
});



jQuery(document).ready(function(){
	
	jQuery(".toggle-container").hide();

	jQuery(".trigger").click(function(){
		jQuery(this).toggleClass("active").next().slideToggle("slow");
	});
	
	jQuery(".toggle-container-f").hide();

	jQuery(".trigger-f").click(function(){
		jQuery(this).toggleClass("active").next().slideToggle("slow");
	});
	
	// blog
	jFadeInit();	
	jQuery(".post").css({opacity: 0});
	jQuery(".post").fadeTo("slow", 1); 	

	// page image scroller
	jQuery('.slider') 
	.before('<div class="stages">') 
	.cycle({
		fx:     'scrollHorz',
		speed:  'slow', 
		timeout: 10000, 
		pager:  '.stages'
	});

	jQuery("#trackback_switch").click(function(){
		jQuery("#comment_switch").removeClass("comment_switch_active");
		jQuery(this).addClass("comment_switch_active");
		jQuery("#comment_area").animate({opacity: 'hide'}, 0);
		jQuery("#trackback_area").animate({opacity: 'show'}, 1000);
		return false;
	});
	
	jQuery("#comment_switch").click(function(){
		jQuery("#trackback_switch").removeClass("comment_switch_active");
		jQuery(this).addClass("comment_switch_active");
		jQuery("#trackback_area").animate({opacity: 'hide'}, 0);
		jQuery("#comment_area").animate({opacity: 'show'}, 1000);
		return false;
	});

	// setting the tabs in the sidebar hide and show, setting the current tab
	jQuery('div.sb-tabs-wrap ul.sb-posts').hide();
	jQuery('div.sb-tabs-wrap ul.t1').show();
	jQuery('div.sb-tabs-wrap ul.sb-tabs li.t1').addClass('active');

	// SIDEBAR TABS
	jQuery('div.sb-tabs-wrap ul li a').click(function(){
		var thisClass = this.className.slice(0,2);
		jQuery('div.sb-tabs-wrap ul.sb-posts').hide();
		jQuery('ul.' + thisClass).show();
		jQuery('div.sb-tabs-wrap ul.sb-tabs li').removeClass('active');
		jQuery(this).parent().addClass('active');
	});

	jQuery('.close').click(function() {
		jQuery('.alert-msg').fadeOut('slow', function() {
			// Animation complete.
		});
	});
	
	// switch thumbnails in gallery to large version on click
	jQuery('.thumbs li a').click(function() {
		
		jQuery('.maingalimage').attr('src', jQuery(this).attr('rel'));
		jQuery('.shadowimage a').attr('href', jQuery(this).attr('rev'));
		
		jQuery('.maingalimage').attr('alt', jQuery('img', this).attr('alt'));
		jQuery('.maingalimage').attr('title', jQuery('img', this).attr('title'));
		jQuery('.shadowimage a').attr('title', jQuery('img', this).attr('name'));
		
		Shadowbox.clearCache(); 
		Shadowbox.setup(); 
			
	});
	
	var image_thumb = 0;
	var numItems = jQuery('.thumbs li a').length;
	
	jQuery('.nextimage').click(function() {
		
		image_thumb += 1;
		
		if(image_thumb == numItems)
		{
			image_thumb = 0;
		}
		
		// get next image in thumbs image_thumb_i
		//alert(jQuery('.image_thumb_' + image_thumb + ' a').attr('rel'));
		
		jQuery('.maingalimage').attr('src', jQuery('.image_thumb_' + image_thumb + ' a').attr('rel'));
		jQuery('.shadowimage a').attr('href', jQuery('.image_thumb_' + image_thumb + ' a').attr('rev'));
		
		jQuery('.maingalimage').attr('alt', jQuery('img', '.image_thumb_' + image_thumb + ' a').attr('alt'));
		jQuery('.maingalimage').attr('title', jQuery('img', '.image_thumb_' + image_thumb + ' a').attr('title'));
		jQuery('.shadowimage a').attr('title', jQuery('img', '.image_thumb_' + image_thumb + ' a').attr('name'));
		
		Shadowbox.clearCache(); 
		Shadowbox.setup(); 
		
		return false;
			
	});
	
	jQuery('.previmage').click(function() {
		
		image_thumb -= 1;
		
		if(image_thumb == -1)
		{
			image_thumb = numItems-1;
		}
		
		// get next image in thumbs image_thumb_i
		//alert(jQuery('.image_thumb_' + image_thumb + ' a').attr('rel'));
		
		jQuery('.maingalimage').attr('src', jQuery('.image_thumb_' + image_thumb + ' a').attr('rel'));
		jQuery('.shadowimage a').attr('href', jQuery('.image_thumb_' + image_thumb + ' a').attr('rev'));
		
		jQuery('.maingalimage').attr('alt', jQuery('img', '.image_thumb_' + image_thumb + ' a').attr('alt'));
		jQuery('.maingalimage').attr('title', jQuery('img', '.image_thumb_' + image_thumb + ' a').attr('title'));
		jQuery('.shadowimage a').attr('title', jQuery('img', '.image_thumb_' + image_thumb + ' a').attr('name'));
		
		Shadowbox.clearCache(); 
		Shadowbox.setup(); 
		
		return false;
			
	});
	
	
	jQuery(".full-slider").smoothDivScroll();
	
	// clear input on focus
	jQuery('.clearme').focus(function(){
		if(jQuery(this).val()==this.defaultValue){
			jQuery(this).val('');
		}
	});
	
	// if field is empty afterward, add text again
	jQuery('.clearme').blur(function(){
		if(jQuery(this).val()==''){
			jQuery(this).val(this.defaultValue);
		}
	});	
	
	
	// our customers dropdowns 
	jQuery('#hotel-chain-drop li a').click(function() {
		
		jQuery('#hotel-chain').attr('value', jQuery(this).attr('rel'));
		jQuery('input#hotel-chain-input').attr('value', jQuery(this).attr('rel'));
		jQuery('input#hotel-chain-text').attr('value', jQuery(this).html());
		jQuery('.hotel-chain-trigger').toggleClass("active").next().slideToggle("slow");
		jQuery('.hotel-chain-drop').html(jQuery(this).html());
		
		return false;
			
	});
	
	// our customers dropdowns 
	jQuery('#cont-chain-drop li a').click(function() {
		
		jQuery('input#continent-input').attr('value', jQuery(this).attr('rel'));
		jQuery('#cont-chain-input').val(jQuery(this).attr('rel'));
		jQuery('input#continent-text').val(jQuery(this).html());
		jQuery('.cont-chain-trigger').toggleClass("active").next().slideToggle("slow");
		jQuery('.cont-chain-drop').html(jQuery(this).html());
		
		return false;
			
	});
	
	// our customers dropdowns 
	jQuery('#date-drop li a').click(function() {
		
		jQuery('input#date-input').attr('value', jQuery(this).attr('rel'));
		jQuery('#date-input').val(jQuery(this).attr('rel'));
		jQuery('input#date-text').val(jQuery(this).html());
		jQuery('.date-trigger').toggleClass("active").next().slideToggle("slow");
		jQuery('.date-drop').html(jQuery(this).html());
		
		return false;
			
	});
	
	// fade in homepage images
	jQuery(".fade-img").each(function(index) {
		jQuery(this).load().delay(100*index).fadeIn(1000);
	});
	
	jQuery("ul.hoverenlarge li").hover(function() {
		jQuery("ul.hoverenlarge li img").css({'z-index' : '0'});
		jQuery(this).css({'z-index' : '9999'}); /*Add a higher z-index value so this image stays on top*/ 
		jQuery(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
			.animate({
				marginTop: '-120px', /* The next 4 lines will vertically align this image */ 
				marginLeft: '-90px',
				top: '50%',
				left: '50%',
				width: '187px', /* Set new width */
				height: '217px', /* Set new height */
				padding: '0',
				opacity: 1
			}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
	
		} , function() {
		jQuery(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
		jQuery(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
			.animate({
				marginTop: '0', /* Set alignment back to default */
				marginLeft: '0',
				top: '0',
				left: '0',
				width: '75px', /* Set width back to default */
				height: '87px', /* Set height back to default */
				padding: '0',
				opacity: 0.5
			}, 400);
	});
	
	// collections tabs
	// When page loads...
	jQuery(".tab_content").hide(); //Hide all content
	jQuery("ul.tabs li:first").addClass("selected").show(); //Activate first tab
	jQuery(".tab_content:first").show(); //Show first tab content

	//On Click Event
	jQuery("ul.tabs li a").click(function() {

		jQuery("ul.tabs li a").removeClass("selected"); //Remove any "active" class
		jQuery(this).addClass("selected"); //Add "active" class to selected tab
		jQuery(".tab_content").hide(); //Hide all tab content

		var activeTab = jQuery(this).parent().find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		jQuery(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
});

jQuery(function() {
	jQuery("div#controller").jFlow({
		slides: "#slides",
		width: "320px",
		height: "445px"
	});
});

/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jFade
 * Version: 1.0 (Jun 30, 2008)
 * Requires: jQuery 1.2.6+
 *
 *
 * Original Code Copyright (c) 2008 by Michael Leigeber
 * Website: http://www.leigeber.com
 *
 *
 */

(function($) {

	jQuery.fn.jFade = function(options) {
		// merge users option with default options
		var opts = jQuery.extend({}, jQuery.fn.jFade.defaults, options);
		var startrgb,endrgb,er,eg,eb,rint,gint,bint,step;
		var target = this;
	
		//var obj = this;
	
		var init = function() {
			var tgt = target;
			opts.steps = opts.steps || 20;
			opts.duration = opts.duration || 20;
			//clear everything + reset
			clearInterval(tgt.timer);
			endrgb = colorConv(opts.end);
			er = endrgb[0];
			eg = endrgb[1];
			eb = endrgb[2];
		
			if(!tgt.r) {
				//convert to usable rgb value
				startrgb = colorConv(opts.start);
				r = startrgb[0];
				g = startrgb[1];
				b = startrgb[2];
				tgt.r = r;
				tgt.g = g;
				tgt.b = b;
			}
			//process red
			rint = Math.round(Math.abs(tgt.r-er)/opts.steps);
			//process green
			gint = Math.round(Math.abs(tgt.g-eg)/opts.steps);
			//process blue
			bint = Math.round(Math.abs(tgt.b-eb)/opts.steps);
			if(rint == 0) { rint = 1 }
			if(gint == 0) { gint = 1 }
			if(bint == 0) { bint = 1 }
		
			tgt.step = 1;
			tgt.timer = setInterval( function() { animateColor(tgt,opts.property,opts.steps,er,eg,eb,rint,gint,bint) }, opts.duration);
		
			function animateColor(obj,property,steps,er,eg,eb,rint,gint,bint) {
				var tgt = obj;
				var color;
				if(tgt.step <= steps) { // for each loop
					var r = tgt.r;
					var g = tgt.g;
					var b = tgt.b;
					if(r >= er) {
						r = r - rint;
					}
					else {
					r = parseInt(r) + parseInt(rint);
					}
					if(g >= eg) {
						g = g - gint;
					}
					else {
						g = parseInt(g) + parseInt(gint);
					}
					if(b >= eb) {
					b = b - bint;
					}
					else {
						b = parseInt(b) + parseInt(bint);
					}
					color = 'rgb(' + r + ',' + g + ',' + b + ')';
					
					jQuery(obj).css(property, color);
					
					tgt.r = r;
					tgt.g = g;
					tgt.b = b;
					tgt.step = tgt.step + 1;
				}
				else {// last loop
				
					clearInterval(tgt.timer);
					color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
					jQuery(obj).css(property, color);
				}
			}
			
			// convert the color to rgb from hex
			function colorConv(color) {
				//covert 0-2 position hex into decimal in rgb[0]
				//covert 2-4 position hex into decimal in rgb[1]
				//covert 4-6 position hex into decimal in rgb[2]
				var rgb = [parseInt(color.substring(0,2),16),
				parseInt(color.substring(2,4),16),
				parseInt(color.substring(4,6),16)];
				//return array containing rgb[0], rgb[1], rgb[2]
				return rgb;
			}
		};
		if (opts.trigger == "load")
			init();
		else
			jQuery(this).bind(opts.trigger, function(){
				target = this;
				init();
			});
		
		return this;
	};

	jQuery.fn.jFade.defaults = {
		trigger: "load",
		property: 'background',
		start: 'FFFFFF',
		end: '000000',
		steps: 5,
		duration: 30
	};
})(jQuery);

function jFadeInit(){
	// jFade init
	jQuery(".post").jFade({
		trigger: "mouseover",
		property: 'background',
		start: '000000',
		end: '222222',
		steps: 8,
		duration: 8
	}).jFade({
		trigger: "mouseout",
		property: 'background',
		start: '222222',
		end: '000000',
		steps: 8,
		duration: 8
	});
	
}

/*
Author: mg12
Author URI: http://www.neoease.com/
*/
(function() {

function $(id) {
	return document.getElementById(id);
}

function reply(authorId, commentId, commentBox) {
	var author = MGJS.$(authorId).innerHTML;
	var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n|\r\n/g, "") + '</a> \n';

	appendReply(insertStr, commentBox);
}

function quote(authorId, commentId, commentBodyId, commentBox) {
	var author = MGJS.$(authorId).innerHTML;
	var comment = MGJS.$(commentBodyId).innerHTML;

	var insertStr = '<blockquote cite="#' + commentBodyId + '">';
	insertStr += '\n<a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a> :';
	insertStr += comment.replace(/\t/g, "");
	insertStr += '</blockquote>\n';

	insertQuote(insertStr, commentBox);
}

function appendReply(insertStr, commentBox) {
	if(MGJS.$(commentBox) && MGJS.$(commentBox).type == 'textarea') {
		field = MGJS.$(commentBox);

	} else {
		alert("The comment box does not exist!");
		return false;
	}

	if (field.value.indexOf(insertStr) > -1) {
		alert("You've already appended this reply!");
		return false;
	}

	if (field.value.replace(/\s|\t|\n/g, "") == '') {
		field.value = insertStr;
	} else {
		field.value = field.value.replace(/[\n]*$/g, "") + '\n\n' + insertStr;
	}
	field.focus();
}

function insertQuote(insertStr, commentBox) {
	if(MGJS.$(commentBox) && MGJS.$(commentBox).type == 'textarea') {
		field = MGJS.$(commentBox);

	} else {
		alert("The comment box does not exist!");
		return false;
	}

	if(document.selection) {
		field.focus();
		sel = document.selection.createRange();
		sel.text = insertStr;
		field.focus();

	} else if (field.selectionStart || field.selectionStart == '0') {
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var cursorPos = startPos;
		field.value = field.value.substring(0, startPos)
					+ insertStr
					+ field.value.substring(endPos, field.value.length);
		cursorPos += insertStr.length;
		field.focus();
		field.selectionStart = cursorPos;
		field.selectionEnd = cursorPos;

	} else {
		field.value += insertStr;
		field.focus();
	}
}


window['MGJS'] = {};
window['MGJS']['$'] = $;
window['MGJS_CMT'] = {};
window['MGJS_CMT']['reply'] = reply;
window['MGJS_CMT']['quote'] = quote;

})();
