$(document).ready(function(){

     $('#aboutUsContent').dialog({
     draggable: false,
     autoOpen: false,
     modal: true,
     draggable: false
     });
    $('#aboutUs').click(function(){
        $('#aboutUsContent').dialog("open");
    });
    
    
    if (document.getElementById("gallery")) {
        var mygallery = new simpleGallery({
            wrapperid: "gallery", //ID of main gallery container,
            dimensions: [650, 300], //width/height of gallery in pixels. Should reflect dimensions of the images exactly
            imagearray: [["style/images/slider_images/editor650x300.png", "style/images/slider_images/editor650x300.png", "1", "Sql editor with syntax highlighting"], ["style/images/slider_images/tableedit650x300.png", "style/images/slider_images/tableedit650x300.png", "2", "Inline table editing"], ["style/images/slider_images/trigger650x300.png", "style/images/slider_images/trigger650x300.png", "3", "Create triggers, views, functions and routines with templates"], ["style/images/slider_images/search650x300.png", "style/images/slider_images/search650x300.png", "4", "Fancy live search"], ["style/images/slider_images/inlineediting650x300.png", "style/images/slider_images/inlineediting650x300.png", "5", "Inline editing of data"], ["style/images/slider_images/bookmarks650x300.png", "style/images/slider_images/bookmarks650x300.png", "6", "Bookmark Sql-queries and execute them"]],
            autoplay: [true, 8000, 2], //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
            persist: false, //remember last viewed slide and recall within same session?
            fadeduration: 150, //transition duration (milliseconds)
            oninit: function(){ //event that fires when gallery has initialized/ ready to run
                //Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
            },
            onslide: function(curslide, i){ //event that fires after each slide is shown
                //Keyword "this": references current gallery instance
                //curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
                //i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
            
            }
        });
        
        
    }
    
    $('#subscribeInput').focus(function(){
        var e = $('#subscribeInput');
        if (e.val() == "email") {
            e.val("");
        }
    });
    
    $('#subscribeInput').blur(function(){
        var e = $('#subscribeInput');
        if (e.val() == "") {
            e.val("email");
        }
    });
    
    
    $('#searchInput').focus(function(){
        var e = $('#searchInput');
        if (e.val() == "search") {
            e.val("");
        }
        if (e.val() == "please enter text") {
            e.val("");
        }
        
    });
    
    $('#searchInput').blur(function(){
        var e = $('#searchInput');
        if (e.val() == "") {
            e.val("search");
        }
    });
    
    
    $('#subscribeButton').click(function(){
        $('#validation').html("");
        var addr = $('#subscribeInput').val();
        
        if (addr == "email") {
            return;
        }
        
        if (addr != "") {
            if (isMail(addr)) {
            
            
                $.ajax({
                    type: "POST",
                    url: "subscribe.php",
                    data: "email=" + addr,
                    success: function(msg){
                    
                        $('#validation').html("submitted");
                        $('#subscribeInput').val("email");
                        var t = setTimeout(function(){
                            $('#validation').html("");
                        }, 1000)
                        
                    }
                });
                
                
            }
            else {
                $('#validation').html("no valid address");
                
            }
        }
        
        
    });
    
    
    function isMail(s){
        var a = false;
        var res = false;
        if (typeof(RegExp) == 'function') {
            var b = new RegExp('abc');
            if (b.test('abc') == true) {
                a = true;
            }
        }
        
        if (a == true) {
            reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)' +
            '(\\@)([a-zA-Z0-9\\-\\.]+)' +
            '(\\.)([a-zA-Z]{2,4})$');
            res = (reg.test(s));
        }
        else {
            res = (s.search('@') >= 1 &&
            s.lastIndexOf('.') > s.search('@') &&
            s.lastIndexOf('.') >= s.length - 5)
        }
        return (res);
    }
    
    
    $('#bigPictures a').lightBox({
        fixedNavigation: true
    });
    
    $('.screenshots').lightBox({
        fixedNavigation: true
    });
	
	
	  
    function print_r(x, max, sep, l){
    
        l = l || 0;
        max = max || 10;
        sep = sep || ' ';
        
        if (l > max) {
            return "[WARNING: Too much recursion]\n";
        }
        
        var i, r = '', t = typeof x, tab = '';
        
        if (x === null) {
            r += "(null)\n";
        }
        else 
            if (t == 'object') {
            
                l++;
                for (i = 0; i < l; i++) {
                    tab += sep;
                }
                if (x && x.length) {
                    t = 'array';
                }
                
                r += '(' + t + ") :\n";
                
                for (i in x) {
                    try {
                        r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
                    } 
                    catch (e) {
                        return "[ERROR: " + e + "]\n";
                    }
                }
                
            }
            else {
            
                if (t == 'string') {
                    if (x == '') {
                        x = '(empty)';
                    }
                }
                
                r += '(' + t + ') ' + x + "\n";
            }
        
        return r;
        
    };
    
	
    /*
  function loadFeed(){
  
  	$('#feedContainer').append('<img id="feedLoad" src="style/images/ajax-loader.gif" alt="loading..." title="loading...">');
  	$.getFeed({
  		url: 'js/jfeed/proxy.php?url=http://feeds.launchpad.net/chive/announcements.atom',
  		success: function(feed){
  			$('#feedLoad').remove();
  			 			
  			//Title    
					//$('#feedContainer').append('<h2><a href="' + feed.link + '">' + feed.title + '</a>' + '</h2>');
					//Unordered List
					var html = '<h1>Project News <a href="http://feeds.launchpad.net/chive/announcements.atom"><img id="feedRss" alt="rss" src="style/images/rss.jpeg"></a></h1><ul>';
					
					var i = 0;
					$(feed.items).each(function(){
						
						if (i == 4) {
							return;
						}
						
						
						var item = $(this);
						var str = item.attr("updated");
						
						
						var strArr = str.split("T");
						
						var date = strArr[0].split("-");
						
						var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
						
						if(date[1].substr(0, 1) == '0')
						{
							date[1] = date[1].substr(1);
						}
						var m = parseInt(date[1]);
						
						var dateStr = month[m - 1] + " " + date[2] + ", " + date[0];
						
						//trace( $item.attr("link") );
						html += '<li>' +
						'<h3><a href ="' +
						item.attr("link") +
						'" >' +
						item.attr("title") +
						'</a></h3> ' +
						'<span id="feedDate">' +
						dateStr +
						'</span>' +
						'</li>';
						i++;
					});
					html += '</ul><span id="feedMore"><a href="https://launchpad.net/chive/+announcements">read more</a></span>';
					$('#feedContainer').html(html);
				}
			});
			
		}
    
    if (document.getElementById('feedContainer')) {
        loadFeed();
    }
    */
    $('.compTip').tooltip({
        delay: 0,
        showURL: false,
        bodyHandler: function(){
            return $("<img/>").attr("src", $(this).attr("name"));
        }
    });

    
});

function show_big(picId){

    $('#bigPictures  a[rel=' + picId + ']').click();
}


function checkSearchForm(){

    if ($('#searchInput').val() == "") {
    
        $('#searchInput').val("please enter text");
        return false;
    }
    
    if ($('#searchInput').val() == "search") {
        $('#searchInput').val("please enter text");
        return false;
    }
    
    if ($('#searchInput').val() == "please enter text") {
        $('#searchInput').val("please enter text");
        return false;
    }
    
    return true;
    
}









