//browser select
var css_browser_selector = function(){
    var ua = navigator.userAgent.toLowerCase(), is = function(t){
        return ua.indexOf(t) != -1;
    }, h = document.getElementsByTagName('html')[0], b = (!(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('gecko/') ? 'gecko' : is('opera/9') ? 'opera opera9' : /opera (\d)/.test(ua) ? 'opera opera' + RegExp.$1 : is('konqueror') ? 'konqueror' : is('applewebkit/') ? 'webkit safari' : is('mozilla/') ? 'gecko' : '', os = (is('x11') || is('linux')) ? ' linux' : is('mac') ? ' mac' : is('win') ? ' win' : '';
    var c = b + os + ' js';
    h.className += h.className ? ' ' + c : c;
}();

//IE png fix with background-repeat and background-position
document.write('<!--[if IE 6]><script type="text/javascript">DD_belatedPNG.fix(\'div, span, a\');</script><![endif]-->');

//custom fonts
Cufon.replace(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);
Cufon.replace(['.box21 h3'], {textShadow: '#FFFFFF 2px 2px'});

//interactive map
swfobject.embedSWF("/swf/3Dmap.swf", "interactiveMap", "910", "570", "9.0.0");

//superBanners
swfobject.embedSWF("/swf/superBanners.swf", "superB", "910", "180", "9.0.0");

//global functions
function autoHeight(){
    jQuery('#fullPic img').hide().load(function(){
        jQuery(this).fadeIn('slow');
        jQuery('#fullPic').removeClass().stop().animate({
            height: jQuery('#fullPic img').height()
        }, 2000);
    });
};

//open swf files in lightbox
function createSWF(target, swfFile, targetId, swfWidth, swfHeight, flashVersion){
    removeSWF();
    jQuery('body').append('<div id="playerOverlay" style="height:' + screen.height + 'px"></div><div id="playerWrap"><div id="' + targetId + '"></div></div>');
    var flashvars = {
        file: target,
        autostart: "true"
    };
    var params = {
        allowfullscreen: "true",
        allowscriptaccess: "always",
        wmode: "transparent"
    };
    swfobject.embedSWF(swfFile, targetId, swfWidth, swfHeight, flashVersion, false, flashvars, params);
};

function removeSWF(){
    jQuery('#playerOverlay, #playerWrap').remove();
};
jQuery(document).click(function(e){
    if (e.target.nodeName.toLowerCase() != 'object') {
        removeSWF();
    }
});

jQuery(document).keydown(function(e){
	var key = e.which || window.event.keycode;
	if(key == 27){
		removeSWF();
	}
})

jQuery(document).ready(function(){
    //more upload
    var upload_number = 1;
    jQuery('a#moreUploads').click(function(){
        var newUpload = '<li><label for="attachment' + upload_number + '">Select Picture</label><input type="file" id="attachment' + upload_number + '" name="attachmentWork[]" size="30" /></li>';
        jQuery(newUpload).insertBefore(jQuery(this).parent());
        upload_number++;
        return false;
    });
    
    //custom selects
    jQuery('.select1 select, .select2 select').each(function(){
        var element = jQuery(this);
        element.change(function(){
            element.prev('span').text(element.children('option:selected').text());
        });
    });
    
    //external links
    jQuery('a[rel="external"]').attr('target', '_blank');
    
    //star rater
    jQuery('.auto-submit-star').rating({
        callback: function(value, link){
            jQuery('body').addClass('wait');
            jQuery.post(this.form.action, {
                'ratedItemId': this.form.ratedItemId.value,
                'itemRating': value
            }, function(){
                jQuery('body').removeClass('wait');
            });
            jQuery('input', this.form).rating('disable');
            return false;
        }
    });
    
    //filters
    jQuery('#eventFilter').each(function(){
        jQuery(this).change(function(){
            if (jQuery(this).children('option:selected').val() != '') {
                window.location = jQuery(this).children('option:selected').val();
            };
                    });
    });
    
    //tabs
    var openingTimesTabs = jQuery('#openingTimesBox .bot').tabs({});
    //openingTimesTabs.tabs('rotate', 10000, true);
    
    var eventTabs = jQuery('#eventsCategoriesBox .bot').tabs({
        show: function(event, ui){
            if (jQuery('li.ui-tabs-selected').attr('id') == 'kidsLi') {
                jQuery(this).parents('.box').removeClass('box2 box3').addClass('box4');
            }
            else 
                if (jQuery('li.ui-tabs-selected').attr('id') == 'specialLi') {
                    jQuery(this).parents('.box').removeClass('box2 box4').addClass('box3');
                }
                else 
                    if (jQuery('li.ui-tabs-selected').attr('id') == 'familyLi') {
                        jQuery(this).parents('.box').removeClass('box3 box4').addClass('box2');
                    }
        }
    });
    eventTabs.tabs('rotate', 10000, true);
    
    jQuery('#kidsLi a').click(function(){
        jQuery(this).parents('.box').removeClass('box2 box3').addClass('box4');
    });
    jQuery('#specialLi a').click(function(){
        jQuery(this).parents('.box').removeClass('box2 box4').addClass('box3');
    });
    jQuery('#familyLi a').click(function(){
        jQuery(this).parents('.box').removeClass('box3 box4').addClass('box2');
    });
    
    //date picker
    jQuery('#birth').datepicker({
        minDate: null,
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
        yearRange: '1900:2010'
    });
    
    jQuery('input[type="text"].datePicker').datepicker({
        minDate: 0
    });
    
    //gallery
    jQuery('.photos li:first-child img').css({
        opacity: 0.5
    });
    jQuery('.photos li a').each(function(){
        jQuery(this).hover(function(){
            if (jQuery('#fullPic img').attr('src') !== jQuery(this).attr('rel')) {
                jQuery('#fullPic').addClass('loading');
                autoHeight();
                jQuery('.photos li img').css({
                    opacity: 1
                });
                jQuery(this).children('img').css({
                    opacity: 0.5
                });
                jQuery('#fullPic').attr('href', jQuery(this).attr('href')).attr('title', jQuery(this).attr('title'));
                jQuery('#fullPic img').attr('src', jQuery(this).attr('rel'));
            }
        }, function(){
        });
    });
    
    //lightbox
    jQuery('#fullPic, .photos a, a[rel*="lightbox"]').lightBox({});
    
    //print links
    jQuery('a.print').each(function(){
        var printTarget = jQuery(this).attr('href');
        jQuery(this).click(function(){
            window.open(printTarget).print();
            return false;
        });
    });
    
    //news ticker
    jQuery('#newsTicker').cycle({
        fx: 'fade',
        speed: 800
    });
    
    //rides slideshow
    jQuery('#welcomeBox dt div').cycle({
        fx: 'fade',
        speed: 4000
    });
    
    //events slideshow
    jQuery('#featuredEvents').jCarouselLite({
        visible: 4,
        auto: 2000,
        speed: 3000
    });
    
    //banners slideshow
    jQuery('#slideShow').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 6000
    });
    
    //wristbands slideshow
    jQuery('.wristbandsBox').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 6000
    });
	
	//gig guide
    jQuery('#guide-slide').before('<a href="#" id="prev-gig"></a><a href="#" id="next-gig"></a>').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 8000,
		prev: '#prev-gig',
		next: '#next-gig'
    });
    
    //clear inputs
    jQuery('#keyword').each(function(){
        jQuery(this).focus(function(){
            if (jQuery(this).val() == this.defaultValue) {
                jQuery(this).val('');
            }
        });
        jQuery(this).blur(function(){
            if (jQuery(this).val() == '') {
                jQuery(this).val(this.defaultValue);
            }
        });
    });
    
    //validate booking form
    jQuery('#bookWristbandsForm').validate({
        rules: {
            name: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            },
            phone: {
                required: true
            },
            bookingDate: {
                required: true,
                date: true
            },
            areas: {
                required: true,
                minlength: 2
            }
        },
        messages: {
            name: {
                required: "Please enter a name",
                minlength: "Your name must consist of at least 2 characters"
            },
            email: "Please enter a valid email address",
            phone: "Please enter your phone number",
            bookingDate: "Please enter a booking date"
        }
    });
    jQuery('#otherAreas').click(function(){
        jQuery('#aditionalAreasWrap').toggle().children('textarea').focus();
    });
    
    //validate send to friend form
    jQuery('#referFriendForm').validate({
        rules: {
            name: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            },
            friendName: {
                required: true,
                minlength: 2
            },
            friendEmail: {
                required: true,
                email: true
            },
            message: {
                required: true,
                minlength: 2
            }
        },
        messages: {
            name: {
                required: "Please enter your name",
                minlength: "The name must consist of at least 2 characters"
            },
            friendName: {
                required: "Please enter your friend's name",
                minlength: "The name must consist of at least 2 characters"
            },
            email: "Please enter a valid email address",
            friendEmail: "Please enter a valid email address",
            message: "Please enter your message"
        }
    });
    
    //validate login form
    jQuery('#loginForm').validate({
        rules: {
            user: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            user: "Please enter your username",
            password: "Please enter your password"
        }
    });
    
    //validate recover pass form
    jQuery('#recoverForm').validate({
        rules: {
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            email: "Please enter a valid email address"
        }
    });
    
    //validate register pass form
    jQuery('#registerForm').validate({
        rules: {
            name: {
                required: true,
                minlength: 2
            },
            user: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 4
            },
            confirmPass: {
                required: true,
                minlength: 4,
                equalTo: "#password"
            },
            phone: {
                required: true
            },
            address: {
                required: true
            }
        },
        messages: {
            name: {
                required: "Please enter your  name",
                minlength: "The name must consist of at least 2 characters"
            },
            user: {
                required: "Please enter your user name",
                minlength: "The user name must consist of at least 2 characters"
            },
            email: "Please enter a valid email address",
            password: {
                required: "Please enter a password",
                minlength: "The password must be at least 4 characters long"
            },
            confirmPass: {
                required: "Please enter a password",
                minlength: "The password must be at least 4 characters long",
                equalTo: "Please enter the same password as above"
            },
            phone: "Please enter your phone number",
            address: "Please enter your address"
        }
    });
    
    //validate picture upload form
    jQuery('#uploadPictures').validate({
        rules: {
            name: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            },
            picture1: {
                required: true
            }
        },
        messages: {
            name: {
                required: "Please enter your name",
                minlength: "The name must consist of at least 2 characters"
            },
            email: "Please enter a valid email address",
            picture1: "Please upload at least one picture"
        }
    });
    
    //validate comment form
    jQuery('#commentsForm').validate({
        rules: {
            commentName: {
                required: true,
                minlength: 2
            },
            commentEmail: {
                required: true,
                email: true
            },
            commentMessage: {
                required: true
            }
        },
        messages: {
            commentName: {
                required: "Please enter your name",
                minlength: "The name must consist of at least 2 characters"
            },
            commentEmail: "Please enter a valid email address",
            commentMessage: "Please enter your comment"
        }
    });
    
    //vote comment form
    jQuery('.commentVote').each(function(){
        var $form = jQuery(this);
        var $score = jQuery('span.commentScore', $form);
        var $commentId = jQuery('input[name="commentRatingId"]', $form);
        jQuery('button', $form).one('click', function(){
            if (jQuery(this).not('disabled')) {
                jQuery.post($form.attr('action'), {
                    'vote': jQuery(this).val(),
                    'commentId': jQuery($commentId).val()
                });
                jQuery('button', $form).attr('disabled', 'disabled').addClass('disabled');
                $score.text(parseInt($score.text()) + parseInt(this.value));
            }
            else {
                return false;
            };
            return false;
        });
        jQuery('button', $form).click(function(){
            return false;
        });
    });
    
    //video player
    jQuery('.videos a').each(function(){
        jQuery(this).click(function(){
            createSWF(jQuery(this).attr('href'), "/swf/player.swf", "player", "500", "400", "9");
            return false;
        });
    });
    
    //interactive map
    jQuery('a.map').click(function(){
        window.open(this.href, "", "location=0, status=0, scrollbars=0, width=1030,height=700");
        return false;
    });
    
    //daniel open new window pop up
    jQuery('a.applyForm').live('click', function(){
    	newwindow = window.open($(this).attr('href'),'','height=600,width=800,scrollbars=yes');
    	if (window.focus) {newwindow.focus()}
    	return false;
    });

});
