document.observe('dom:loaded', function(){
        new ProtoFish('menu-1', '200', 'hover', false, true, true);
});
                            
function productMenu() {
        var floaties = $$('#productMenu a');
		if(floaties==''){return;}
        var transition = function(floaty, xPx) {
                Effect.Appear(floaty);
                new Effect.Move(floaty, {
                        x: xPx, y: 0, mode: 'relative',
                        transition: Effect.Transitions.sinoidal,
                        afterFinish: popOne,
                        duration: 0.5
                });
        };
        var popOne = function() {
                if(floaties.size() > 0) transition(floaties.shift(), 10)
        }
        transition(floaties.shift(), 10);
};

function addModals() {
        //dragable green window
        var content;
        var aryIframe = new Array();
        var window_factory = function(container,options){
                var window_header = new Element('div',{
                        className: 'window_header'
                });
                var window_close = new Element('div',{
                        className: 'window_close'
                });
                var window_contents = new Element('div',{
                        className: 'window_contents'
                });
                
                var offesetLeftFunction = function() {
                        var half = document.viewport.getWidth()/2;
                        return (half - 305);
                }
                var offesetTopFunction = function() {
                        var doc = document.viewport.getScrollOffsets();
                        return (doc.top+50);
                }
                var w = new Control.Modal(container,Object.extend({
                        className: 'window',
                        closeOnClick: window_close,
                        draggable: window_header,
                        insertRemoteContentAt: window_contents,
                        beforeOpen: function(){
                        	if(content != null){
								//alert(content);
								//window_contents.update('');
								}
                        	var nodeList = document.getElementsByTagName('iframe');
                        	if(aryIframe.length > 0){
	                        	for(var i = 0; i < nodeList.length; i++){
	                        		if(nodeList[i].src.toLowerCase() == 'javascript:void(0);'){
	                        			nodeList[i].style.height = aryIframe[i];
	                        		}
	                        	}
                        	}
                        },
                        afterClose: function(){
                        	if(Prototype.Browser.IE)location.reload();
                        	content = window_contents.innerHTML;
                        	//window_contents.update('');
                        	var nodeList = document.getElementsByTagName('iframe');
                        	for(var i = 0; i < nodeList.length; i++){
                        		if(nodeList[i].src.toLowerCase() == 'javascript:void(0);'){
                        			aryIframe[i] = nodeList[i].style.height;
                        			nodeList[i].style.height = '0';
                        		}
                        	}
                        },
                        position: [offesetLeftFunction, offesetTopFunction],
                        fade:true
                },options || {}));
                w.container.insert(window_header);
                window_header.insert(window_close);
                w.container.insert(window_contents);
                return w;
        };
        $$('.press_box a').each(function(a) {
                var mediaItem = window_factory(a);
        });
		$$('.press_box area').each(function(a) {
                var mediaItem = window_factory(a);
        });
        

        //modal window

/*
        $$('.press_box a').each(function(a) {
                var offesetLeftFunction = function() {
                        var half = document.viewport.getWidth()/2;
                        return (half - 305);
                }
                var offesetTopFunction = function() {
                        var doc = document.viewport.getScrollOffsets();
                        return (doc.top+50);
                }
                var window_close = new Element('div',{
                        className: 'window_close'
                });
                var modal = new Control.Modal($(a),{
                        overlayOpacity: 0.75,
                        closeOnClick: window_close,
                        className: 'modal',
                        position: [offesetLeftFunction, offesetTopFunction],
                        fade: true
                });
        });
*/
}

function newsletter() {
        Event.observe('newsletterSubscribe', 'submit', function(event) {
                Event.stop(event);
                if(emailCheck($F('email'))){
                        Effect.Fade('signupUpdate', { afterFinish: function() {
                                new Ajax.Updater('signupUpdate', $('newsletterSubscribe').action, {
                                        method: 'post',
                                        parameters: Form.serialize('newsletterSubscribe'),
                                        onComplete: function() {
                                                Effect.Appear('signupUpdate');
                                        }
                                });
                        }});
                }
        });
        ['click', 'keyup'].each(function(s){
                $('email').observe(s, function() {
                        if($F('email').toLowerCase() == 'email') $('email').value = '';
                });
        });
}

