﻿var header_link = '#'

$(document).ready(function () {

    $('ul#menu').hide();

    if ($('body.startpage').length > 0) {

        $('#content img.imagecache-postcard-large').hide();
        $('#content img.imagecache-postcard-large:first').show();
        $('#image_blocker').append(
			'<br/>'+
			'<img class="prev" src="prinfo_new/img/card_arrow_prev.png"/> '+
			'<img class="next" src="prinfo_new/img/card_arrow_next.png"/>'
		);
        $('#image_blocker').show();

        if ($('#startfooter p').length > 0) {
            var photoAlt = $('#content img.imagecache-postcard-large:first').attr('alt');
            $('#startfooter p').append('<br/><span id="photog">' + photoAlt + "</span>");
        }
		enablePostCardClick('#content img.imagecache-postcard-large');
    }

    if ($('#sidebar div.postcard').length > 0) {

        $('#sidebar div.postcard').hide();
        $('#sidebar div.postcard:first').show();
        enablePostCardClick('#sidebar div.postcard');

    }

    $('a#menu-opener').click(function () {
        if ($('ul#menu').css('display') == 'none') {
            $('div#content').css({ opacity: 0.3 });
            $('div#topframe').css({ opacity: 0.3 });
            $('div#postcard').css({ opacity: 0.3 });
            $('div#sidebar ul.share').css({ opacity: 0.3 });
            $('div#footer').css({ opacity: 0.3 });
            $('ul#twitter-feed').css({ opacity: 0.3 });
            $('a#pcg_link').css({ opacity: 0.3 });
            $('div#image_blocker').css({ opacity: 0.3 });
            $('ul#menu').fadeIn(200);
        }
        else {
            $('div#content').css({ opacity: 1 });
            $('div#topframe').css({ opacity: 1 });
            $('div#postcard').css({ opacity: 1 });
            $('div#sidebar ul.share').css({ opacity: 1 });
            $('div#footer').css({ opacity: 1 });
            $('ul#twitter-feed').css({ opacity: 1 });
            $('a#pcg_link').css({ opacity: 1 });
            $('div#image_blocker').css({ opacity: 1 });
            $('ul#menu').fadeOut(200);
        }
    })

    $('span#scandinavian').click(function () {
        document.location = $('span#prinfo a').attr('href');
    });

    $('span#prinfo').click(function () {
        document.location = $('span#prinfo a').attr('href');
    });


    $('ul#menu li span.expander').click(function () {
        $(this).parent().parent().children('ul').toggle(200).show();
		var $expander = $(this);
		if ($expander.hasClass('plus')) {
			$expander.removeClass('plus');
			$expander.addClass('minus')
		} else if ($expander.hasClass('minus')) {
				$expander.removeClass('minus');
				$expander.addClass('plus');
		}
        return false;
    });
	
	 $('#menu li a').click(function () {
        $(this).children('ul').toggle(200).show();
		var $expander = $(this);
		if ($expander.hasClass('plus')) {
			$expander.removeClass('plus');
			$expander.addClass('minus')
		} if ($expander.hasClass('')) {
			$expander.addClass('plus')
		} else if ($expander.hasClass('minus')) {
				$expander.removeClass('minus');
				$expander.addClass('plus');
		}
        return false;
    });

    $('ul#menu li a span.linker').click(function () {
        var url = $(this).parent().attr('href');
        if (url.length > 0)
            document.location = url;

        return false;

    })

    if ($('h2.list').length > 0) {

        $('div.news-item').hide();
        for (var i = 0; i < 2; i++) {
            if ($('h2.list').length > i)
                $('div.news-item').eq(i).show();
        }

        $('h2.list').click(function () {
            if ($(this).next().css('display') == 'none') {
                $(this).next().fadeIn(200);
            }
            else
                $(this).next().fadeOut(200);
        });

    }

    $('ul#menu li a').each(function () {

        var name = 'ul#menu li a.' + $(this).attr('class');
        /*if ($(name).length > 1) {
            $(this).find('span.expander').addClass('plus');
        }*/

    });

    if ($('#sub_menu a.active').length > 0) {
        var the_href = $('#sub_menu a.active').attr('href').replace('http://', '');
        var url_id = the_href.substr(the_href.indexOf('/'), the_href.length);
        var noHitYet = true;
        var p_title = '';

        $('ul#menu li a').each(function () {


            var tmp_url_id = $(this).attr('href').substr($(this).attr('href').indexOf('/'), the_href.length);
            //alert(tmp_url_id);
            //alert(tmp_url_id + '==' + url_id);
            if (tmp_url_id == url_id && $(this).hasClass('head') == false && noHitYet) {
                var $prev = $(this).parent();

                my_while:
                while ($prev.length > 0) {

                    if ($prev.attr('class') == 'head') {
                        //alert($prev.find('span.linker').html());
                        p_title = $prev.find('span.linker').html();
                        noHitYet = false;
                        break my_while;
                    }
                    $prev = $prev.prev();
                }
            }
        });

        if ($('h1#page-title').html() != p_title) {
            $('h1#page-title').html(p_title + ' <span>/' + $('h1#page-title').html() + '</span>');
        }
    }

    if ($('img#locator').length > 0) {

        $('div#map_search a.m1').click(function () {

            $('#content div.country_list').fadeIn(200);
            $('#content div.city_list').hide();
            $('#content div.locator_office').hide();
            $('#content div.locator_list').hide();

        });

        $('div#map_search a.m2').click(function () {

            $('#content div.country_list').hide()
            $('#content div.city_list').fadeIn(200);
            $('#content div.locator_office').hide();
            $('#content div.locator_list').hide();

        });

        $('img#locator').click(function () {

            $('#content div.country_list').hide();
            $('#content div.city_list').hide();
            $('#content div.locator_office').hide();
            $('#content div.locator_list').fadeIn(200);

        });

        $('div#map_search a.m3').click(function () {

            $('#content div.country_list').hide();
            $('#content div.city_list').hide();
            $('#content div.locator_office').hide();
            $('#content div.locator_list').fadeIn(200);

        });

        $('#content div.country_list table td').click(function () {
            $('#content div.country_list').fadeOut(200, function () { $('#content div.city_list').fadeIn(200); });
        });

        $('#content div.city_list table td').click(function () {
            $('#content div.city_list').fadeOut(200, function () { $('#content div.locator_list').fadeIn(200); });
        });

        $('#content div.locator_list table td').click(function () {
            $('#content div.locator_list').fadeOut(200, function () { $('#content div.locator_office').fadeIn(200); });
        });

        $('#content div.country_list img.close').click(function () {

            $('#content div.country_list').fadeOut(200);

        });

        $('#content div.city_list img.close').click(function () {

            $('#content div.city_list').fadeOut(200);

        });

        $('#content div.locator_list img.close').click(function () {

            $('#content div.locator_list').fadeOut(200);

        });

        $('#content div.locator_office img.close').click(function () {

            $('#content div.locator_office').fadeOut(200);

        });
    }
    if ($('#case-list a').length > 0) {
        $('#case-list a').wrapInner('<span class="img_wrap" />');

        $('#case-list a').mouseover(function () {

            $('#case-list a').css({ opacity: 0.7 });
            $(this).css({ opacity: 1 });

        });
    }

    if ($('div#content div.field-field-case-image img').length > 1) {

        var imgs = '';
        var links = '';

        $('div#content div.field-field-case-image img').hide();
        $('div#content div.field-field-case-image img').each(function () {
            scaleImg($(this));
            imgs += '<div>' + $(this).parent().html() + '</div>\n';
            links += '<li><a href="#">no</a></li>\n';
        });

        var str = '<div id="loopedSlider">\n' +
		    '<div class="container">\n' +
			    '<div class="slides">\n' +
				    imgs +
			    '</div>\n' +
		    '</div>\n' +
		    '<a href="#" class="previous"><img src="/sites/all/themes/prinfo/css/case-arrow-1.jpg" alt="previous" /></a>\n' +
            '<a href="#" class="next"><img src="/sites/all/themes/prinfo/css/case-arrow-2.jpg" alt="previous" /></a>\n' +
            '<ul class="pagination">\n' +
                links +
            '</ul>\n' +
        '</div>\n';

        $('div#content div.field-field-case-image').prepend(str);
        $('div#loopedSlider img').show();
        $("#loopedSlider").loopedSlider();
    }

    if ($('img#locator').length > 0) {

        $('div#map_search a.m1').click(function () {

            $('#content div.country_list').fadeIn(200);
            $('#content div.city_list').hide();
            $('#content div.locator_office').hide();
            $('#content div.locator_list').hide();

        });

        $('div#map_search a.m2').click(function () {

            $('#content div.country_list').hide()
            $('#content div.city_list').fadeIn(200);
            $('#content div.locator_office').hide();
            $('#content div.locator_list').hide();

        });

        $('img#locator').click(function () {

            $('#content div.country_list').hide();
            $('#content div.city_list').hide();
            $('#content div.locator_office').hide();
            $('#content div.locator_list').fadeIn(200);

        });

        $('div#map_search a.m3').click(function () {

            $('#content div.country_list').hide();
            $('#content div.city_list').hide();
            $('#content div.locator_office').hide();
            $('#content div.locator_list').fadeIn(200);

        });

        $('#content div.country_list table td').click(function () {
            $('#content div.country_list').fadeOut(200, function () { $('#content div.city_list').fadeIn(200); });
        });

        $('#content div.city_list table td').click(function () {
            $('#content div.city_list').fadeOut(200, function () { $('#content div.locator_list').fadeIn(200); });
        });

        $('#content div.locator_list table td').click(function () {
            $('#content div.locator_list').fadeOut(200, function () { $('#content div.locator_office').fadeIn(200); });
        });

        $('#content div.country_list img.close').click(function () {

            $('#content div.country_list').fadeOut(200);

        });

        $('#content div.city_list img.close').click(function () {

            $('#content div.city_list').fadeOut(200);

        });

        $('#content div.locator_list img.close').click(function () {

            $('#content div.locator_list').fadeOut(200);

        });

        $('#content div.locator_office img.close').click(function () {

            $('#content div.locator_office').fadeOut(200);

        });
    }

    if ($('div#agencies').length > 0) {
        var offset = $('img#agency_map').offset();
        $('div#agency-list').css('top', offset.top+ 30);
        $('div#agency-table').css('top', offset.top+ 30);
        $('div#agency-list').css('left', offset.left + 10);
        $('div#agency-table').css('left', offset.left + 10);
        $('img.close').click(function () { hideRegionObjects(); });
        loadSource();
    }


    if ($('h1#page-title').length > 0 && $('div#sub_menu').length == 0) {

        if (GetVirtualDirectory().length > 0) {

            var currentUrl = GetVirtualDirectory().toLowerCase();
            var menu_flag = false;
            var header = '';
            var spaner = '';

            $('ul#menu li a').each(function () {
                var url = $(this).attr('href').toLowerCase();

                if (currentUrl == url && menu_flag == false) {

                    var $link_q = $('ul#menu li a.' + $(this).attr('class'));

                    if ($link_q.length > 1) {

                        var choosen_link = $(this).attr('href').toLowerCase();
                        var menu_html = '';
                        var sel_class = '';
                        var noOfLinks = 0;
                        var is_selected;

                        $link_q.each(function () {

                            is_selected = false;
                            sel_class = '';

                            if (choosen_link == $(this).attr('href').toLowerCase()) {
                                is_selected = true;
                                sel_class = ' class="active"';
                                spaner = $(this).html();
                            }

                            if ($(this).find('span.linker').length > 0) {
                                header_link = $(this).attr('href');
                                header = $(this).find('span').html();
                                //menu_html += '<a' + sel_class + ' href="' + header_link + '">' + $(this).find('span.linker').html() + '</a>  | ';
                                noOfLinks++;
                                $('#content h1').click(function () { document.location = header_link; });
                            }
                            else {
                                menu_html += '<a' + sel_class + ' href="' + $(this).attr('href') + '">' + $(this).html() + '</a>  | ';
                                noOfLinks++;
                            }
                        });

                        if (noOfLinks == 1 && is_selected)
                            menu_flag = true;
                        else {
                            $('#content h1').after('<div id="sub_menu">' + menu_html.substr(0, menu_html.length - 3) + '</div>');
                            $('h1#page-title').html(header + ' <span>/' + spaner + '</span>');
                        }
                        menu_flag = true;

                    }

                }
            });


            //                if (currentUrl == url && menu_flag == false) {

            //                    
            //                    if ($link_q.length > 1) {

            //                        var choosen_link = $(this).attr('href').toLowerCase();
            //                        var menu_html = '';
            //                        var sel_class = '';
            //                        var noOfLinks = 0;
            //                        var is_selected;

            //                        $link_q.each(function () {

            //                            is_selected = false;
            //                            sel_class = '';

            //                            if (choosen_link == $(this).attr('href').toLowerCase()) {
            //                                is_selected = true;
            //                                sel_class = ' class="current"';
            //                            }

            //                            if ($(this).find('span.linker').length > 0) {
            //                                header_link = $(this).attr('href');
            //                                //menu_html += '<a' + sel_class + ' href="' + header_link + '">' + $(this).find('span.linker').html() + '</a>  | ';
            //                                noOfLinks++;
            //                                $('#content h1').click(function () { document.location = header_link; });
            //                            }
            //                            else {
            //                                menu_html += '<a' + sel_class + ' href="' + $(this).attr('href') + '">' + $(this).html() + '</a>  | ';
            //                                noOfLinks++;
            //                            }


            //                        });

            //                        if (noOfLinks == 1 && is_selected)
            //                            menu_flag = true;
            //                        else
            //                            $('#content h1').after('<div id="sub_menu">' + menu_html.substr(0, menu_html.length - 3) + '</div>');

            //                        menu_flag = true;
            //                    }

            //                    var $prev = $(this).parent();

            //                    my_while:
            //                    while ($prev.length > 0) {

            //                        if ($prev.attr('class') == 'head') {
            //                            $prev.find('a').trigger('click');
            //                            break my_while;
            //                        }
            //                        $prev = $prev.prev();
            //                    }

            //                    return;
            //                }
            //            });
        }
	}

	$('ul#twitter-feed li').click(function () {
		document.location = "/news";
	});
});

