$(document).ready(function() {
    
    bVersion = getInternetExplorerVersion();
    
    //collapse boxes for animate in
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { } else { 
        if(bVersion == 6) { } else {
            $('.contentMain, .imageRotatorContent').css({height: '0px', 'min-height': '0px', overflow: 'hidden'});
            setTimeout(function() { 
                contentMainHeight = $('.contentMainWrapper').height();
                setTimeout("$('.contentMain').animate({height: contentMainHeight}, 350, 'easeOutExpo');", 800);
                $('.imageRotatorContent li:first img').load(function() {
                    $('.imageRotatorContent').animate({height: 279+'px'}, 550, 'easeOutExpo');
                });
            }, 400);            
        }        
    }
    if(bVersion == 6) { } else { 
        setTimeout(function() { 
            $('.rightColumnContentWrapper, .imageRotatorContent').each(function() {
                if($(this).height() == 0) {
                    imageHeight = $('img', this).height();
                    if(imageHeight > 0) {
                        $(this).animate({height: imageHeight}, 550, "swing");
                    }
                }
            });
        }, 1500);
    }
    
    $('ul.primaryNav li a').hover(function () {
	    if($(this).parent().hasClass("selected")) {
	    	
	    } else {
		    $(this).children().css("top","-32px");
	    }
	  }, 
	  function () {
	  	if($(this).parent().hasClass("selected")) {
	  		
	  	} else {
		    $(this).children().css("top","0px");
	  	}
	  }
	);

    
    $('#vacancyRegion').change(function() {
        $('#storeId option:eq(0)').attr('selected', 'selected');
        updateLocationSelect($(this).val());
    });
    $('#storeId').change(function() {
        /*$('#locationSubmit').click();*/
    });
    
    selectGroup = $('#storeId').children();
    
    updateLocationSelect($('#vacancyRegion').val());
        
    $('a.popupLink').bind('click', function(e) { popupWindow($(this).attr('href'), $(this).attr('title').replace(/[^A-z]/ig, ''),'width=550, height=400, scrollbars'); e.preventDefault(); });
    
    /*$('.primaryNav').mouseover(function() {
        $('.level1').show();
    });
    $('.primaryNav').mouseout(function() {
        $('.level1').hide();
    });*/
	
    $('form.vacancyApplicationForm').bind('submit', function(e) {
    	$('input:file').each(function(index) {
    		if($(this).val() != '' && $(this).val() != undefined) {
    			$('.fileUploadContainer').remove();
			}
    	});
    });
    
    $('a.fileUploadClear').bind('click', function (e) {
    	e.preventDefault();
    	// Get the hidden input and clear it's value
    	$(this).parent('.fileUploadContainer').remove();
    });
    
    $('#subscribeHeaderLink').click(function(e) {
        e.preventDefault();
        $('.popupOverlay').stop(true, true);
    	$('.popupContainer').stop(true, true);
        showOverlay();
    });
    
    $('.popupClose, .closeOverlay').click(function(e) {
        e.preventDefault();
        $('.popupOverlay').stop(true, true);
    	$('.popupContainer').stop(true, true);
        closeOverlay();
    });
    
    $('#newsletterSubscribeForm').submit(function() {
    	// Only proceed if newsletterSubscribe is checked
    	if ($('#newsletterSubscribe').is(':not(:checked)')) {
    	    closeOverlay();
    	    return false;
    	}
    	
    	$('#newsletterSubscribeErrors').hide();
    	var formData = $(this).serialize();
    	var formUrl = 'index.php?do=restNewsletterSubscribe';
    	$.get(formUrl, formData, function(data) {
    	    if ($('response', data).attr('status') != "ok") {
                $('#newsletterSubscribeErrors').show().empty().html($('error-message', data).text());
            }
            else {
                closeOverlay();
            }
    	});
    	
    	return false;
    });
});

function updateLocationSelect(stateClass) {
    var selectedStoreId = $('#storeId').val();
    var flagStoreAvailable = false;
    
    if(stateClass == 'ALL' || stateClass == '') {
        $('#storeId').append($(selectGroup));
        if($('#storeId option:eq(1)').attr('value') == '') {
            $('#storeId option:eq(1)').remove();
        }
    } else {
        $('#storeId option').remove();
        $('#storeId').append('<option value="">Select...</option>');
        $(selectGroup).each(function() {
            if($(this).hasClass(stateClass)) {
                if($(this).val() == selectedStoreId) flagStoreAvailable = true;
                $('#storeId').append($(this));
            }
        });
    }
    // Select one that was already selected if available
    if(flagStoreAvailable) {
    	$('#storeId').val(selectedStoreId);
	} else {
    	$('#storeId').val('');
	}
}

function popupWindow(url, name, attributes){
	var newWindow = window.open(url, name, attributes);
	newWindow.focus();
}

function storeChangeHandler() {
	$("#storeSubmit").click();
}

function searchSubmitCheck() {
	if($('#searchTerm').val() == 'keyword search') {
		$('#searchTerm').val('');
	}		
	return true;
}

function animateTwitter() {
    /* if(bVersion == 6 || bVersion == 7) { } else {
        tweetHeight = $('.rightColumnContent').height();
        $('.rightColumnContentWrapper').animate({height: tweetHeight+'px'}, 1050, 'easeOutExpo');
    } */
}

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function showOverlay() {
	$('body').css({overflow: 'hidden'});
	if(bVersion == 7 || bVersion == 6) {
	    $('.sIFR-replaced').css({position: 'relative', top: '-9999px'});
        if(bVersion == 6) {
    	    $('html').css({'overflow-y': 'hidden'});
	        $('select').css('visibility', 'hidden');
	    }
	}
	$('.popupOverlay').css({display: 'block'});
	$('.popupContainer').css({display: 'block'});
    $('.popupOverlay').animate({opacity: 0.7}, 300, "easeOutExpo", function() {
		$('.popupContainer').animate({opacity: 1.0}, 300, "easeOutExpo");
    });
}

function closeOverlay() {
    $('body').css({overflow: 'visible'});    
    if(bVersion == 6 || bVersion == 7) {
	    $('.sIFR-replaced').css({position: 'relative', top: '0px'});
	    if(bVersion == 6) {
    	    ('html').css({'overflow-y': 'auto'});
	    }
	}
    $('.popupContainer').animate({opacity: 0.0}, 200, "easeOutExpo", function() {
		$('.popupOverlay').animate({opacity: 0.0}, 200, "easeOutExpo", function() {
    		$('.popupOverlay').css({display: 'none'});
    	    $('.popupContainer').css({display: 'none'});
    	    if(bVersion == 6) {
                $('select').css('visibility', 'visible');    	    
    	    }
		});
	});		    
}

$(window).load(
    function() {
		$('#channelSelectorList').show();
	}
);

