$(document).ready(function() {

 	 //determine ff
	var firefox = jQuery.browser.mozilla;
	
	//alert(jQuery.browser.version);
	
	$('input[type=file]').change(function(e){
	  $("#filename").html($(this).val());
	});

	//thank you fancybox
 	$('a.ty_link').live('click', function() {
  		$(this).fancybox({
  		'frameWidth'			: 100,
		'frameHeight'			: 100,
		'overlayOpacity'		: 0.7,
		'overlayColor'			: '#000',
			callbackOnShow : function () { 
				
					if (!firefox){
					
					settings = {
						  tl: { radius: 10 },
						  tr: { radius: 10 },
						  bl: { radius: 10 },
						  br: { radius: 10 },
						  antiAlias: true,
						  autoPad: true,
						  validTags: ["div"]
					  }
					  
				
				curvyCorners(settings, ".fancy_corner"); 
			}
			}
  		
  		});
	});
	
	//fancy box
	$("a.iframe").fancybox({
		'frameWidth'			: 340,
		'frameHeight'			: 310,
		'overlayOpacity'		: 0.7,
		'overlayColor'			: '#000',
		callbackOnShow : function () { 
				if (!firefox){
					
					setTimeout("window.frames[0].location.reload(true);", 200);
					
					//ie 6/7 hack
					settings = {
					  tl: { radius: 10 },
					  tr: { radius: 10 },
					  bl: { radius: 10 },
					  br: { radius: 10 },
					  antiAlias: true,
					  autoPad: true,
					  validTags: ["div"]
					}
					
					curvyCorners(settings, ".fancy_corner"); 
					
				}
			
			}
		
	});
	
	
	//close fancybox
	$('a.close').click(function(){ parent.top.$('#fancy_close').trigger('click'); });
	$('a.cancel').click(function(){ parent.top.$('#fancy_close').trigger('click'); });
	
	$('input[type=checkbox]').click( function(){
		if ( $('#agree').is(':checked') ){
			$("#agreetext").attr("style", "display:none;");
		}  
	});

	//accept agreement
	$("a.accept").click(function () { 
		
		if ( $('#agree').is(':checked') ){
			
			$("#agreetext").attr("style", "display:none;");
			
			//close fancy
			parent.top.$('#fancy_close').trigger('click');
			
			var email = $("#form_field_email_agree").val();
			parent.top.location.href = '/Plugin.php?email='+email;
			 
			//if firefox create box
			if (firefox){
					
					parent.top.$("#container").after(
						'<script>' + 
							'function CloseWindow() { ' + 
								'document.getElementById("popup").style.display=\'none\';' + 
								'document.getElementById("popup-dl-firefox").style.display=\'none\';' + 
							 '}'+
						'</script>'+
						'<div id="popup"></div>' + 
							'<div id="popup-dl-firefox" onclick=\'CloseWindow();\'>' +
								'Firefox?<br />Please click<br /><em>allow</em> to<br />install heeii' +
							'</div>'
					);
					
			}  
		}else {
			$("#agreetext").attr("style", "display:inline;");
		}
		
		
	});
			
	//on success show arrow
	if (firefox){
		
		var txt = '<br/>Click to <br/>show / hide Heeii';
		var curUrl = window.location.href;
 		if (curUrl.match(/success/) && !curUrl.match(/url/i)){
			//dutch
			if ($.cookie('lang') == 'nl' && !curUrl.match(/lang=en/i)){
				txt = '<br/>Klik om Heeii te tonen / verbergen';
			}
			
			parent.top.$("#container").after(
						'<script>' + 
							'function CloseWindow() { ' + 
								'document.getElementById("popup").style.display=\'none\';' + 
								'document.getElementById("popup-dl-firefoxbelow").style.display=\'none\';' + 
							 '}'+
						'</script>'+
						'<div id="popup"></div>' + 
							'<div id="popup-dl-firefoxbelow" onclick=\'CloseWindow();\'>' +
								txt +
							'</div>'
					);
		}
	}	
	
	
	//submenu
	$('li.hassubmenu').hover(function () {
		
			if ( $('ul.submenu').is(':visible') ) {
				return true;
			}else {
				$('ul.submenu').slideToggle('fast'); 
				return false;  
			}
		},
		function () {
			$('ul.submenu').hide();
		}
		
	);
	
     //succesful feedback
     if ( $("#succes").val() == 1 ){
		  // fancybox workaround
		  $('body').append('<a class="ty_link" href="#ty_text"></a>');
		  $('a.ty_link').slideDown('fast', function() { $('a.ty_link').click(); });
		  $('a.ty_link').click();
     }
    
	//open in new window
	var links = document.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
      var rels = links[i].getAttribute("rel");
      if (rels) {
        var testpattern = new RegExp("external");
        if (testpattern.test(rels)) {
          links[i].onclick = function() {
            return !window.open(this.href);  
          }
        }
      }    
    } 

 //after page load show flashmovie, no flicker
 setTimeout('$(".swf_holder").css("display","block");', 500);

});



/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
