function overlay() {
    jQuery.fn.colorbox({
        width: '870px',
        height: '660px',
        inline: true, 
        href: "#splashy"  
    });
}

function photoset_openphoto(postid, photoid) {
        
    jQuery.fn.colorbox({
        width: '870px',
        height: '660px',
        href:'/wp-content/plugins/flickrgallery/?postid='+postid + '&photoid=' + photoid,
        onComplete: function () {
            swfobject.switchOffAutoHideShow();
            swfobject.registerObject("photoviewer_flash", "10.0", "/wp-content/plugins/flickrgallery/expressInstall.swf");
        }
    }); 
}

function photoset_open(postid) {
    
    jQuery.fn.colorbox({
		iframe: true,
        width: '870px',
        height: '660px',
        href:'/flickr/?postid='+postid,
        onComplete: function () {
        }
    });
	
    
}
   
function close_flash() {

	createCookie('noflash','true', false);

	jQuery('#main').animate({ top: '-=335'}, 300, function () {
		jQuery('#flash').hide();
	});
	
}


function equalColumns(){

    //we need this height as blueprint
    var newsHeight = jQuery('#news').height();     
    
    //space above the event column
    //var coolestHeight = jQuery('#coolest').height(); 
    var coolestHeight = jQuery('#best-job').height(); 
    var gutter = parseFloat(jQuery('.column #events').css('margin-top'));
    var eventPadding = 60;
    
    // space above starbeach tv column
    //var flashHeight = 230;
    var flashHeight = -20;   
    var tvMargin = 80;
    
    //console.log('newsheight: '+newsHeight);
   // console.log('coolestheight: '+coolestHeight);            
                        
    //do the math johnny
    var eventsHeight = newsHeight - coolestHeight - gutter; 
    var starbeachtvHeight = newsHeight - flashHeight - tvMargin;
 
    //apply the math
    jQuery('.column #events').height(eventsHeight); 
    jQuery('.event-scroll').height(eventsHeight-eventPadding);
    jQuery('.starbeachtv-scroll').height(starbeachtvHeight);
    
    jQuery('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 11});
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

jQuery.fn.labelToggle = function() {

	return this.each(function () {
		
		jQuery(this).focus(function () {
			if ( !jQuery(this).attr('data-label') ) {
				jQuery(this).attr('data-label', jQuery(this).val() );
			}

			if ( jQuery(this).val() == jQuery(this).attr('data-label') ) {
				jQuery(this).val('')
			}
			

		});
		
		jQuery(this).blur(function () {
			if ( jQuery(this).val() == '' && jQuery(this).attr('data-label') ) {
				 jQuery(this).val( jQuery(this).attr('data-label') ) 
			}
		});
		
	});
	
};



jQuery(document).ready(function($) {
    
    //webkit has to do this function on onload
    if ( $.browser.safari == false){  
        equalColumns();
    }
    
    $('.clickable').each(function () {
        
        $(this).css('cursor','pointer');
        $(this).click(function () {
            
            dest = $('a', $(this)).attr('href');

            window.location=dest;


        });
        
    });

    
    $('#partnermenu a img').click(function() {
    
    $('#partnermenu a img').each(function(index) {
            
            var array = $(this).attr('name').split(',');
        
        $(this).attr({
                'src': array[0],
                'class': 'normal'
           }); 
        });
        
        var array = $(this).attr('name').split(','); 
               
        $(this).attr({
                'src': array[1],
                'class': 'selected'
        });
    });


	$('input.label,textarea.label').labelToggle();
    
    //ie needs image for rounded corners and needs a background color
    if($.browser.msie){             
        $('#news').corner('cc:#1B1713');
        $('#footer').corner('top cc:#1B1713');
        $('#coolest').corner('top bl cc:#1B1713'); 
        $('#events').corner('cc:#1B1713');
        $('#starbeachtv-home').corner('cc:#1B1713');      
    }else{
        $('#news').corner();
        $('#footer').corner('top');
        $('#coolest').corner('top bl'); 
        $('#events').corner();
        $('#starbeachtv-home').corner();    
    }
	    

	//$('.column #events').height(((($('.news-intro').position().top + 85) + $('.news-intro').height()) - ($('.column #events').position().top + 85)) -38);
    
    $("#connect img").hover(
         function()
         {
            this.src = this.src.replace("_normal","_over");
         },
         function()
         {
          this.src = this.src.replace("_over","_normal");
         }
        );
        
        
    /* Wallpaper download */
    
    $('a.wallpaper').click(function () { 
    
        $.fn.colorbox( { width:"400px", inline:true, href:"#lightbox"} );
    
        return false;
    });  
    
    
    
        $('#register_form').submit(function() {
        
        $(".error").hide();
        
        
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailToVal = $("#vote_email").val();
        if(emailToVal == '') {
            $("#vote_email").after('<span class="error">This field is required.</span>');
            hasError = true;
        } else if(!emailReg.test(emailToVal)) {
            $("#vote_email").after('<span class="error">Please enter a valid e-mail address.</span>');
            hasError = true;
        }
        
        var messageVal = $("#vote_name").val();
        if(messageVal == '') {
            $("#vote_name").after('<span class="error">This field is required.</span>');
            hasError = true;
        }
        
        var messageVal = $("#vote_age").val();
        if(messageVal == '') {
            $("#vote_age").after('<span class="error">This field is required.</span>');
            hasError = true;
        }
        
        var gender = $("input[name='gender']:checked").val();
        if ( gender != 'male' && gender != 'female' ) {
            $("#vote_gender_error").after('<span class="error">This field is required.</span>');
            hasError = true;
        }
        
        var optin = $("input[name='opt-in']").attr('checked')
        if ( !optin ) {
            $("#vote_optin_error").after('<span class="error">This field is required.</span>');
            hasError = true;
        }

        if(hasError == false) {
                            
            $.ajax({
                type: 'POST',
                url: "/register.php",
                dataType: 'text',
                data:  $(this).serialize(),
                success: function (data) {
                    
                   // $.fn.colorbox.close();
                   
                    $("#hide_this_thing").hide(); 
                    $("#flashknopdiv").show();
                                       
                },
                error: function () {
                    alert('Something went wrong');
                }
                
            });

            
        }

        return false;
    });
    
     
	
    /* dj vote page */
	
	$('a.vote').click(function () {
	
	
		$('span#vote_dj_name').text( $(this).attr('data-name') );
		$('input#vote_post_id').val( $(this).attr('data-id') );
		$('input#vote_secret').val( $(this).attr('data-secret') );
	
		$.fn.colorbox( { width:"400px", inline:true, href:"#lightbox"} );
	
		return false;
	});

    /* DJ-contest vote */
   	$('#vote_dj_form').submit(function() {
		
		$(".error").hide();
		
		post_id = $("input#vote_post_id").val();

		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var emailToVal = $("#vote_email").val();
		if(emailToVal == '') {
			$("#vote_email").after('<span class="error">This field is required.</span>');
			hasError = true;
		} else if(!emailReg.test(emailToVal)) {
			$("#vote_email").after('<span class="error">Please enter a valid e-mail address.</span>');
			hasError = true;
		}
		
		var messageVal = $("#vote_name").val();
		if(messageVal == '') {
			$("#vote_name").after('<span class="error">This field is required.</span>');
			hasError = true;
		}
		
		var messageVal = $("#vote_age").val();
		if(messageVal == '') {
			$("#vote_age").after('<span class="error">This field is required.</span>');
			hasError = true;
		}
		
		var gender = $("input[name='gender']:checked").val();
		if ( gender != 'male' && gender != 'female' ) {
			$("#vote_gender_error").after('<span class="error">This field is required.</span>');
			hasError = true;
		}
		
		var optin = $("input[name='opt-in']").attr('checked')
		if ( !optin ) {
			$("#vote_optin_error").after('<span class="error">This field is required.</span>');
			hasError = true;
		}

		if(hasError == false) {
							
			$.ajax({
				type: 'POST',
				url: "/vote.php",
				dataType: 'text',
				data:  $(this).serialize(),
				success: function (data) {
					
					$.fn.colorbox.close();
					
					if (data == 'Your vote is received.') {
						count = parseInt( $('#' + post_id + 'votes').html() );
						
						count++;
						$('#' + post_id + 'votes').html( count++ );
					}

					alert(data);
					
				},
				error: function () {
					alert('Something went wrong');
				}
				
			});

			
		}

		return false;
	});
    
    
	$('#vote_form').submit(function() {
		
		$(".error").hide();
		
		post_id = $("input#vote_post_id").val();

		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var emailToVal = $("#vote_email").val();
		if(emailToVal == '') {
			$("#vote_email").after('<span class="error">This field is required.</span>');
			hasError = true;
		} else if(!emailReg.test(emailToVal)) {
			$("#vote_email").after('<span class="error">Please enter a valid e-mail address.</span>');
			hasError = true;
		}
		
		var messageVal = $("#vote_name").val();
		if(messageVal == '') {
			$("#vote_name").after('<span class="error">This field is required.</span>');
			hasError = true;
		}
		
		var messageVal = $("#vote_age").val();
		if(messageVal == '') {
			$("#vote_age").after('<span class="error">This field is required.</span>');
			hasError = true;
		}
		
		var gender = $("input[name='gender']:checked").val();
		if ( gender != 'male' && gender != 'female' ) {
			$("#vote_gender_error").after('<span class="error">This field is required.</span>');
			hasError = true;
		}
		
		var optin = $("input[name='opt-in']").attr('checked')
		if ( !optin ) {
			$("#vote_optin_error").after('<span class="error">This field is required.</span>');
			hasError = true;
		}

		if(hasError == false) {
							
			$.ajax({
				type: 'POST',
				url: "/vote.php",
				dataType: 'text',
				data:  $(this).serialize(),
				success: function (data) {
					
					$.fn.colorbox.close();
					
					if (data == 'Your vote is received.') {
						count = parseInt( $('#' + post_id + 'votes').html() );
						
						count++;
						$('#' + post_id + 'votes').html( count++ );
					}

					alert(data);
					
				},
				error: function () {
					alert('Something went wrong');
				}
				
			});

			
		}

		return false;
	});
    
    /* vote producer */
    
    $('a.vote_producer').click(function () {
    
    
        $('span#vote_producer_name').text( $(this).attr('data-name') );
        $('input#vote_post_id').val( $(this).attr('data-id') );
        $('input#vote_secret').val( $(this).attr('data-secret') );
    
        $.fn.colorbox( { width:"400px", inline:true, href:"#lightbox"} );
    
        return false;
    });

    $('#vote_producer_form').submit(function() {
        
        $(".error").hide();
        
        post_id = $("input#vote_post_id").val();

        
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailToVal = $("#vote_email").val();
        if(emailToVal == '') {
            $("#vote_email").after('<span class="error">This field is required.</span>');
            hasError = true;
        } else if(!emailReg.test(emailToVal)) {
            $("#vote_email").after('<span class="error">Please enter a valid e-mail address.</span>');
            hasError = true;
        }
        
        var messageVal = $("#vote_name").val();
        if(messageVal == '') {
            $("#vote_name").after('<span class="error">This field is required.</span>');
            hasError = true;
        }
        
        var messageVal = $("#vote_age").val();
        if(messageVal == '') {
            $("#vote_age").after('<span class="error">This field is required.</span>');
            hasError = true;
        }
        
        var gender = $("input[name='gender']:checked").val();
        if ( gender != 'male' && gender != 'female' ) {
            $("#vote_gender_error").after('<span class="error">This field is required.</span>');
            hasError = true;
        }
        
        var optin = $("input[name='opt-in']").attr('checked')
        if ( !optin ) {
            $("#vote_optin_error").after('<span class="error">This field is required.</span>');
            hasError = true;
        }

        if(hasError == false) {
                            
            $.ajax({
                type: 'POST',
                url: "/vote_producer.php",
                dataType: 'text',
                data:  $(this).serialize(),
                success: function (data) {
                    
                    $.fn.colorbox.close();
                    
                    if (data == 'Your vote is received.') {
                        count = parseInt( $('#' + post_id + 'votes').html() );
                        
                        count++;
                        $('#' + post_id + 'votes').html( count++ );
                    }

                    alert(data);
                    
                },
                error: function () {
                    alert('Something went wrong');
                }
                
            });

            
        }

        return false;
    }); 

 });  
 
jQuery(window).load(function($) {
        equalColumns();    
});