var competitionQuestion = 'Tell us in 10 words or less why you want to win an iPad 2';

function competition() {
        enter();
        Event.observe('competitionEnter', 'submit', function(event) {
                Event.stop(event);
                if(enterCheck()) {
                        new Ajax.Updater('content', $('competitionEnter').action, {
                                method: 'post',
                                parameters: Form.serialize('competitionEnter')
                        });
                        $('content').update('<div class="page competition loading"></div>');
                }
        });
}

function enterCheck() {
        this.msg = new Array();
        var validated = true;
        if($F('CompetitionNameFirst').toLowerCase() =='first name' || $F('CompetitionNameFirst')=='') {
                if($F('CompetitionNameFirst')=='') $('CompetitionNameFirst').value='First Name';
                this.msg.push('*please enter your first name');
                validated = false;
        }
        if($F('CompetitionNameLast').toLowerCase() =='last name' || $F('CompetitionNameLast')=='') {
                if($F('CompetitionNameLast')=='') $('CompetitionNameLast').value='Last Name';
                this.msg.push('*please enter your last name');
                validated = false;
        }
        if(!emailCheck($F('CompetitionEmail'))) {
                if($F('CompetitionEmail')=='') $('CompetitionEmail').value='Email';
                this.msg.push('*please enter your email address');
                validated = false;
        }
        if(!postCodeCheckAu($F('CompetitionPostcode')) && !postCodeCheckNz($F('CompetitionPostcode'))) {
                if($F('CompetitionPostcode')=='') $('CompetitionPostcode').value='Postcode';
                this.msg.push('*please enter your postcode');
                validated = false;
        }
        if($F('CompetitionCustomValue').split(' ').length > 25) {
                this.msg.push('*answer is more than 25 words');
                validated = false;
        }
        if($F('CompetitionCustomValue')==competitionQuestion || $F('CompetitionCustomValue')=='') {
                if($F('CompetitionCustomValue')=='') $('CompetitionCustomValue').value=competitionQuestion;
                this.msg.push('*please answer the question');
                validated = false;
        }
        if(!validated) startFlash();
        return validated;
}

function enter() {
        ['click','keyup'].each(function(e){
                $('CompetitionNameFirst').observe(e,function(){
                    if($F(this).toLowerCase() =='first name') this.value='';
                });
        });
        ['click','keyup'].each(function(e){
                $('CompetitionNameLast').observe(e,function(){
                    if($F(this).toLowerCase() =='last name') this.value='';
                });
        });
        ['click','keyup'].each(function(e){
                $('CompetitionEmail').observe(e,function(){
                    if($F(this).toLowerCase() =='email') this.value='';
                });
        });
        ['click','keyup'].each(function(e){
                $('CompetitionPostcode').observe(e,function(){
                    if($F(this).toLowerCase() =='postcode') this.value='';
                });
        });
        ['click','keyup'].each(function(e){
                $('CompetitionCustomValue').observe(e,function(){
                    if($F(this)==competitionQuestion) this.value='';
                });
        });
}


function contact() {
        inquiry();
        Event.observe('contact', 'submit', function(event) {
                Event.stop(event);
                if(inquiryCheck()) {
                        new Ajax.Updater('content', $('contact').action, {
                                method: 'post',
                                parameters: Form.serialize('contact'),
                                onComplete: contact
                        });
                        $('content').update('<div class="page contact loading"></div>');
                }
        });
}