var m_agencySource = new Array();
function loadSource() {

    $('ul#intra_list li').each(function () {
        var m_item = new Array();
        $(this).find('span').each(function () {
            m_item.push($(this).html());
        });
        m_agencySource.push(m_item);
    });

}

function showRegionListOnName(nameIn) {

	hideRegionObjects();
	$('div#agency-list ul').empty();
	
	for(var i=0;i<m_agencySource.length;i++) {

	    if ((getCountryOnRegion(m_agencySource[i][11]).toLowerCase()).indexOf(nameIn) >= 0)
		{
			var img_cmp = '';
			if(m_agencySource[i][5].toLowerCase() == 'pcg')
				img_cmp = '<img src="/sites/all/themes/prinfo/css/pcg-smaller.gif" alt="PCG" align="right" style="margin-top:10px; margin-right: 20px;"/>';
			
			$('div#agency-list ul').append('<li class=' + i + '>' + img_cmp + m_agencySource[i][1] + ' &raquo;<br/>' + m_agencySource[i][4] + '<br/>' + m_agencySource[i][3] + '<br/></li>')
		}
	}
	$('div#agency-list').fadeIn();

	$('div#agency-list ul li').click(function () {
	    showAgency($(this).attr('class'));
	});

    if($('div#agency-list ul li').length == 1)
        $('div#agency-list ul li:first-child').click();  	
	
}

