
//clearing input fields start
function ClearInput(value, id) {
    var input = document.getElementById(id);
    if (value == input.value) {
        input.value = '';
    }
    else {
        input.value = input.value;
    }
}

function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',o='opera',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$(document).ready(function(){
	//Cufon.replace('', { fontFamily: '' });
	$('#mainContent').supersleight();

	$('#tellAFriend, #tellAFriend2').click(function() {
		_buildLightbox();
        loadContent(this.href);
		//$('#lightboxContentWrap').css('top',(($(window).height()-$('#lightboxContentWrap').height())/2)+'px');
		return false;
	});	
	$('#joinUs1, #joinUs2').click(function(){
	    _buildLightbox();
        loadContent(this.href);
		//$('#lightboxContentWrap').css('top',(($(window).height()-$('#lightboxContentWrap').height())/2)+'px');
		return false;
	
	});
	
});  

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function _buildLightbox() {
    $('body').append('<div id="lightboxOverlay"></div>');
    $('body').append('<div id="lightboxContentWrap" style="top: 120px"><div id="lightboxContent"><a class="close" href="">Close</a><div id="lightboxLoadTarget"></div></div></div>');
	$('#lightboxOverlay').click(function() {
		_destroyLightbox()
	});

	$('#lightboxOverlay').click(function() {
		_destroyLightbox();
		return false;
	});
	$('#lightboxContentWrap').click(function() {
		_destroyLightbox();
		return false;
	});
	$('#lightboxContent').click(function() {
		return false;
	});
	$('#lightboxContent a.close').click(function() {
		_destroyLightbox();
		return false;
	});
	changeheight();
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function _destroyLightbox() {
	$('#lightboxOverlay').remove();
	$('#lightboxContentWrap').remove();
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function loadContent(url) {
    $('#lightboxLoadTarget').html('<iframe src="' + url + '" border="0" frameborder="0" ALLOWTRANSPARENCY="true" width="272px" height="490px" style="background:transparent;" />');
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function changeheight(){
	if (window.innerHeight && window.scrollMaxY) {
		var yWithScroll = window.innerHeight + window.scrollMaxY;
		var lightBoxOverlayHeight = yWithScroll;
	} else {
		//this really needs some attention
		var yWithScroll = document.body.scrollHeight;
		//var yHeight = document.body.clientHeight;
		var lightBoxOverlayHeight = yWithScroll;
	}
	e = MM_findObj("lightboxOverlay");
	e.style.height = lightBoxOverlayHeight + 'px';
	//alert(yWithScroll);
	//alert(e.style.height);
}


//handle tell a friend labels being inside form
$(document).ready(function() {
    $('#tellAFriendPanel input').each(function() {

        if ($(this).val() == "" || $(this).val() == $(this).parent('label').find('span').text()) {
			$(this).val($(this).parent('label').find('span').text()).addClass('defaultText');
        }

        $(this).focus(function() {
            $(this).addClass('focusText');
            if ($(this).val() == $(this).parent('label').find('span').text()) {
                $(this).val("").removeClass('defaultText');
            }
        });
        $(this).blur(function() {
            $(this).removeClass('focusText');
            if ($(this).val() == "") {
                $(this).val($(this).parent('label').find('span').text()).addClass('defaultText');
            }
        });
    });

    $('#tellAFriendButton a').click(function() {
        $('#tellAFriendPanel input').each(function() {
            if ($(this).val() == $(this).parent('label').find('span').text()) {
                $(this).val("")
            }
        });
    });
});

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
(function($) {
    var skipOne = false;
    $.fn.extend({
        indexGallery: function(options) {
            var defaults = {
                controlClass: "controls", controlActive: "active", interval: 3000, fadeOutSpeed: 200, fadeInSpeed: 100, imgSel: 'img', scale: true
            }
            var options = $.extend(defaults, options);

            return this.each(function() {
                var o = options;
                var obj = $(this);
                var images = obj.find(o.imgSel);
                this.activeImage = 1;
                this.ops = o;

                var incId = 1;
                var controlsHtml = '<div class="' + o.controlClass + '">';
                images.each(function() {
                    if (o.scale) {
                        if ($(this).width() > $(this).height()) {
                            $(this).css("width", obj.width() + 'px');
                        }
                        else {
                            $(this).css('height', obj.height() + 'px');
                        }
                    }
                    $(this).addClass("img-" + incId);
                    controlsHtml += '<a href="#gallery-' + incId + '" class="gal-' + incId + '"' + '>' + incId + '</a>';
                    incId++;
                });
                controlsHtml += '</div>';
                obj.append(controlsHtml);
                obj.find('.' + o.controlClass + ' a').click(function() {
                    var gal = $(this).parent().parent();
                    var ops = gal[0].ops;
                    var idx = parseInt($(this)[0].className.replace(/gal-/, ''));
                    if (idx == 1)
                        idx = obj.find(ops.imgSel).length;
                    else
                        idx = idx - 1;

                    obj[0].activeImage = idx;
                    $.indexGalleryProcessTimer(obj);

                    skipOne = true;
                });
                options.timeoutID = window.setInterval(function() {
                    $.indexGalleryProcessTimer(obj);
                }, options.interval);
            });
        }
    });
    $.indexGalleryProcessTimer = function(obj) {
        if (!skipOne) {
            var ops = obj[0].ops;
            var active = obj[0].activeImage;
            var imgs = obj.find(ops.imgSel);
            obj.find("img.img-" + active).fadeOut(ops.fadeOutSpeed);
            active++;
            if (active > imgs.length)
                active = 1;
            obj[0].activeImage = active;
            $(obj).find("img.img-" + active).fadeIn(ops.fadeInSpeed);
            $(obj).find('div.' + ops.controlClass + ' a').removeClass(ops.controlActive);
            $(obj).find('div.' + ops.controlClass + ' a.gal-' + active).addClass(ops.controlActive);
        }
        skipOne = false;
    }
})(jQuery);