function inquiryCheck() {
        this.msg = new Array();
        var validated = true;
        if($F('ContactNameFirst').toLowerCase() =='first name' || $F('ContactNameFirst')=='') {
                if($F('ContactNameFirst')=='') $('ContactNameFirst').value='First Name';
                this.msg.push('*please enter your first name');
                validated = false;
        }
        if($F('ContactNameLast').toLowerCase() =='last name' || $F('ContactNameLast')=='') {
                if($F('ContactNameLast')=='') $('ContactNameLast').value='Last Name';
                this.msg.push('*please enter your last name');
                validated = false;
        }
        if(!emailCheck($F('ContactEmail'))) {
                if($F('ContactEmail')=='') $('ContactEmail').value='Email';
                this.msg.push('*please enter your email address');
                validated = false;
        }
        if($F('ContactDayTimePhoneNumber').toLowerCase() =='phone' || $F('ContactDayTimePhoneNumber')=='') {
                if($F('ContactDayTimePhoneNumber')=='') $('ContactDayTimePhoneNumber').value='Phone';
                this.msg.push('*please enter your phone number');
                validated = false;
        }
        if($F('ContactInquiryValue')=='Inquiry' || $F('ContactInquiryValue')=='') {
                if($F('ContactInquiryValue')=='') $('ContactInquiryValue').value='Inquiry';
                this.msg.push('*please enter an Inquiry');
                validated = false;
        }
        if(!validated) startFlash();
        return validated;
}

function inquiry() {
        ['click','keyup'].each(function(e){
                $('ContactNameFirst').observe(e,function(){
                    if($F(this).toLowerCase() =='first name') this.value='';
                });
        });
        ['click','keyup'].each(function(e){
                $('ContactNameLast').observe(e,function(){
                    if($F(this).toLowerCase() =='last name') this.value='';
                });
        });
        ['click','keyup'].each(function(e){
                $('ContactEmail').observe(e,function(){
                    if($F(this).toLowerCase() =='email') this.value='';
                });
        });
        ['click','keyup'].each(function(e){
                $('ContactDayTimePhoneNumber').observe(e,function(){
                    if($F(this).toLowerCase() =='phone') this.value='';
                });
        });
        ['click','keyup'].each(function(e){
                $('ContactInquiryValue').observe(e,function(){
                    if($F(this)=='Inquiry') this.value='';
                });
        });
}

function startFlash() {
    var timeOut = 2000
    var counter = timeOut;
    var arLen=this.msg.length;
    $('formValidateMessage').style.display='block';
    flash();
    for ( var i=1, len=arLen; i<len; ++i ){
                setTimeout("flash()", counter);
                counter=counter+timeOut;
    }
}

function flash() {
    $('formValidateMessage').style.display='none';
    $('formValidateMessage').update(this.msg.pop());
    Effect.Appear('formValidateMessage');
}

function emailCheck (email) {
        emailRegExp=new RegExp (/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        if(emailRegExp.test(email)) return true;
        return false;
}

function postCodeCheckAu (code) {
        postRegExp=new RegExp (/^((0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2}))$/);
        return postRegExp.test(code);
}

function postCodeCheckNz (code) {
        postRegExp=new RegExp (/^([0-9]){4}?$/);
        return postRegExp.test(code);
}

function phoneNumberCheck (number) {
        phoneRegExp=new RegExp (/^(\+\d{2}[ \-]{0,1}){0,1}(((\({0,1}[ \-]{0,1})0{0,1}\){0,1}[2|3|7|8]{1}\){0,1}[ \-]*(\d{4}[ \-]{0,1}\d{4}))|(1[ \-]{0,1}(300|800|900|902)[ \-]{0,1}((\d{6})|(\d{3}[ \-]{0,1}\d{3})))|(13[ \-]{0,1}([\d \-]{5})|((\({0,1}[ \-]{0,1})0{0,1}\){0,1}4{1}[\d \-]{8,10})))$/);
        if(phoneRegExp.test(number)) return true;
        return false;
}

function stateCheck (number) {
        phoneRegExp=new RegExp (/^(NSW|New South Wales|ACT|Australian Capital Teritory|NT|Northern Teritory|QLD|Queensland|VIC|Victoria|SA|South Australia|TAS|Tasmania|WA|Western Australia)$/i);
        if(phoneRegExp.test(number)) return true;
        return false;
}