function getCountryOnRegion(regionIn) {

    switch (regionIn.toLowerCase()) {
        case "region viken":
            return "norway";
        case "region viken":
            return "norway";
        case "region vest":
            return "norway";
        case "region vest":
            return "norway";
        case "region nor":
            return "norway";
        case "danmark":
            return "denmark";
        case "lettland":
            return "latvia";
        case "finland":
            return "finland";
        default:
            return "sweden";
    }
   
}

function showRegionListOnSearch(termIn)
{
	alert('showRegionListOnSearch');
}

function showAgency(indexIn)
{
    hideRegionObjects();
    var myFileName = ''
    $('#sidebar span.p_card_title').each(function (index) {
        if ($(this).html().toString().toLowerCase() == m_agencySource[indexIn][3].toLowerCase()) {
            myFileName = new String($('#sidebar div.postcard').eq(index).find('img').attr('src'));
            myFileName = myFileName.substring(myFileName.lastIndexOf("/") + 1, myFileName.length);
        }
    });

    var isPCG = (m_agencySource[indexIn][5] == '1');

	$('div#agency-table table').empty();
	$('div#agency-table table').append('<tr>' +
      '<td class="td_1"><img src="http://www2.prinfo.se/sites/default/files/imagecache/postcard-tiny/news/images/' + myFileName + '" alt="' + m_agencySource[indexIn][3]  + '" title=""  class="imagecache imagecache-postcard-tiny imagecache-default imagecache-postcard-tiny_default" width="161" height="110" /></td>' +
      '<td>' +
        (isPCG ? '<img src="/sites/all/themes/prinfo/css/pcg-smaller.gif" alt="PCG" align="right" style="margin-right: 15px">' : '' ) +
        '<b>Agency:</b> ' + m_agencySource[indexIn][1] + '<br/>' +
        '<b>City:</b> ' + m_agencySource[indexIn][3] + '<br>' +
        '<b>Address:</b> ' +  m_agencySource[indexIn][4] + ', ' + m_agencySource[indexIn][3] + '<br/>' +
        '<b>Telephone:</b> ' + m_agencySource[indexIn][9] + '<br>' +
        '<b>Web:</b> <a href="http://' + m_agencySource[indexIn][10] + '" target="_blank">' + m_agencySource[indexIn][10] + '</a><br>' +
      '</td>' +
     '</tr>');
	$('div#agency-table').fadeIn();
}

