/*
 * Front-End scripts
 * 
 * @name default.js
 * @package cnc
 * @version 1.0
 * @since 2010-07-12
 * @author Cristian Ciobanu <cristian@pallasweb.com>
 */

// Raise a popup
function openWindow(url,width,height) {day = new Date();id = day.getTime();window.open(url, id, "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left="+((screen.width - width)/2)+",top="+((screen.height - height)/2));return false;}
// Open links in a new window
function externalLinks() {if (!document.getElementsByTagName) return;var anchors = document.getElementsByTagName("a");for (var i=0; i<anchors.length; i++) {var anchor = anchors[i];if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")anchor.target = "_blank";}}
// Turns a tag into a link
function href(url) {window.location = url;return false;}

var logos_smf = 0;

$(document).ready(function() {
    browserDetection();
    fixIE6alphaTransparency();
    $("#news").newsTicker();
    externalLinks();

    if ($('#home-media').length > 0) {
        $('#home-media').flashembed({src: "assets/flash/home.swf", wmode:'transparent'});
    }
    
    // Home Slideshow
    if ($('#slideshow').length > 0) {
        setInterval("fadeInOut('slideshow')", 5000);
    }
    
    
    if ($('#leftlogos').length > 0 && $('#rightlogos').length > 0) {
        setInterval("fadeInOutLogos()", 5000);
    }
    
    
	$('a.player').overlay({
        target: '.overlay',
        top: 100,
        mask: {
            color: '#000',
            loadSpeed: 200,
            opacity: 0.5
        },
        onBeforeLoad: function() {
            var wrap = this.getOverlay().find(".wrap");
            var media = this.getTrigger().attr("href");
            wrap.html('<div class="video-player"></div>');
            flowplayer('.video-player', {
                src: "assets/player/flowplayer-3.1.5.swf",
                version: [9, 115],
                onFail: function()  {
                    wrap.html("You need the latest Flash version to view MP4 movies. Your version is " + this.getVersion());
                }
            }, {
                clip: media
            });
        },
        onBeforeClose: function() {
            this.getOverlay().find(".wrap").empty();
        }
    });
    $.each($('img.gallery'), function(i, e) {
        $(this).overlay({
            target: '.photo-overlay',
            top: 100,
            mask: {
                color: '#000',
                loadSpeed: 200,
                opacity: 0.5
            },
            onLoad: function() {
				$(".photos-scrollable").data("scrollable").seekTo(i, 0);
			}
        });
    });
    $(".photos-scrollable").scrollable();
    
    // Date (Range) input
    if ($('.date').length > 0) {
        $(".date").dateinput({format: 'yyyy-mm-dd', trigger: true});
    }
    if ($('.date-range').length > 0) {
        $(".date-range").dateinput({format: 'dddd, mmmm dd, yyyy', trigger: true, min: -1});
        $(".date-range").bind("onShow onHide", function()  {
            $(this).parent().toggleClass("active");
        });
        $(".date-range:first").data("dateinput").change(function() {
            $(".date-range:last").data("dateinput").setMin(this.getValue(), true);
        });
    }

});

function swapPIC(id, source) {
	document.getElementById(id).src = source;
	return false;
}


function inputFocus(e, v) {
    if ($(e).val() == v) {
        $(e).val('');
    }
    $(e).addClass('focus');
}

function inputBlur(e, v) {
    if ($.trim($(e).val()) == '') {
        $(e).val(v);
        $(e).removeClass('focus');
    }
}

function validateEmail(email)
{ 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return email.match(re)
}

function validatePhone(phone)
{
    var re = /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/
    return phone.match(re)
}

function validateZip(zip)
{
    var re = /^\d{5}?$/
    return zip.match(re)
}

// See: http://jonraasch.com/blog/a-simple-jquery-slideshow
function fadeInOut(selector) {
    var $active = $('#' + selector + ' img.active');
    if ( $active.length == 0 ) $active = $('#' + selector + ' img:last');
    var $next =  $active.next().length ? $active.next() : $('#' + selector + ' img:first');
    $active.addClass('last-active');
    $next.css({
        opacity: 0.0
    })
    .addClass('active')
    .animate({
        opacity: 1.0
    }, 2000, function() {
        $active.removeClass('active last-active');
    });
}
// See: http://jonraasch.com/blog/a-simple-jquery-slideshow
function fadeInOutLogos() {
	if (logos_smf % 2 == 0) {
		selector = "leftlogos";
	} else {
		selector = "rightlogos";
	}
    var $active = $('#' + selector + ' img.active');
    if ( $active.length == 0 ) $active = $('#' + selector + ' img:last');
    var $next =  $active.next().length ? $active.next() : $('#' + selector + ' img:first');
    $active.addClass('last-active');
    $next.css({
        opacity: 0.0
    })
    .addClass('active')
    .animate({
        opacity: 1.0
    }, 2000, function() {
        $active.removeClass('active last-active');
    });
    logos_smf++;
}
