(function($) {
    var ws_href_regex = /[[cid|sid]\=[-][0-9]\&]*$/;

    function loadPage(firsttime, data) {
        $.fancybox.showActivity();
        if (!data) {
            data = '';
        }
        $('#ws_div').load('index.php?option=com_ideafarms&controller=worksamples&format=raw', data, function () {
            if (firsttime) {
                $.fancybox($('#ws_content'), {
                    titlePosition       : "inside",
                    transitionIn        : "none",
                    transitionOut       : "none",
                    hideOnOverlayClick  : false,
                    hideOnContentClick  : false,
                    width               : 835,
                    height              : 470,
                    padding             : 0,
                    margin              : 0,
                    scrolling           : 'no',
                    autoScale           : true,
                    centerOnScroll      : true,
					autoDimensions		: false
                });
            }
			$('#fancybox-inner , #fancybox-outer' ).width('835px');
            $.fancybox.resize();
			$('#ws_content a').click(linkClicked);
        });
    }
	
    function show_ws_lb(event, href, firsttime) {
		// expression that starts with the hash and goes on till the end of the string and no has in between thats what we need
        //href = href.replace("#", "");
		
		var patt1=/\#[\=|a-z|0-9|A-Z|\&]+$/;
        href = href.match(patt1);
        href = href[0];
        href = href.replace('#', '');
        loadPage(firsttime, href);
		
        if (event) {
            event.preventDefault();
        }
        
        return false;
    }

	
	
    function loadContactUsPage() {
	      
        $('#ws_div').load('/contactus.html', null, function () {
            $.fancybox($('#cntct_us'), {
                titlePosition       : "inside",
                transitionIn        : "none",
                transitionOut       : "none",
                hideOnOverlayClick  : false,
                hideOnContentClick  : false,
                width               : 100,
                height              : 300,
                padding             : 0,
                margin              : 0,
                scrolling           : 'no',
                autoScale           : false,
                centerOnScroll      : true
            });
            $.fancybox.resize();
        });
    }
	
	
	var linkClicked = function(event) {
            var href = $(this).attr('href');
            show_ws_lb(event, href, true);
            event.preventDefault();
            return false;
        }

    $(document).ready(function() {
        $(worksamples_selector + ', a.ws-trigger').click(linkClicked);

        $(cu_selector).click(function(event) {
            loadContactUsPage();
            event.preventDefault();
            return false;
        });
		$('a.image').fancybox({
		autoDimensions:true,
		autoScale:true
		});
    });
})(jQuery);