function hideRegionObjects()
{
	$('div#agency-list').fadeOut();
	$('div#agency-table').fadeOut();
}

function scaleImg($imgObj)
{
	var maxWidth = 605;
	var maxHeight = 370;
	var c_width = $imgObj.attr('width');
	var c_height = $imgObj.attr('height');

	
	if(maxWidth > c_width || maxHeight > c_height)
		return;
	
	var n_width = c_width;
	var n_height = c_height;
	
	if(n_width > maxWidth)
	{
		var factor = (maxWidth/n_width)
		n_width = maxWidth;
		n_height = (factor * n_height);
	}
	
	if(n_height > maxHeight)
	{
		var factor = (maxHeight/n_height)
		n_height = maxHeight;
		var n_width = (factor * n_width);
	}

	$imgObj.css('width', n_width);
	$imgObj.css('height', n_height);	
}

function enablePostCardClick(selector) {

    function flipCard(cObject, cBack, cIndex) {
        if (cObject.css('display') != 'none') {

            if (!cBack) {
                if ((cIndex + 1) >= $(selector).length) {
                    cObject.fadeOut(200, function () { $(selector).eq(0).fadeIn(200); });
                    replacePhotoG($(selector).eq(0));
                }
                else {
                    cObject.fadeOut(200, function () { $(selector).eq(cIndex + 1).fadeIn(200); });
                    replacePhotoG($(selector).eq(cIndex + 1));
                    return (false);
                }
            }
            else {
                if ((cIndex - 1) < 0) {
                    cObject.fadeOut(200, function () { $(selector).eq($(selector).length - 1).fadeIn(200); });
                    replacePhotoG($(selector).eq(0));
                }
                else {
                    cObject.fadeOut(200, function () { $(selector).eq(cIndex - 1).fadeIn(200); });
                    replacePhotoG($(selector).eq(cIndex - 1));
                    return (false);
                }
            }

        }
    }

    $(selector).click(function (e) {

        var width = $(this).css('width').replace('px', '');
        var middlePx = $(this).position().left + (width / 2);

        $(selector).each(function (index) {

            flipCard($(this), (e.pageX >= middlePx), index);

        });

    });

    $('div#image_blocker img.prev').click(function (e) {
        $(selector).each(function (index) {
			flipCard($(this), true, index);
		});
    });
    $('div#image_blocker img.next').click(function (e) {
        $(selector).each(function (index) {
			flipCard($(this), false, index);
		});
    });

}

            function replacePhotoG(object) {
                if ($('#startfooter p').length > 0) {
                    var photoAlt = object.attr('alt');
                    $('span#photog').html(photoAlt);
                }
            }

            function GetVirtualDirectory() 
            {
                var str = '';

                if ($('h2#case_h2').length > 0)
                    str = document.referrer;
                else
                    str = document.location.toString().toLowerCase();

//                if (document.location.toString().indexOf('case') > 0) {
//                    str = document.location.toString().toLowerCase();
//                }
//                else {
//                    str = document.referrer;
//                }

                if (str.length > 0) {
                    str = str.replace(window.location.host.toString().toLowerCase(), "");
                    str = str.replace("http://", "");
                }

                return str;

            }

